在Firefox中使用XSLT替换功能

问题描述 投票:0回答:1

我正在尝试通过XSLT执行字符串替换,但实际上我在Firefox中看不到这种方法。

当我通过这样的事情使用XSLT 2.0 replace()函数时:

<xsl:value-of select="replace(., 'old', 'new')"/>

我在Firefox中遇到错误“调用了一个未知的XPath扩展函数”。当我尝试使用任何执行替换的XSLT 1.0兼容模板时,我得到错误“XSLT样式表(可能)包含一个递归”(当然它包含一个递归,我看不到在XSLT中执行字符串替换的其他方法没有递归函数)。

所以,没有机会使用XSLT 2.0的replace()函数,也没有机会使用递归模板。如何使用XSLT执行该技巧?请不要在服务器端做出任何建议,我实现了我的整个网站,以便只运行客户端转换而我只能因为一个问题而无法回滚,而且在2011年我不能使用它是不对的像XSLT这样的强大技术,因为它的错误和不完整的实现。

编辑:

我使用的代码与此处提供的代码相同:XSLT Replace function not found

我用这个XML进行测试:

<?xml version="1.0"?>
<?xml-stylesheet href="/example.xsl" type="text/xsl"?>

<content>lol</content>

这个XSLT:

<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>

<xsl:template name="string-replace-all">
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="by"/>
<xsl:choose>
<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:template match="content">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="lolasd"/>
<xsl:with-param name="replace" select="lol"/>
<xsl:with-param name="by" select="asd"/>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>

在Firefox上我得到“XSLT样式表(可能)包含一个递归”。嗯,当然是这样,否则它不会是字符串替换模板。使用相同样式在网络上拾取的其他模板也会触发相同的问题。

xml xslt replace xslt-2.0
1个回答
2
投票

有了这个文件

<content>lol</content>

这些参数

<xsl:with-param name="text" select="lolasd"/>
<xsl:with-param name="replace" select="lol"/>
<xsl:with-param name="by" select="asd"/>

将是空的,因此这个条件

<xsl:when test="contains($text,$replace)">

将永远是真实的,您的代码将无限追加。

我想你的意图是在<xsl:with-param>元素中选择一个字符串而不是节点,但是你忘了使用引号/撇号。你应该拥有的是什么

<xsl:with-param name="replace" select="'lol'"/>

不过,如果您最终选择了emtpy字符串,则应该为一个案例添加一个检查,其中参数为空以避免此类问题。

© www.soinside.com 2019 - 2024. All rights reserved.