string xslt 有 split() 功能吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3336424/
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
Does xslt have split() function?
提问by Ketan
How do you split a string based on some separator?
如何根据某个分隔符拆分字符串?
Given a string Topic1,Topic2,Topic3
, I want to split the string based on ,
to generate:
给定一个字符串Topic1,Topic2,Topic3
,我想根据,
生成的字符串拆分字符串:
Topic1 Topic2 Topic3
回答by
In XSLT 1.0 you have to built a recursive template. This stylesheet:
在 XSLT 1.0 中,您必须构建一个递归模板。这个样式表:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text/text()" name="tokenize">
<xsl:param name="text" select="."/>
<xsl:param name="separator" select="','"/>
<xsl:choose>
<xsl:when test="not(contains($text, $separator))">
<item>
<xsl:value-of select="normalize-space($text)"/>
</item>
</xsl:when>
<xsl:otherwise>
<item>
<xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
</item>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $separator)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Input:
输入:
<root>
<text>Item1, Item2, Item3</text>
</root>
Output:
输出:
<root>
<text>
<item>Item1</item>
<item>Item2</item>
<item>Item3</item>
</text>
</root>
In XSLT 2.0 you have the tokenize()
core function. So, this stylesheet:
在 XSLT 2.0 中,您拥有tokenize()
核心功能。所以,这个样式表:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text/text()" name="tokenize">
<xsl:param name="separator" select="','"/>
<xsl:for-each select="tokenize(.,$separator)">
<item>
<xsl:value-of select="normalize-space(.)"/>
</item>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Result:
结果:
<root>
<text>
<item>Item1</item>
<item>Item2</item>
<item>Item3</item>
</text>
</root>
回答by Max Toro
Use fn:tokenize
回答by Oded
回答by Kamal Soni
XSLT 1.0
I needed a slight variant compared to other answers given here.
XSLT 1.0
与此处给出的其他答案相比,我需要一个轻微的变体。
Input:
输入:
1, 2, 3
1、2、3
Ouput:
输出:
1, 2 and 3
1、2 和 3
Input:
输入:
1
1
Output
输出
1
1
If the delimiter is space instead of comma, it would still work.
如果分隔符是空格而不是逗号,它仍然可以工作。
Input:
输入:
1 2 3
1 2 3
Ouput:
输出:
1, 2 and 3
1、2 和 3
I have just created a slightly modified template.
我刚刚创建了一个稍微修改过的模板。
<xsl:template name="tokenizeString">
<xsl:param name="list"/>
<xsl:param name="delimiter"/>
<xsl:choose>
<xsl:when test="contains($list, $delimiter)">
<xsl:variable name="listLength" select="string-length($list)" />
<xsl:variable name="listLengthWithoutDelimiters" select="string-length(translate($list, $delimiter,''))" />
<xsl:variable name="noOfDelimiters" select="($listLength - $listLengthWithoutDelimiters)" />
<xsl:value-of select="substring-before($list,$delimiter)"/>
<xsl:if test="$noOfDelimiters > 1">, </xsl:if>
<xsl:if test="$noOfDelimiters = 1"> and </xsl:if>
<xsl:call-template name="tokenizeString">
<xsl:with-param name="list" select="substring-after($list,$delimiter)"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="$list = ''">
<xsl:text/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$list"/>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
The template can be called as below when the delimiter is comma
当分隔符为逗号时,模板可以如下调用
<xsl:call-template name="tokenizeString">
<xsl:with-param name="list">1, 2, 3</xsl:with-param>
<xsl:with-param name="delimiter">
<xsl:value-of select="','" />
</xsl:with-param>
</xsl:call-template>
The template can be called as below when the delimiter is space
当分隔符为空格时,模板可以如下调用
<xsl:call-template name="tokenizeString">
<xsl:with-param name="list">1 2 3</xsl:with-param>
<xsl:with-param name="delimiter">
<xsl:value-of select="' '" />
</xsl:with-param>
</xsl:call-template>
回答by Bludwarf
Thank you user357812. I use your nice template with little customization to make it generic :
谢谢用户357812。我使用你的漂亮模板,几乎没有定制,使其通用:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Main template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="tokenize-children" />
</xsl:copy>
</xsl:template>
<!-- Split child nodes -->
<xsl:template match="*" mode="tokenize-children">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="*" mode="tokenize" />
</xsl:copy>
</xsl:template>
<!-- Tokenize text node of child nodes -->
<xsl:template match="*/text()" name="tokenize" mode="tokenize">
<xsl:param name="text" select="."/>
<xsl:param name="separator" select="','"/>
<xsl:variable name="item" select="name(..)" />
<xsl:choose>
<xsl:when test="not(contains($text, $separator))">
<xsl:element name="{$item}">
<xsl:value-of select="normalize-space($text)"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{$item}">
<xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
</xsl:element>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $separator)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
回答by Nigel Alderton
Depending on what XSL processor you are using, you may have access to the extension function str:tokenize().
根据您使用的 XSL 处理器,您可能可以访问扩展函数str:tokenize()。
So to split Topic1,Topic2,Topic3
on ,
do;
因此分裂Topic1,Topic2,Topic3
上,
做到;
<xsl:copy-of select="str:tokenize('Topic1,Topic2,Topic3', ',')"/>
which will give the result;
这将给出结果;
<token>Topic1</token>
<token>Topic2</token>
<token>Topic3</token>