XSLT 字符串替换

标签 xslt xpath replace xslt-1.0 xpath-1.0

我不太了解 XSL,但我需要修复此代码,我已减少它以使其更简单。
我收到此错误

Invalid XSLT/XPath function

在这条线上

<xsl:variable name="text" select="replace($text,'a','b')"/>

这是 XSL

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:inm="http://www.inmagic.com/webpublisher/query" version="1.0">
    <xsl:output method="text" encoding="UTF-8" />

    <xsl:preserve-space elements="*" />
    <xsl:template match="text()" />

    <xsl:template match="mos">
        <xsl:apply-templates />

        <xsl:for-each select="mosObj">
          'Notes or subject' 
           <xsl:call-template
                name="rem-html">
                <xsl:with-param name="text" select="SBS_ABSTRACT" />
            </xsl:call-template>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="rem-html">
        <xsl:param name="text" />
        <xsl:variable name="text" select="replace($text, 'a', 'b')" />
    </xsl:template>
</xsl:stylesheet>

谁能告诉我这是怎么回事吗?

最佳答案

replace 不适用于 XSLT 1.0。

Codesling 有一个 template for string-replace您可以使用该函数的替代品:

<xsl:template name="string-replace-all">
    <xsl:param name="text" />
    <xsl:param name="replace" />
    <xsl:param name="by" />
    <xsl:choose>
        <xsl:when test="$text = '' or $replace = ''or not($replace)" >
            <!-- Prevent this routine from hanging -->
            <xsl:value-of select="$text" />
        </xsl:when>
        <xsl:when test="contains($text, $replace)">
            <xsl:value-of select="substring-before($text,$replace)" />
            <xsl:value-of select="$by" />
            <xsl:call-template name="string-replace-all">
                <xsl:with-param name="text" select="substring-after($text,$replace)" />
                <xsl:with-param name="replace" select="$replace" />
                <xsl:with-param name="by" select="$by" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

调用方式:

<xsl:variable name="newtext">
    <xsl:call-template name="string-replace-all">
        <xsl:with-param name="text" select="$text" />
        <xsl:with-param name="replace" select="a" />
        <xsl:with-param name="by" select="b" />
    </xsl:call-template>
</xsl:variable>

另一方面,如果您实际上只需要将一个字符替换为另一个字符,则可以调用 translate具有相似的签名。像这样的东西应该可以正常工作:

<xsl:variable name="newtext" select="translate($text,'a','b')"/>

另外,请注意,在这个示例中,我将变量名称更改为“newtext”,在 XSLT 中变量是不可变的,因此您不能像您一样执行 $foo = $foo 的等效操作在你的原始代码中有。

关于XSLT 字符串替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3067113/

相关文章:

xsltproc 预附加 <?xml 版本 ="1.0"?>

php - 在递归函数中获取 xml 节点的 xpath

javascript - 正则表达式替换与任何字符匹配的所有内容

Objective-C – 用一个空格替换换行序列

python - 如何在 Python 中使用 Selenium 进行参数化/数据驱动测试

.Net 替换 VBScript (interop.msscriptcontrol.dll) DLL

xslt - "XPath is invalid"出现错误 "hours-from-duration($duration)"

xml - 连接 xml 文件

xml - 如何使用 xslt 获取元素的最后一个值

java - Java中如何使用节点属性值读取、更新和删除现有的XML文件