2011-09-03 105 views
2

使用XSLT转换转换XML到CSV使用XSLT

<start> 
<article> 
<key>key1</key> 

<definition type="m"> 
<![CDATA[abcdefghijk]]> 
</definition> 
</article> 

<article> 
<key>key2</key> 

<definition type="m"> 
<![CDATA[bcdefghijkl]]> 
</definition> 
</article> 
</start> 

CSV看起来像

key1,abcdefghijk 
key2,bcdefghijkl 

心中已经学会W3C学校XSLT教程转换XML转换为CSV,但不能得到实践。 有人可以编写用于转换的XSLT代码吗?

+1

我认为这可以帮助你: http://stackoverflow.com/questions/365312/xml-to-csv - 使用 - XSLT –

回答

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

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

    <xsl:template match="key"> 
    <xsl:copy-of select="normalize-space(concat(., ';'))"/> 
    </xsl:template> 

    <xsl:template match="definition"> 
    <xsl:copy-of select="normalize-space(.)"/> 
    <xsl:text>&#xD;&#xA;</xsl:text> 
    </xsl:template> 

</xsl:stylesheet> 
1

可以实现与一个模板想要的结果:

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

    <xsl:template match="start/article"> 
     <xsl:if test="position()>1"> 
      <xsl:text>&#xA;</xsl:text> 
     </xsl:if> 
     <xsl:value-of select="normalize-space(
      concat(key,'; ',definition))"/> 
    </xsl:template> 

</xsl:stylesheet>