Edit: As @LarsH has noticed, I hadn't paid attention to the requirement that the replacement should be in bold.
There are two easy ways to correct this:
.1. In the transformation replace:
<xsl:value-of select="$pRep"/>
with
<b><xsl:value-of select="$pRep"/></b>
.2. Pass as the value of the pReplacement
parameter not just "ABC"
but <b>ABC</b>
This XSLT 1.0 transformation:
<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>
when applied on the provided XML document (corrected to be well-formed):
<html>
<h1>This word should not be replaced: TEST</h1>.
But this one should be replaced: test
</html>
produces the wanted result:
<html>
<h1>This word should not be replaced: TEST</h1>.
But this one should be replaced: ABC
</html>
Do note:
This is a generic transformation that accepts as parameters the target and the replacement text.
The replacement is case-incensitive, but we suppose that the target parameter is provided in lowercase.
It is even much easier to solve this with XSLT 2.0.