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!
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 ...
I see 4 things wrong:
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 thexml-stylesheet
processing instruction, you're probably not using a 2.0 processor.The first argument of your tokenize (
[text]
) is invalid. Just usetext
.You've prematurely closed your
xsl:for-each
.You're outputting an
<items>
for each item. Put the<items>
outside thexsl:for-each
.Example of changes:
To truly get your desired output with a 2.0 processor, I'd also suggest using
xsl:output
andnormalize-space()
: