如何实现 XSLT 标记化功能? [英] How to implement XSLT tokenize function?

查看:20
本文介绍了如何实现 XSLT 标记化功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎 EXSLT 标记化功能不适用于 PHP XSLTProcessor (XSLT 1.0).

It seems like EXSLT tokenize function is not available with PHP XSLTProcessor (XSLT 1.0).

我尝试在纯 XSL 中实现它,但我无法使它工作:

I tried to implement it in pure XSL but I can't make it work :

<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>

预期结果:

    <token>a</token><token>b</token><token>c</token>

实际结果:

    abc

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

I know this question has been posted many times but I can't find a simple solution.

感谢您的帮助.

推荐答案

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

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

The following XSLT processors support str:tokenize:

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

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

Since PHP uses libxslt, it means tokenize is available, but you have to use the right extension namespaces (which you dont do):

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:str="http://exslt.org/strings"
    extension-element-prefixes="str"
    …

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

Then you can use tokenize as a function, for example to build a select box with numbers 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>

这篇关于如何实现 XSLT 标记化功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆