Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make xsl tokenize work

Tags:

xml

xslt

xslt-1.0

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!

like image 459
user858697 Avatar asked Jul 14 '12 22:07

user858697


People also ask

How do you tokenize a string in XSLT?

A tokenizer splits up a string based on a regular expression and returns a node-set of token individual elements and loop over it by then changes the profile of the document. <xsl:with-param name="entitycharac" select="stringval" />? The tokenize function takes two parameters.

How do I replace in XSLT?

XSLT replace is deterministic and does string manipulation that replaces a sequence of characters defined inside a string that matches an expression. In simple terms, it does string substitution in the specified place by replacing any substrings. Fn: replace function is not available in XSLT1.

What is substring after in XSLT?

substring-after() Function — Returns the substring of the first argument after the first occurrence of the second argument in the first argument. If the second argument does not occur in the first argument, the substring-after() function returns an empty string.


1 Answers

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>
like image 148
Sean B. Durkin Avatar answered Oct 05 '22 08:10

Sean B. Durkin