【问题标题】:XSLT Iterate convert to CSVXSLT 迭代转换为 CSV
【发布时间】:2015-10-07 09:45:18
【问题描述】:

谁能帮我把下面的xml转换成csv(下面的格式)。

XML>

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Header>
      <Shop>Walmart</Shop>
  </soapenv:Header>
  <soapenv:Body>
    <ResponseEnvelope>
      <Response>
        <Part_electronics>
          <type>electronics</type>
          <item>Freeze</item>
        </Part_electronics>
        <Part_utility>
          <type>utility</type>
          <item>Parker</item>
        </Part_utility>
        <Part_grocery>
          <type>grocery</type>
          <item>sugar</item>
        </Part_grocery>
      </Response>
    </ResponseEnvelope>
  </soapenv:Body>
</soapenv:Envelope>

强文本 CSV 输出:

Walmart,electronics,Freeze
Walmart,utility,pen
Walmart,grocery,sugar

提前致谢。

【问题讨论】:

  • SO 不是编码服务。请向我们展示您到目前为止所做的尝试。
  • w3.org/1999/XSL/Transform">
  • 但是上面的工作不能正常工作,并且它在单独的行中打印每一列的值。需要一些指导。
  • 您能否编辑您的问题以包含上述 XSLT。在 cmets 中很难阅读 XSLT。谢谢。

标签: xml csv xslt xpath


【解决方案1】:

看起来你想要为每个“part_”元素设置一行。在这种情况下,您应该从选择这些元素开始

<xsl:for-each select="//Response/*[starts-with(local-name(), 'Part_')]">

然后您可以通过选择所有子项来获取字段

     <xsl:for-each select="*">
         <xsl:text>,</xsl:text>
         <xsl:value-of select="text()" />
     </xsl:for-each>

这假设所有“Part_”元素具有相同数量的相同顺序的元素。

试试这个 XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="text" indent="yes"/> 
   <xsl:template match="/">
      <xsl:variable name="Shop" select="//Shop" />
      <xsl:for-each select="//Response/*[starts-with(local-name(), 'Part_')]"> 
         <xsl:value-of select="$Shop"/> 
         <xsl:for-each select="*">
             <xsl:text>,</xsl:text>
             <xsl:value-of select="text()" />
         </xsl:for-each>
         <xsl:text>&#xA;</xsl:text>
      </xsl:for-each> 
   </xsl:template> 
</xsl:stylesheet>

这会输出以下内容:

Walmart,electronics,Freeze
Walmart,utility,Parker
Walmart,grocery,sugar

【讨论】:

    猜你喜欢
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 2013-02-20
    • 2017-05-23
    • 2011-10-28
    相关资源
    最近更新 更多