我正尝试通过XSLT执行字符串替换,但实际上我在Firefox中看不到这样的方法。
当我通过如下代码使用XSLT2.0place()函数时:
<xsl:value-of select="replace(., 'old', 'new')"/>在火狐中,我收到错误消息“调用了未知的XPath扩展函数”。当我尝试使用任何执行替换的XSLT 1.0兼容模板时,我得到错误消息"XSLT样式表(可能)包含一个递归“(当然它包含一个递归,我看不到在没有递归函数的情况下在XSLT中执行字符串替换的其他方法)。
因此,没有机会使用XSLT2.0place()函数,也没有机会使用递归模板。如何使用XSLT执行此操作?请不要建议在服务器端实现它,我实现整个网站是为了操作客户端的转换,我不能仅仅因为一个问题而回滚,在2011年,我不能使用像XSLT这样的强大技术,因为它的buggy和不完整的实现。
编辑:
我使用的代码与这里提供的代码相同: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样式表(可能)包含一个递归“。当然是这样,否则它就不是字符串替换模板了。其他在网上使用相同风格的模板也会引发同样的问题。
发布于 2011-05-30 18:58:32
使用本文档
<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字符串,则应该为参数为空的情况添加检查,以避免此类问题。
https://stackoverflow.com/questions/6171330
复制相似问题