Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
901 views
in Technique[技术] by (71.8m points)

xml - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...