More efficient approach to XSLT for-each
- by Paul
I have an XSLT which takes a . delimted string and splits it into two fields for a SQL statement:
<xsl:for-each select="tokenize(Path,'\.')">
<xsl:choose>
<xsl:when test="position() = 1 and position() = last()">SITE = '<xsl:value-of select="."/>' AND PATH = ''</xsl:when>
<xsl:when test="position() = 1 and position() != last()">SITE = '<xsl:value-of select="."/>' </xsl:when>
<xsl:when test="position() = 2 and position() = last()">AND PATH = '<xsl:value-of select="."/>' </xsl:when>
<xsl:when test="position() = 2">AND PATH = '<xsl:value-of select="."/></xsl:when>
<xsl:when test="position() > 2 and position() != last()">.<xsl:value-of select="."/></xsl:when>
<xsl:when test="position() > 2 and position() = last()">.<xsl:value-of select="."/>' </xsl:when>
<xsl:otherwise>zxyarglfaux</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
The results are as follows:
INPUT: North OUTPUT: SITE = 'North' AND PATH = ''
INPUT: North.A OUTPUT: SITE = 'North' AND PATH = 'A'
INPUT: North.A.B OUTPUT: SITE = 'North' AND PATH = 'A.B'
INPUT: North.A.B.C OUTPUT: SITE = 'North' AND PATH = 'A.B.C'
This works, but is very lengthy. Can anyone see a more efficient approach?
Thanks!