Tuesday, 13 August 2013

xslt to create comma-separated list from node attributes

xslt to create comma-separated list from node attributes

I have the following XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Export channel="XXX" date_stamp="20130113 01:01:01">
<Script show_code="ALR" script_no="13081300" duration="1">
<Product skn="222363" />
<Product skn="203092" />
<Product skn="219585" />
<Product skn="201371" />
<Product skn="201029" />
<Product skn="202648" />
<Product skn="201294" />
<Product skn="201370" />
</Script>
<Script show_code="BQV" script_no="13081301" duration="1">
<Product skn="400063" />
<Product skn="203089" />
<Product skn="212059" />
<Product skn="202770" />
<Product skn="400292" />
<Product skn="400108" />
<Product skn="400407" />
</Script>
</Export>
And I would like to output (in simply text format) the Product @skn values
as a comma separated entity. My current XSLT is..
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" version="1.0" encoding="utf-8" indent="no"
media-type="text/xml" />
<xsl:template match="/">
<xsl:for-each select="/EPGExport/Script">
<xsl:value-of select="substring(@script_no,5,2)"/>/<xsl:value-of
select="substring(@script_no,3,2)"/>/20<xsl:value-of
select="substring(@script_no,1,2)"/><xsl:text>&#9;</xsl:text>
<xsl:value-of
select="substring(@script_no,7,2)"/>:00<xsl:text>&#9;</xsl:text>
<xsl:value-of select="@show_code"/><xsl:text>&#9;</xsl:text>
<xsl:value-of select="Product/@skn"/><xsl:text>&#10;</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Which will output as:
13/08/2013 00:00 ALR 222363
13/08/2013 01:00 BQV 400063
But I would like the last of the tab-separated values to output as:
13/08/2013 00:00 ALR
222363,203092,219585,201371,201029,202648,201294,201370
13/08/2013 01:00 BQV 400063,203089,212059,202770,400292,400108,400407
Any help much appreciated. Thanks.

No comments:

Post a Comment