我想从以下形式的XML文件进行CSV创建CSV:如何使用XSLT从XML
<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Header>
<env:MessageSentDateTime>2012-09-19T09:50:04Z</env:MessageSentDateTime>
<env:MessageSequenceNumber>856432</env:MessageSequenceNumber>
</Header>
<Body>
<Data>
<Data:ID>
<Data:AODB>9346280</Data:AODB>
<Data:Ref>
<common:Code>HJ</common:Code>
<common:num>8113</common:num>
</Data:Ref>
</Data:ID>
... Continues like this, no set number of nodes, parts or AnotherParts
</Body>
Second message starting with <Header> ending with </Body>,
will be more than 2 in practice
</Envelope>
我想提出一个换行符CSV文件在/身体标签因为这表示一条新消息。在正文部分中会有不同数量的节点,不同数量的部分和不一致的末端节点的消息混合。另外,将会有不包含任何文本的节点,但我仍然需要逗号。
到目前为止,我有:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*[not(*)]">
<xsl:value-of select="normalize-space(.)"/>
<xsl:text>,</xsl:text>
</xsl:template>
<xsl:template match="Body[last()]">
<xsl:value-of select="normalize-space(.)"/>
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
它还在机身的最后一条信息后加逗号。我想用换行符替换那个逗号,有没有简单的方法来做到这一点?
问候, 大卫
试试这个:http://stackoverflow.com/questions/3056579/convert-xml-document-to-comma-delimited-csv-file-using-xslt-stylesheet –