如何使 xsl 标记化工作 [英] how to make xsl tokenize work
问题描述
我有一个巨大的 xsl 文件,但是我使用tokenize"来解析逗号分隔字符串的部分抛出错误.为简单起见,我将其分解为仅测试标记化部分,似乎无法取得任何进展.我不断收到以下错误:
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:
预期的表达.tokenize(-->[<--text],',')
Expression expected. tokenize(-->[<--text],',')
我尝试使用在其他帖子中共享的一些示例 xsl,但从未设法让它工作.我很难理解为什么我下面的 xsl 代码无效.这似乎不是很简单,但我想我错过了一些简单的东西.任何让我朝着正确方向前进的帮助将不胜感激.
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>
我期待如下的 XML 输出:
I am expecting an XML output as follows:
<items>
<item>Item1</item>
<item>Item2</item>
<item>Item3</item>
</items>
谢谢!
推荐答案
正如 DevNull 所述,tokenize() 是一个 XSLT 2.0 函数.但是,如果您的处理器支持 EXSLT,则可以使用 str:tokenize() 函数.否则你将需要用户递归来分割你的逗号分隔值,就像这样......
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>
这篇关于如何使 xsl 标记化工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!