【问题标题】:XSLT (for CSV) for the give XML structure用于给出 XML 结构的 XSLT(用于 CSV)
【发布时间】:2011-05-04 04:43:54
【问题描述】:

我不知道如何为 XML 制作 XSLT 以将其转换为 CSV。请为给定的 XML 结构编写 XSLT 代码,并尝试验证并考虑所有原始逗号和引号等。

<DROPSHIPITEMS>
  <CREATED value="Thu Apr 21 23:17:39 BST 2011">
    <PRODUCT ITEM='8101'>
      <MODEL>FY316A</MODEL>
      <EAN>5055071647109</EAN>
      <NAME>Enchanted Twilight Flower Fairy 'Amethyst'</NAME>
      <DESCRIPTION><![CDATA[<p> Twilight Fairy 'Amethyst'</p><p>This, Fairy.</p>]]></DESCRIPTION>
      <DIMENSION><![CDATA[Height 31 - 32cm Width, 16 - 18.5cm Depth 12 - 13.5cm]]></DIMENSION>
      <PRICE>16.63</PRICE>
      <DELIVERY>I</DELIVERY>
      <QUANTITY>224</QUANTITY>
      <MIN_ORDER_QTY>1</MIN_ORDER_QTY>
      <URL>http://www.abc-dropship.co.uk/gifts/product_info.php?products_id=8101</URL>
      <IMAGE_URL>http://www.abc-dropship.co.uk/gifts/images/FY316A_001.jpg</IMAGE_URL>
      <CATEGORIES>9003|100297</CATEGORIES>
      <OPTIONS><![CDATA[B - Hand on Dress|A - Flower in Hand|Any]]></OPTIONS>
    </PRODUCT>
</CREATED>
</DROPSHIPITEMS>

谢谢

【问题讨论】:

  • CSV 只是一种格式:应该将哪个 XML 元素放入 CSV?你绑东西了吗?
  • 我已经回答了一个与此类似的问题:stackoverflow.com/questions/3056579/… 看看那个链接,它应该会给你足够的帮助,让你自己能够做到这一点。

标签: xml xslt csv


【解决方案1】:

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<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="*/*/*[1]/*">
      <xsl:value-of select="name()" />
      <xsl:if test="not(position() = last())">,</xsl:if>
    </xsl:for-each>
    <xsl:text>&#10;</xsl:text>
    <xsl:apply-templates select="*/*/*" mode="row"/>
  </xsl:template>

  <xsl:template match="*" mode="row">
    <xsl:apply-templates select="*" mode="data" />
    <xsl:text>&#10;</xsl:text>
  </xsl:template>

  <xsl:template match="*" mode="data">
    <xsl:choose>
      <xsl:when test="contains(text(),',')">
        <xsl:text>&quot;</xsl:text>
        <xsl:call-template name="doublequotes">
          <xsl:with-param name="text" select="text()" />
        </xsl:call-template>
        <xsl:text>&quot;</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="." />
      </xsl:otherwise>
    </xsl:choose>
    <xsl:if test="position() != last()">,</xsl:if>
  </xsl:template>

  <xsl:template name="doublequotes">
    <xsl:param name="text" />
    <xsl:choose>
      <xsl:when test="contains($text,'&quot;')">
        <xsl:value-of select="concat(substring-before($text,'&quot;'),'&quot;&quot;')" />
        <xsl:call-template name="doublequotes">
          <xsl:with-param name="text" select="substring-after($text,'&quot;')" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

它可以正确处理带有逗号的字段,以及带有双引号的字段。

编辑:差点忘了;这个模板添加了一个带有字段名称的标题行。如果你不需要,第一个模板只需要是

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

【讨论】:

    【解决方案2】:

    将 XML 转换为逗号分隔与任何其他转换相同。以下内容按原样复制字段,除非它们在您需要处理的每个产品中的顺序相同。该示例将帮助您入门:

      <xsl:output method="text"/>
    
      <xsl:template match="//PRODUCT">
            <xsl:value-of select="@ITEM" />
        <xsl:text>,</xsl:text>
        <xsl:apply-templates select="./*" />
        <xsl:text>&#x0A;</xsl:text>
      </xsl:template>
    
      <xsl:template match="PRODUCT/*">
            <xsl:value-of select="translate(text(), ',', '@')" />
        <xsl:text>,</xsl:text>
      </xsl:template>
    

    顺便说一句,&amp;#0A; 添加了换行符,对于 dos/windows,您可能还需要添加 &amp;#0D;

    更新:值中的逗号可以通过使用translate() 函数替换为另一个字符来转义。

    【讨论】:

    • 感谢 rsp,XSL 工作正常,但不处理我在描述元素的 DATA 中的原始逗号。它还包括我们在原始文本中的逗号。所以如果我导入,它肯定会生成单独的列那么如何处理这个问题?
    • @Mehboob,查看更新 - @ 可能不是您的替代选择,但很容易发现 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    • 2021-10-28
    • 2012-09-06
    • 1970-01-01
    相关资源
    最近更新 更多