如何实现XSLT tokenize函数?

Mat*_*sta 4 php xml xslt tokenize

似乎EXSLT tokenize函数不适用于PHP XSLTProcessor(XSLT 1.0).

我试图在纯XSL中实现它,但我不能使它工作:

<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:func="http://exslt.org/functions"
    xmlns:exsl="http://exslt.org/common"
    xmlns:my="http://mydomain.com/">

    <func:function name="my:tokenize">
        <xsl:param name="string"/>
        <xsl:param name="separator" select="'|'"/>
        <xsl:variable name="item" select="substring-before(concat($string,$separator),$separator)"/>
        <xsl:variable name="remainder" select="substring-after($string,$separator)"/>
        <xsl:variable name="tokens">
            <token><xsl:value-of select="$item"/></token>
            <xsl:if test="$remainder!=''">
                <xsl:copy-of select="my:tokenize($remainder,$separator)"/>
            </xsl:if>
        </xsl:variable>
        <func:result select="exsl:node-set($tokens)"/>
    </func:function>

    <xsl:template match="/">
        <xsl:copy-of select="my:tokenize('a|b|c')"/>
    </xsl:template>

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

预期结果 :

    <token>a</token><token>b</token><token>c</token>
Run Code Online (Sandbox Code Playgroud)

实际结果 :

    abc
Run Code Online (Sandbox Code Playgroud)

我知道这个问题已被多次发布,但我找不到简单的解决方案.

谢谢您的帮助.

Gor*_*don 7

引用http://www.exslt.org/str/functions/tokenize/index.html

以下XSLT处理器支持str:tokenize:

  • 4XSLT,来自4Suite.(版本0.12.0a3)
  • 来自Daniel Veillard等人的libxslt.(版本1.0.19)

由于PHP使用libxslt,它意味着tokenize可用,但您必须使用正确的扩展名称空间(您不这样做):

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:str="http://exslt.org/strings"
    extension-element-prefixes="str"
    …
Run Code Online (Sandbox Code Playgroud)

然后您可以使用tokenize作为函数,例如构建一个数字为1-12的选择框:

<select name="months">
    <xsl:for-each select="str:tokenize('1,2,3,4,5,6,7,8,9,10,11,12', ',')">
        <xsl:element name="option">
            <xsl:attribute name="value">
                <xsl:value-of select="."/>
            </xsl:attribute>
            <xsl:value-of select="."/>
        </xsl:element>
    </xsl:for-each>
</select>
Run Code Online (Sandbox Code Playgroud)