我需要使用XSLT从XML生成CSV文本输出
我的XML
<Row>
<cell>Currecy</cell>
<cell>RUR</cell>
<cell>USD</cell>
<cell>EURO</cell>
</Row>
<Row>
<cell>Param</cell>
<cell>17.2</cell>
<cell>12.12</cell>
<cell>100.2345</cell>
</Row>
<Row>
<cell>Param1</cell>
<cell>100</cell>
<cell>200</cell>
<cell>3556</cell>
</Row>
输出格式CSV
Cur Param, Param1
RUR, 17.2, 100
USD, 12.12, 200
EURO, 100.2345, 3556
我不知道如何制作“换位”
答案 0 :(得分:3)
如果您有有效 XML输入,例如:
<Rows>
<Row>
<cell>Currency</cell>
<cell>RUR</cell>
<cell>USD</cell>
<cell>EURO</cell>
</Row>
<Row>
<cell>Param</cell>
<cell>17.2</cell>
<cell>12.12</cell>
<cell>100.2345</cell>
</Row>
<Row>
<cell>Param1</cell>
<cell>100</cell>
<cell>200</cell>
<cell>3556</cell>
</Row>
</Rows>
然后你可以使用以下样式表:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/">
<xsl:for-each select="Rows/Row[1]/cell">
<xsl:variable name="i" select="position()" />
<xsl:for-each select="/Rows/Row">
<xsl:value-of select="cell[$i]"/>
<xsl:if test="position()!=last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:if test="position()!=last()">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
获得:
Currency,Param,Param1
RUR,17.2,100
USD,12.12,200
EURO,100.2345,3556