編輯:作爲@LarsH已經注意到,我沒有注意,更換應在大膽的要求。
有兩種簡單的方法可以解決這個問題:
.1。 在改造替換:
<xsl:value-of select="$pRep"/>
與
<b><xsl:value-of select="$pRep"/></b>
0.2。 通行證作爲pReplacement
參數不僅"ABC"
但<b>ABC</b>
這XSLT 1.0轉化的值:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pTarget" select="'test'"/>
<xsl:param name="pReplacement" select="'ABC'"/>
<xsl:variable name="vCaps" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:variable name="vLowecase" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()[not(ancestor::h1)]">
<xsl:call-template name="replaceCI">
<xsl:with-param name="pText" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template name="replaceCI">
<xsl:param name="pText"/>
<xsl:param name="pTargetText" select="$pTarget"/>
<xsl:param name="pRep" select="$pReplacement"/>
<xsl:variable name="vLowerText"
select="translate($pText, $vCaps, $vLowecase)"/>
<xsl:choose>
<xsl:when test=
"not(contains($vLowerText, $pTargetText))">
<xsl:value-of select="$pText"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="vOffset" select=
"string-length(substring-before($vLowerText, $pTargetText))"/>
<xsl:value-of select="substring($pText,1,$vOffset)"/>
<xsl:value-of select="$pRep"/>
<xsl:call-template name="replaceCI">
<xsl:with-param name="pText" select=
"substring($pText, $vOffset + string-length($pTargetText)+1)"/>
<xsl:with-param name="pTargetText" select="$pTargetText"/>
<xsl:with-param name="pRep" select="$pRep"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
當所提供的XML文檔施加(校正爲良好形成):
<html>
<h1>This word should not be replaced: TEST</h1>.
But this one should be replaced: test
</html>
產生通緝的結果:
<html>
<h1>This word should not be replaced: TEST</h1>.
But this one should be replaced: ABC
</html>
請注意:
這是一個通用的轉型接受作爲參數來確定目標和替換文本。
替換是不區分大小寫的,但我們假設目標參數是以小寫形式提供的。
用XSLT 2.0解決這個問題甚至更容易。
只要您輸入「DOM」的輸入,就不能使用'$ dom-> saveHTML'來實現輸出。在這裏看到我對你的問題的回答[如何用PHP進行不區分大小寫的XPath節點測試](http://stackoverflow.com/questions/4046022/parse-content-for-appearance-of-a-keyword-inside-h1 -h2-and-h3-heading-tags)或[純XPath 1.0解決方案](http://stackoverflow.com/questions/3238989/case-insensitive-xpath-searching-in-php) – Gordon 2010-11-05 23:01:15
好問題,+ 1。 Seemy回答了完整的XSLT 1.0解決方案。:) – 2010-11-05 23:14:10