Reputation: 11
My XML includes an element like this...
<root>
<data>
<data_item>
<colours>White, Red, Light Blue</colours>
</data_item>
</data>
</root>
I need to transform this into...
<Items>
<item>
<custom_options>name=Options,type=multiple,required=0,price=0.0000,price_type=fixed,sku=,option_title=White|name=Options,type=multiple,required=0,price=0.0000,price_type=fixed,sku=,option_title=Red|name=Options,type=multiple,required=0,price=0.0000,price_type=fixed,sku=,option_title=Light Blue</custom_options>
</item>
</Items>
Upvotes: 1
Views: 3157
Reputation: 1193
Create a recursive template to split a string, based on the separator:
<xsl:template name="splitter">
<xsl:param name="remaining-string"/>
<xsl:param name="pattern"/>
<xsl:choose>
<xsl:when test="contains($remaining-string,$pattern)">
<split-item>
<xsl:value-of select = "normalize-space(substring-before($remaining-string,$pattern))"/>
</split-item>
<xsl:call-template name="splitter">
<xsl:with-param name="remaining-string" select="substring-after($remaining-string,$pattern)"/>
<xsl:with-param name="pattern" select="$pattern"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<split-item>
<!-- normalize-space reduces a sequence of spaces to at most one space. Do your own stuff to format individual split item -->
<xsl:value-of select = "normalize-space($remaining-string)"/>
</split-item>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Call this template on the node which contains a sequence of items separated by a pattern:
<xsl:template match="/">
<xsl:variable name = "colors">
<xsl:for-each select="root/data/data_item">
<xsl:call-template name="splitter">
<xsl:with-param name="remaining-string" select="colours"/>
<xsl:with-param name="pattern" select="','"/>
</xsl:call-template>
</xsl:for-each>
</xsl:variable>
<!--Do your thing with the variable colors. I simply send it to the output stream. If you further want to iterate over the split items, make sure you convert the variable to a node-set-->
<xsl:copy-of select = "$colors"/>
</xsl:template>
See it in action here: http://xsltransform.net/ei5Pwj6
Upvotes: 4