Reputation: 17873
Source:
<Data>
<value>M1,M2,M3,M4,M5,M6</value>
</Data>
Need to Display them as
Output:
<ABCD>
<value1>M1</value1>
<value2>M2</value2>
<value3>M3</value3>
<value4>M4</value4>
<value5>M5</value5>
<value6>M6</value6>
</ABCD>
XSLT:
I actually want to split the value based on "," and place them in different variables. Using str-split(), Can I load it in different variables.
Upvotes: 2
Views: 1291
Reputation: 243459
This XSLT 1.0 transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<ABCD>
<xsl:apply-templates/>
</ABCD>
</xsl:template>
<xsl:template match="value/text()" name="split">
<xsl:param name="pText" select="."/>
<xsl:param name="pOrd" select="1"/>
<xsl:if test="$pText">
<xsl:element name="value{$pOrd}">
<xsl:value-of select=
"substring-before(concat($pText, ','), ',')"/>
</xsl:element>
<xsl:call-template name="split">
<xsl:with-param name="pText" select="substring-after($pText, ',')"/>
<xsl:with-param name="pOrd" select="$pOrd+1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<Data>
<value>M1,M2,M3,M4,M5,M6</value>
</Data>
produces the wanted, correct result:
<ABCD>
<value1>M1</value1>
<value2>M2</value2>
<value3>M3</value3>
<value4>M4</value4>
<value5>M5</value5>
<value6>M6</value6>
</ABCD>
Explanation:
Recursive named template, with stop-condition when the passed text-parameter becomes the empty string.
Proper use of xsl:element
and AVT.
Proper use of the standard XPath functions substring-before()
and substring-after
Proper use of a sentinel to simplify the code and make it more efficient.
Upvotes: 3
Reputation: 34556
If you have access to EXSLT you can use str:split()
.
<xsl:apply-templates select='str:split(/Data/value, ",")' />
Upvotes: 0