2011-05-30 52 views
0

我试图通过XSLT执行字符串替换,但实际上我没有看到Firefox中的替换方法。Firefox中的XSLT替换函数

当我使用XSLT 2.0替换()函数经过是这样的:

<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样式表(可能)包含递归”。那么,它当然是的,否则它不会是一个字符串替换模板。其他使用相同风格在网络中搜索的模板也触发相同的问题。

+2

FF消息最有可能意味着*无限递归*,这意味着存在(实际)堆栈溢出。您需要更正您的代码。要么提供您的代码(编辑问题并输入代码),要么提供一个可用的XSLT 1.0字符串替换解决方案(xslt标签中的各种答案中有很多这样的解决方案)。 – 2011-05-30 01:10:39

+1

您也可以尝试使用Saxon CE,它是一个运行客户端的XSLT 2.0实现作为浏览器的一部分。 – 2011-05-30 01:13:00

+0

我加上遗憾:各大浏览器项目什么时候会迁移到优秀的XSLT 2.0?这整个Q将被否定。 – Paulb 2014-09-04 22:27:44

回答

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字符串。

+1

谢谢,它做到了。我不知道XSLT在字符串和节点名称之间做了这种区别,很高兴知道:) – BlackLight 2011-05-30 11:46:39

+0

Aha ... xslt gotcha – GuruM 2012-04-05 12:13:17