More efficient approach to XSLT for-each

Posted by Paul on Stack Overflow See other posts from Stack Overflow or by Paul
Published on 2012-09-12T21:36:39Z Indexed on 2012/09/12 21:37 UTC
Read the original article Hit count: 296

Filed under:
|
|

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!

© Stack Overflow or respective owner

Related posts about Xml

Related posts about xslt