One Developer
One Developer

Reputation: 576

XSL Sample Coding

I am not a XSL expert and struggling with the simple logic:

I have a XSL variable with the value of "A,B,C", want to split it and store the individual values into three different XSL variable like

X= A
Y= B
Z= C

but sometimes either it may have only one/two value or no values...

if it has only one value then the variable values should be like

X= A
Y=
Z=

if it does not have any value then

X=
Y=
Z=

Kindly help me with the XSL code for the same

Lets say:

Tags has the value of "Test,Demo,Sample" then i would like to split like this

<xsl:choose>
    <xsl:when test="contains($Tags,',')">
        <xsl:variable name="Tags1">
            <xsl:value-of select="substring-before($Tags,',')" />
        </xsl:variable>
        <xsl:variable name="ATag1">
            <xsl:value-of select="substring-after($Tags,',')" />
        </xsl:variable>
    </xsl:when>
    <xsl:otherwise>
        <xsl:variable name="Tags1"/>
        <xsl:variable name="ATags1"/>
    </xsl:otherwise>
</xsl:choose>

<xsl:choose>
    <xsl:when test="contains($ATags1,',')">
        <xsl:variable name="Tags2">
            <xsl:value-of select="substring-before($ATags1,',')" />
        </xsl:variable>
        <xsl:variable name="ATag2">
            <xsl:value-of select="substring-after($ATags1,',')" />
        </xsl:variable>
    </xsl:when>
    <xsl:otherwise>
        <xsl:variable name="Tags2"/>
        <xsl:variable name="ATags2"/>
    </xsl:otherwise>
</xsl:choose>



<xsl:choose>
    <xsl:when test="contains($ATags2,',')">
        <xsl:variable name="Tags3">
            <xsl:value-of select="substring-before($ATags2,',')" />
        </xsl:variable>
    </xsl:when>
    <xsl:otherwise>
        <xsl:variable name="Tags3"/>
    </xsl:otherwise>
</xsl:choose>

however it is not working for me...

Upvotes: 0

Views: 402

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243599

Here is an XSLT 2.0 solution:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="text()">
  <xsl:variable name="vSeq" select="tokenize(.,',')"/>

  <xsl:variable name="X" select="$vSeq[1]"/>
  <xsl:variable name="Y" select="$vSeq[2]"/>
  <xsl:variable name="Z" select="$vSeq[3]"/>

  <xsl:value-of select=
   "concat('X = ',$X, '&#xA;',
           'Y = ',$Y, '&#xA;',
           'Z = ',$Z, '&#xA;'
           )"
   />
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the following XML document:

<t>A,B,C</t>

the wanted, correct result is produced:

X = A
Y = B
Z = C

XSLT 1.0 solution:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="text()">
  <xsl:variable name="X" select="substring-before(.,',')"/>
  <xsl:variable name="Y" select=
   "substring-before(substring-after(.,','),',')"/>
  <xsl:variable name="Z" select=
   "substring-before(substring-after(.,','),',')"/>

  <xsl:value-of select=
   "concat('X = ',$X, '&#xA;',
           'Y = ',$Y, '&#xA;',
           'Z = ',$Z, '&#xA;'
           )"
   />
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the same XML document (as above), the wanted, correct result is produced:

X = A
Y = B
Z = B

Upvotes: 2

Related Questions