我想实现一个`split function/template`,它将一个字符串和一个分隔符作为输入,并返回一个字符串的拆分数组 [英] I would like to implement a `split function/template` which takes as input a string and a delimiter and returns a split array of the string
问题描述
我想在 XSLT 中实现一个 split 函数/模板
,它将字符串和分隔符作为输入,并返回字符串的拆分数组..
I would like to implement a split function/template
in XSLT which takes as input a string and a delimiter and returns a split array of the string..
或者更确切地说,我希望能够按照以下方式做一些事情:
Or rather I'd like the ability to do something along the lines of:
<xsl:call-template name="F">
<xsl:with-param name="input" select="'a,b,c,d,e'"/>
<xsl:with-param name="replacement">
<option value='$x'>$x</option>
</xsl:with-param>
</xsl:call-template>
这会给我
<option value='a'>a</option><option value='b'>b</option><option value='c'>c</option><option value='d'>d</option><option value='e'>e</option>
针对 XSLT 1.0 的问题(但我不介意学习 XSLT 2.0 也是如何做到的)
Question targeted at XSLT 1.0 (but i don't mind learning how XSLT 2.0 does it too)
推荐答案
I.XSLT 2.0 解决方案
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="vStr" select="'a,b,c,d,e'"/>
<xsl:for-each select="tokenize($vStr, ',')">
<option value="{.}"><xsl:value-of select="."/></option>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
将此转换应用于 ant XML 文档(未使用)时,会产生所需的正确结果:
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
说明:标准 XPath 2.0 函数的使用 tokenize()
.
Explanation: Use of the standard XPath 2.0 function tokenize()
.
二.XSLT 1.0:使用 FXSL 1.x str-split-to-words
函数/模板
II. XSLT 1.0: Using the FXSL 1.x str-split-to-words
function/template
这个 XSLT 1.0 转换:
This XSLT 1.0 transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
exclude-result-prefixes="ext">
<xsl:import href="strSplit-to-Words.xsl"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:variable name="vwordNodes">
<xsl:call-template name="str-split-to-words">
<xsl:with-param name="pStr" select="/"/>
<xsl:with-param name="pDelimiters"
select="', '"/>
</xsl:call-template>
</xsl:variable>
<xsl:apply-templates select=
"ext:node-set($vwordNodes)/*"/>
</xsl:template>
<xsl:template match="word">
<option value="{.}"><xsl:value-of select="."/></option>
</xsl:template>
</xsl:stylesheet>
应用于此 XML 文档时:
<t>a,b,c d,e</t>
产生想要的、正确的结果:
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
请注意 pDelimiters
参数(顾名思义)可以包含多个分隔符——在这种情况下,我们同时使用 ','
和 ' '
.
Do note The pDelimiters
parameter (as its name says) can hold more than one delimiting character -- in this case we use both ','
and ' '
.
三.使用手写递归命名模板的 XSLT 1.0 解决方案:
这种转变:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="text()" name="tokenize">
<xsl:param name="pText" select="."/>
<xsl:param name="pDelim" select="','"/>
<xsl:if test="string-length($pText) > 0">
<xsl:variable name="vToken" select=
"substring-before(concat($pText,','), ',')"/>
<option value="{$vToken}">
<xsl:value-of select="$vToken"/>
</option>
<xsl:call-template name="tokenize">
<xsl:with-param name="pText" select=
"substring-after($pText,',')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
应用于此 XML 文档时:
<t>a,b,c,d,e</t>
产生想要的、正确的结果:
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
四.将模板/函数作为参数传递给上面 III 中的 tokenize
模板以处理每个令牌
IV. Passing to the tokenize
template in III above as a parameter a template/function to process each token
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:f="http://fxsl.sf.net" exclude-result-prefixes="f">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<f:processToken/>
<xsl:variable name="vFunc" select=
"document('')/*/f:processToken[1]"/>
<xsl:template match="text()" name="tokenize">
<xsl:param name="pText" select="."/>
<xsl:param name="pDelim" select="','"/>
<xsl:param name="pProcessFunc" select="$vFunc"/>
<xsl:if test="string-length($pText) > 0">
<xsl:variable name="vToken" select=
"substring-before(concat($pText,','), ',')"/>
<xsl:apply-templates select="$pProcessFunc">
<xsl:with-param name="arg1" select="$vToken"/>
</xsl:apply-templates>
<xsl:call-template name="tokenize">
<xsl:with-param name="pText" select=
"substring-after($pText,',')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="f:processToken">
<xsl:param name="arg1"/>
<option value="{$arg1}">
<xsl:value-of select="$arg1"/>
</option>
</xsl:template>
</xsl:stylesheet>
当此转换应用于与 III 中相同的 XML 文档时,会产生相同的、想要的、正确的结果:
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
现在,如果我们用这个替换最后一个模板:
<xsl:template match="f:processToken">
<xsl:param name="arg1"/>
<p>
<xsl:value-of select="$arg1"/>
</p>
</xsl:template>
再次产生了想要的结果:
<p>a</p>
<p>b</p>
<p>c</p>
<p>d</p>
<p>e</p>
这篇关于我想实现一个`split function/template`,它将一个字符串和一个分隔符作为输入,并返回一个字符串的拆分数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!