0

我正在尝试从以下形式的 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>

我想在 /Body 标记处的 csv 文件中添加一个换行符,因为这表示一条新消息。消息会混合在一起,因此在 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 中的最后一条信息之后添加了一个逗号。我想用换行符替换那个逗号,有没有简单的方法可以做到这一点?

问候,大卫

4

2 回答 2

0

一种方法是更改​​与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>
于 2012-11-27T11:23:37.267 回答
0

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

    <?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>

感谢所有看过并试图提供帮助的人。

问候,大卫

于 2012-11-29T14:36:30.573 回答