user858697
user858697

Reputation: 133

how to make xsl tokenize work

I have a huge xsl file but the section where i use "tokenize" to parse through a comma separated string is throwing an error. For simplicity purposes I have broke it down to just test the tokenize piece only and cannot seem to make any progress. I keep getting the following error:

Expression expected. tokenize(-->[<--text],',')

I tried using some example xsl shared in other posts but never managed to get it to work. I am having a difficult time understanding why my xsl code below is not valid. It seems t be very straightforward but I think I am missing something simple. Any help to get me in the right direction would be much appreciated.

XSL:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/root">
<xsl:for-each select="tokenize([text],',')"/>
<items>
<item>
<xsl:value-of select="."/>
</item>
</items>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

XML:

<?xml-stylesheet type="text/xsl" href="simple.xsl"?>
<root>
<text>Item1, Item2, Item3</text>
</root>

I am expecting an XML output as follows:

<items>
<item>Item1</item>
<item>Item2</item>
<item>Item3</item>
</items>

Thank you!

Upvotes: 13

Views: 52971

Answers (2)

Sean B. Durkin
Sean B. Durkin

Reputation: 12729

As stated by DevNull, tokenize() is an XSLT 2.0 function. However, if your processor supports EXSLT, use can use the str:tokenize() function. Otherwise you will need to user recursion to split your comma separated values like thus ...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />

<xsl:template match="/">
 <items>
   <xsl:apply-templates select="root/text"/>
 </items>
</xsl:template>

<xsl:template match="text">
 <xsl:call-template name="tokenize"> 
   <xsl:with-param name="csv" select="." /> 
 </xsl:call-template>    
</xsl:template>

<xsl:template name="tokenize">
 <xsl:param name="csv" />
  <xsl:variable name="first-item" select="normalize-space( 
    substring-before( concat( $csv, ','), ','))" /> 
 <xsl:if test="$first-item">
  <item>
   <xsl:value-of select="$first-item" /> 
  </item>  
  <xsl:call-template name="tokenize"> 
   <xsl:with-param name="csv" select="substring-after($csv,',')" /> 
  </xsl:call-template>    
 </xsl:if>  
</xsl:template>

</xsl:stylesheet>

Upvotes: 5

Daniel Haley
Daniel Haley

Reputation: 52848

I see 4 things wrong:

  1. You're using tokenize() in a 1.0 stylesheet. You need to change the version to 2.0 and use a 2.0 processor. If you're using a web browser to do the transform, based on the xml-stylesheet processing instruction, you're probably not using a 2.0 processor.

  2. The first argument of your tokenize ([text]) is invalid. Just use text.

  3. You've prematurely closed your xsl:for-each.

  4. You're outputting an <items> for each item. Put the <items> outside the xsl:for-each.

Example of changes:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/root">
        <items>
            <xsl:for-each select="tokenize(text,',')">
                <item>
                    <xsl:value-of select="."/>
                </item>
            </xsl:for-each>
        </items>
    </xsl:template>
</xsl:stylesheet>

To truly get your desired output with a 2.0 processor, I'd also suggest using xsl:output and normalize-space():

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>

    <xsl:template match="/root">
        <items>
            <xsl:for-each select="tokenize(text,',')">
                <item>
                    <xsl:value-of select="normalize-space(.)"/>
                </item>
            </xsl:for-each>
        </items>
    </xsl:template>

</xsl:stylesheet>

Upvotes: 11

Related Questions