如何使XSL tokenize正常工作

13

我有一个非常庞大的xsl文件,但在使用“tokenize”解析逗号分隔字符串的部分时出现错误。为简单起见,我将其简化为仅测试解析器的部分,但似乎无法取得任何进展。我一直收到以下错误:

需要表达式。tokenize(-->[<--text],',')

我尝试使用其他帖子中共享的一些示例xsl,但从未成功使其工作。我很难理解为什么我的xsl代码无效。它似乎非常简单,但我认为我错过了一些简单的东西。希望能提供任何帮助使我朝正确的方向前进,不胜感激。

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>

我期望的XML输出如下:

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

谢谢!


可能是Does XSLT have a Split() function?的重复问题。 - Isaac G Sivaa
2个回答

11

我发现有4个问题:

  1. 你正在使用1.0样式表中的 tokenize()。您需要将版本更改为2.0并使用2.0处理器。如果您正在使用Web浏览器进行转换,基于xml-stylesheet处理指令,您可能没有使用2.0处理器。

  2. 您 tokenize 函数的第一个参数([text])是无效的。只需使用 text

  3. 您过早关闭了 xsl:for-each

  4. 您正在为每个项输出一个 <items>。将 <items> 放在 xsl:for-each 外部。

更改示例:

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

要在2.0处理器上真正获得所需的输出,我还建议使用xsl:outputnormalize-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>

谢谢DevNull。这非常有帮助。我的最后一个问题是......我怎么知道我正在使用什么处理器?有没有一种方法可以在某个地方确认它? - user858697
调用函数 system-property('xsl:vendor') 和 system-property('xsl:version')。它们返回字符串。将它们输出。 - Sean B. Durkin
明白了,谢谢!看起来我的处理器是1.0,我该如何升级呢? - user858697

5
如DevNull所述,tokenize()是一个XSLT 2.0函数。但是,如果您的处理器支持EXSLT,则可以使用str:tokenize()函数。否则,您需要使用递归来拆分逗号分隔的值,如下所示...
<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>

以上解决方案假设列表中没有空项。 - Sean B. Durkin
+1。你刚刚帮我省了一天的时间,特别是因为你给了 Exslt tokenize 函数的链接。 - GuruM

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接