xml 如何使 xsl 标记化工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11487704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to make xsl tokenize work
提问by user858697
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:
我有一个巨大的 xsl 文件,但是我使用“tokenize”来解析逗号分隔字符串的部分抛出错误。为简单起见,我将其分解为仅测试标记化部分,似乎无法取得任何进展。我不断收到以下错误:
Expression expected. tokenize(-->[<--text],',')
表达预期。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 代码无效。这似乎不是很简单,但我想我错过了一些简单的东西。任何让我朝着正确方向前进的帮助将不胜感激。
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:
<?xml-stylesheet type="text/xsl" href="simple.xsl"?>
<root>
<text>Item1, Item2, Item3</text>
</root>
I am expecting an XML output as follows:
我期待一个 XML 输出如下:
<items>
<item>Item1</item>
<item>Item2</item>
<item>Item3</item>
</items>
Thank you!
谢谢!
采纳答案by Sean B. Durkin
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 ...
正如 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>
回答by Daniel Haley
I see 4 things wrong:
我认为有 4 件事是错误的:
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 thexml-stylesheetprocessing instruction, you're probably not using a 2.0 processor.The first argument of your tokenize (
[text]) is invalid. Just usetext.You've prematurely closed your
xsl:for-each.You're outputting an
<items>for each item. Put the<items>outside thexsl:for-each.
您
tokenize()在 1.0 样式表中使用。您需要将版本更改为 2.0 并使用 2.0 处理器。如果您使用 Web 浏览器进行转换,根据xml-stylesheet处理指令,您可能没有使用 2.0 处理器。tokenize (
[text])的第一个参数无效。只需使用text.你过早地关闭了你的
xsl:for-each.您正在
<items>为每个项目输出一个。把<items>外面的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:outputand normalize-space():
要真正使用 2.0 处理器获得所需的输出,我还建议使用xsl:outputand 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>

