XML到CSV问题

XML到CSV问题,xml,xslt,Xml,Xslt,我正在尝试将xml转换为csv。我面临的问题是如何将子节点(如ProdIDT、IDV)作为一行tab delimeted txt文件。子节点值即将接近,但没有标头。请看下面我的输入和XSL文件 XML 我的XSL 所需输出 谢谢 Byomokesh类似这样的东西: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="te

我正在尝试将xml转换为csv。我面临的问题是如何将子节点(如ProdIDT、IDV)作为一行tab delimeted txt文件。子节点值即将接近,但没有标头。请看下面我的输入和XSL文件

XML 我的XSL

所需输出 谢谢 Byomokesh

类似这样的东西:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:for-each select="//Product[position()=1]">
      <xsl:for-each select=".//*">
        <xsl:value-of select="name()" />
        <xsl:call-template name="WriteTab" />
      </xsl:for-each>
    </xsl:for-each>
    <xsl:call-template name="WriteNewLine" />

    <xsl:for-each select="//Product">
      <xsl:for-each select=".//*">
        <xsl:value-of select="text()" />
        <xsl:call-template name="WriteTab" />
      </xsl:for-each>
      <xsl:call-template name="WriteNewLine" />
    </xsl:for-each>
  </xsl:template>

  <xsl:template name ="WriteNewLine">
    <xsl:text>
</xsl:text>
  </xsl:template>

  <xsl:template name="WriteTab">
    <xsl:text>&#9;</xsl:text>
  </xsl:template>

基本上,您需要两个循环。每个产品都有一个循环,在该循环中,产品的所有属性都应该有另一个循环。在每个产品之后,写出一个换行符。 大概是这样的:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:for-each select="//Product[position()=1]">
      <xsl:for-each select=".//*">
        <xsl:value-of select="name()" />
        <xsl:call-template name="WriteTab" />
      </xsl:for-each>
    </xsl:for-each>
    <xsl:call-template name="WriteNewLine" />

    <xsl:for-each select="//Product">
      <xsl:for-each select=".//*">
        <xsl:value-of select="text()" />
        <xsl:call-template name="WriteTab" />
      </xsl:for-each>
      <xsl:call-template name="WriteNewLine" />
    </xsl:for-each>
  </xsl:template>

  <xsl:template name ="WriteNewLine">
    <xsl:text>
</xsl:text>
  </xsl:template>

  <xsl:template name="WriteTab">
    <xsl:text>&#9;</xsl:text>
  </xsl:template>
此样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:template match="/*">
        <xsl:apply-templates select="*[1]" mode="header"/>
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="*[not(*)]" mode="header">
        <xsl:value-of
         select="concat(name(),
                        substring('&#x9;&#xA;',
                                  1+boolean(following::*[1][self::Product]
                                            or not(following::*[1])),
                                  1))"/>
    </xsl:template>
    <xsl:template match="*[not(*)]">
        <xsl:value-of
         select="concat(.,
                        substring('&#x9;&#xA;',
                                  1+boolean(following::*[1][self::Product]
                                            or not(following::*[1])),
                                  1))"/>
    </xsl:template>
</xsl:stylesheet>

如果有两条信息,它将上线。并不是每个标签都有“回车”标记。@Byomokesh:我不理解你的任何评论。请澄清。在这种情况下,两个产品文本都在一行中。喜欢记录非产品ID产品IDV 1616200243 03 02 1616200243记录非产品ID产品IDV 4614200243 02 02 1616200243
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

<xsl:template match="/">
  <xsl:for-each select="/*//*">
   <xsl:value-of select="concat(name(), '&#9;')"/>
  </xsl:for-each>
  <xsl:text>&#xA;</xsl:text>
  <xsl:for-each select="/*//*">
    <xsl:value-of select="concat(text(), '&#9;')"/>
  </xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:for-each select="//Product[position()=1]">
      <xsl:for-each select=".//*">
        <xsl:value-of select="name()" />
        <xsl:call-template name="WriteTab" />
      </xsl:for-each>
    </xsl:for-each>
    <xsl:call-template name="WriteNewLine" />

    <xsl:for-each select="//Product">
      <xsl:for-each select=".//*">
        <xsl:value-of select="text()" />
        <xsl:call-template name="WriteTab" />
      </xsl:for-each>
      <xsl:call-template name="WriteNewLine" />
    </xsl:for-each>
  </xsl:template>

  <xsl:template name ="WriteNewLine">
    <xsl:text>
</xsl:text>
  </xsl:template>

  <xsl:template name="WriteTab">
    <xsl:text>&#9;</xsl:text>
  </xsl:template>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:template match="/*">
        <xsl:apply-templates select="*[1]" mode="header"/>
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="*[not(*)]" mode="header">
        <xsl:value-of
         select="concat(name(),
                        substring('&#x9;&#xA;',
                                  1+boolean(following::*[1][self::Product]
                                            or not(following::*[1])),
                                  1))"/>
    </xsl:template>
    <xsl:template match="*[not(*)]">
        <xsl:value-of
         select="concat(.,
                        substring('&#x9;&#xA;',
                                  1+boolean(following::*[1][self::Product]
                                            or not(following::*[1])),
                                  1))"/>
    </xsl:template>
</xsl:stylesheet>
<root>
    <Product>
        <Record>1616200243</Record>
        <Not>03</Not>
        <ProductId>
            <ProdIDT>02</ProdIDT>
            <IDV>1616200243</IDV>
        </ProductId>
        <ProductId>
            <ProdIDT>03</ProdIDT>
            <IDV>9781616200244</IDV>
        </ProductId>
        <ProdFormDe>Electronic book text</ProdFormDe>
        <EpTy>000</EpTy>
        <NoS/>
        <Title>
            <TitleT>01</TitleT>
            <TTx>The Sound of a Wild Snail Eating</TTx>
            <Sbt>A Memoir</Sbt>
        </Title>
    </Product>
</root>
Record  Not ProdIDT IDV ProdIDT IDV ProdFormDe  EpTy    NoS TitleT  TTx Sbt
1616200243  03  02  1616200243  03  9781616200244   Electronic book text    000     01  The Sound of a Wild Snail Eating    A Memoir