XSLT转换嵌套的元素用逗号分隔 [英] XSLT to convert the nested elements with comma separated
问题描述
我试图将任何xml文件转换为CSV格式。当xml具有嵌套/子元素时,这些值不是作为逗号分隔输出的问题。我已经尝试了很多例子在其他职位,但没有一个根据需要工作。我是XSLT的初学者,对此非常有帮助。
< 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> $ ok $。
<我已经尝试修改脚本,但它产生输出
1,2,3,4,5,6,7, 8,(我需要摆脱最后一个逗号,看起来保持这个逗号加上每一行)
10,20,30,40,50,60,70,80
请帮忙。我需要摆脱每个行的最后一个值后的结束逗号。注意:逗号仅在最后一行删除。
< xsl :template match =// S:Body>
< xsl:apply-templates select =// trade/>
< / xsl:template>
< xsl:template match =trade>
< xsl:application-templates />
< xsl:text>&#10;< / 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>
尝试以下方法:
<?xml version =1.0encoding =UTF-8? ;
< xsl:stylesheet version =1.0xmlns:xsl =http://www.w3.org/1999/XSL/Transform
xmlns:S =http:// schemas。 xmlsoap.org/soap/envelope/\">
< xsl:output method =textindent =no/>
< xsl:strip-space elements =*/>
< xsl:template match =/>
< xsl:call-template name =getProduct>
< xsl:with-param name =Productselect =// 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 =Childselect =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 =Childselect =*/>
< / 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 =Childselect =*/>
< / 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>
输出:
code> 1,2,3,4,5,6,7,8
10,20,30,40,50,60,70,80
您也可以在将SOAP响应转换为CSV
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>
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
这篇关于XSLT转换嵌套的元素用逗号分隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!