问题
I am trying to just convert any xml file to CSV format. the issue as when the xml has nested / child elements those values are not output as comma separated. i have tried lot of examples in other posts but none of them works out as needed. I am a beginner to XSLT any help to this very helpful.
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<trade>
<createdBy>1</createdBy>
<createdOnDate>2</createdOnDate>
<Payment>
<indicator>3</indicator>
<updateBy>4</updateBy>
</Payment>
<Parties>
<partyId>5</partyId>
<partyRoleTypeCode>6</partyRoleTypeCode>
</Parties>
<Product>
<term>7</term>
<shortDescription>8</shortDescription>
</Product>
</trade>
<trade>
<createdBy>10</createdBy>
<createdOnDate>20</createdOnDate>
<Payment>
<indicator>30</indicator>
<updateBy>40</updateBy>
</Payment>
<Parties>
<partyId>50</partyId>
<partyRoleTypeCode>60</partyRoleTypeCode>
</Parties>
<Product>
<term>70</term>
<shortDescription>80</shortDescription>
</Product>
</trade>
</S:Body>
</S:Envelope>
thanks OkieOth. I have tried the modified script but it produce the output as
1,2,3,4,5,6,7,8, ( i need to get rid of the last comma , looks keep this comma adding every row)
10,20,30,40,50,60,70,80
Please help. i need to get rid of the ending comma after last value of the every line. note:the comma get removed only for the last row.
<xsl:template match="//S:Body">
<xsl:apply-templates select="//trade"/>
</xsl:template>
<xsl:template match="trade">
<xsl:apply-templates/>
<xsl:text> </xsl:text>
</xsl:template>
<xsl:template match="//text()">
<xsl:copy-of select="." />
<xsl:choose>
<xsl:when test="(following::*)">
<xsl:value-of select="','"/>
</xsl:when>
</xsl:choose>
</xsl:template>
回答1:
Try this one:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<xsl:output method="text" indent="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:call-template name="getProduct">
<xsl:with-param name="Product" select="//S:Body"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="getProduct">
<xsl:param name="Product"/>
<xsl:for-each select="$Product/*">
<xsl:variable name="ProductContent">
<xsl:choose>
<xsl:when test="*">
<xsl:call-template name="getChild">
<xsl:with-param name="Child" select="self::*"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:if test="@*">
<xsl:for-each select="@*">
<xsl:value-of select="."/>
<xsl:text>,</xsl:text>
</xsl:for-each>
<xsl:text>,</xsl:text>
</xsl:if>
<xsl:value-of select="."/>
<xsl:text>,</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="substring($ProductContent,0,string-length($ProductContent))"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
<xsl:template name="getChild">
<xsl:param name="Child"/>
<xsl:for-each select="$Child/node()">
<xsl:choose>
<xsl:when test="* and not(@*)">
<xsl:call-template name="getChild">
<xsl:with-param name="Child" select="*"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="text()">
<xsl:value-of select="."/>
<xsl:text>,</xsl:text>
</xsl:when>
<xsl:when test="*">
<xsl:for-each select="@*">
<xsl:value-of select="."/>
<xsl:text>,</xsl:text>
</xsl:for-each>
<xsl:call-template name="getChild">
<xsl:with-param name="Child" select="*"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="not(*) and @*">
<xsl:for-each select="@*">
<xsl:value-of select="."/>
<xsl:text>,</xsl:text>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:if test="@*">
<xsl:for-each select="@*">
<xsl:value-of select="."/>
<xsl:text>,</xsl:text>
</xsl:for-each>
<xsl:text>,</xsl:text>
</xsl:if>
<xsl:value-of select="."/>
<xsl:text>,</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:if test="$Child/@* and not($Child/node())">
<xsl:for-each select="$Child/@*">
<xsl:value-of select="."/>
<xsl:text>,</xsl:text>
</xsl:for-each>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
output:
1,2,3,4,5,6,7,8
10,20,30,40,50,60,70,80
You may also check my previous post at SOAP Response converting to CSV
来源:https://stackoverflow.com/questions/16524580/xslt-to-convert-the-nested-elements-with-comma-separated