我試圖通過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樣式表(可能)包含遞歸」。那麼,它當然是的,否則它不會是一個字符串替換模板。其他使用相同風格在網絡中搜索的模板也觸發相同的問題。
FF消息最有可能意味着*無限遞歸*,這意味着存在(實際)堆棧溢出。您需要更正您的代碼。要麼提供您的代碼(編輯問題並輸入代碼),要麼提供一個可用的XSLT 1.0字符串替換解決方案(xslt標籤中的各種答案中有很多這樣的解決方案)。 – 2011-05-30 01:10:39
您也可以嘗試使用Saxon CE,它是一個運行客戶端的XSLT 2.0實現作爲瀏覽器的一部分。 – 2011-05-30 01:13:00
我加上遺憾:各大瀏覽器項目什麼時候會遷移到優秀的XSLT 2.0?這整個Q將被否定。 – Paulb 2014-09-04 22:27:44