Reputation: 1
I have a string one of the nodes in my XML that is going to return a delimited list of values. Something similar to this string:
<Item>Code^ReturnCode|Description^ReturnDescription|System^ReturnSystem</Item>
What I am looking for is an output of
<Item>
<Code>ReturnCode</Code>
<Description>ReturnDescription</Description>
<System>ReturnSystem</System>
<Item>
The element names and values are completely dynamic. I know I am getting close to the idea, but this is my first XSLT. Here is what I have so far:
<xsl:template match="/Container/Information" >
<xsl:call-template name="splitReturn">
<xsl:with-param name="String" select="text()"/>
<xsl:with-param name="Separator" select="'|'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="splitReturn">
<xsl:param name="String"/>
<xsl:param name="Separator"/>
<xsl:element name="{substring-before($String,$Separator)}">
<xsl:value-of select="substring-after($String,$Separator)" />
</xsl:element>
</xsl:template>
I think I am just missing how to nest the templates that I am applying.
Upvotes: 0
Views: 148
Reputation: 116993
You must make your template recursive. Try:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/Item">
<xsl:copy>
<xsl:call-template name="splitReturn"/>
</xsl:copy>
</xsl:template>
<xsl:template name="splitReturn">
<xsl:param name="string" select="."/>
<xsl:variable name="token" select="substring-before(concat($string, '|'), '|')" />
<xsl:if test="$token">
<xsl:element name="{substring-before($token, '^')}">
<xsl:value-of select="substring-after($token, '^')" />
</xsl:element>
</xsl:if>
<xsl:if test="contains($string, '|')">
<!-- recursive call -->
<xsl:call-template name="splitReturn">
<xsl:with-param name="string" select="substring-after($string, '|')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
When this is applied to your input example:
<Item>Code^ReturnCode|Description^ReturnDescription|System^ReturnSystem</Item>
the result will be:
<?xml version="1.0" encoding="utf-8"?>
<Item>
<Code>ReturnCode</Code>
<Description>ReturnDescription</Description>
<System>ReturnSystem</System>
</Item>
Note that this will work only if the provided names are valid XML element names.
Upvotes: 1