Rob Ser
Rob Ser

Reputation: 55

EXSL - How to use str:tokenize()?

I'm just starting XSLT and I try to use the str:tokenize() template in XSLT 1.0. I checked on: http://www.exslt.org/str/functions/tokenize/index.html

But I can't get the expected result.

This is the code:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                                xmlns:exsl="http://exslt.org/common"
                                xmlns:str="http://exslt.org/strings"
                                exclude-result-prefixes="str">
<xsl:output method="xml"/>
<xsl:template match="/">
  <xsl:variable name="var" select="John.Wayne"/>
    <root>
       <xsl:for-each select="str:tokenize($var,'.')">
            <element>
                <xsl:value-of select="."/>
            </element>
        </xsl:for-each>
    </root>
</xsl:template>
</xsl:stylesheet>

My expected output should be:

  <root>
     <element>John</element>
     <element>Wayne</element>
    </root>

Any help appreciated. Thanks in advance! Oh, by the way, my output is:

<?xml version="1.0"?>
<root/>

(I'm using xsltproc)

Upvotes: 1

Views: 241

Answers (2)

Aldo
Aldo

Reputation: 559

The line

<xsl:variable name="var" select="John.Wayne"/>

is assigning to var the result of the evaluation of the XPath John.Wayne.

To assign to var the string value John.Wayne you have to surround it with single quotes:

<xsl:variable name="var" select="'John.Wayne'"/>

Upvotes: 1

Tim C
Tim C

Reputation: 70598

The problem is not with tokenize, but with how you set the variable

 <xsl:variable name="var" select="John.Wayne"/>

This is looking for an element named John.Wayne. I guess you really want to use a string literal here...

Try this!

<xsl:variable name="var" select="'John.Wayne'"/>

Upvotes: 2

Related Questions