【问题标题】:How to use xslt to create a csv from xml如何使用 xslt 从 xml 创建 csv
【发布时间】:2012-11-27 10:03:33
【问题描述】:

我正在尝试从表单的 xml 文件制作 csv:

    <?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 文件的 /Body 标记处添加一个换行符,因为这表示一条新消息。消息会混合在一起,因此在 Body 部分中有不同数量的节点、不同数量的部分以及没有一致的端节点。此外,会有一些节点不包含任何文本,但我仍然想要一个逗号。

到目前为止我有:

    <?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>&#10;</xsl:text>
        </xsl:template>
    </xsl:stylesheet>

它还在 Body 中的最后一条信息之后添加一个逗号。我想用换行符替换那个逗号,有没有简单的方法可以做到这一点?

问候, 大卫

【问题讨论】:

标签: xml xslt


【解决方案1】:

一种方法是更改​​与 Body 元素匹配的模板,以专门查找“叶子”子元素

<xsl:template match="Body">
   <xsl:apply-templates select=".//*[not(*)]"/>
   <xsl:text>&#10;</xsl:text>
</xsl:template>

然后,在匹配叶元素的模板中,您可以将其更改为仅在它不是找到的第一个元素时输出逗号

  <xsl:if test="position() &gt; 1">
     <xsl:text>,</xsl:text>
  </xsl:if>

如果您想尝试一下,这里是完整的 XSLT:

<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:if test="position() &gt; 1">
         <xsl:text>,</xsl:text>
      </xsl:if>
      <xsl:value-of select="normalize-space(.)"/>
   </xsl:template>

   <xsl:template match="Body">
      <xsl:apply-templates select=".//*[not(*)]"/>
      <xsl:text>&#10;</xsl:text>
   </xsl:template>
</xsl:stylesheet>

【讨论】:

  • 您好 Tim C,感谢您的回复。这段代码用最后的逗号解决了问题,但它没有在输出中给我一个新行
  • 您能否尝试在 XSLT 中将 &amp;#10; 替换为 NEWLINE,因为这将证明实际上正在使用匹配 Body 的模板。此外,如果您编辑您的问题以显示带有实际数据的 XML 的完整示例,这将有所帮助(不过没什么大不了的!)。谢谢!
  • 我尝试用 NEWLINE 替换,但没有使用模板。我用一些实际的 xml 数据编辑了这个问题
【解决方案2】:

我的规范略有变化,需要节点路径和值以使每个条目唯一。这是我用来解决问题的代码:

    <?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:output>
    <xsl:strip-space elements="*"/>

    <xsl:template match="*[*]">
        <xsl:param name="elementNames" select="''"/>
        <xsl:apply-templates select="*">
            <xsl:with-param name="elementNames">
                <xsl:copy-of select="$elementNames"/>
                <xsl:value-of select="replace(name(.), ':', '.')"/>
                <xsl:text>_</xsl:text>
            </xsl:with-param>
        </xsl:apply-templates>
     </xsl:template>

     <xsl:template match="*[not (*)]">
         <xsl:param name="elementNames" select="''"/>
         <xsl:copy-of select="$elementNames"/>
         <xsl:value-of select="replace(name(.), ':', '.')"/>
         <xsl:value-of select="name()"/>,<xsl:apply-templates select="current()/text()"/>
         <xsl:text>&#10;</xsl:text>
     </xsl:template>

     <xsl:template match="/*">
          <xsl:apply-templates select="*"/>
     </xsl:template> 
     </xsl:stylesheet>

感谢所有关注并尝试提供帮助的人。

问候, 大卫

【讨论】:

    猜你喜欢
    • 2014-11-02
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多