If I have the following HTML page
<div>
<p>
Hello world!
</p>
<p> <a href="example.com"> Hello and Hello again this is an example</a></p>
</div>
I want to get the specific word for example 'hello' and change it to 'welcome' wherever they are in the document
Do you have any suggestion? I will be happy to get your answers whatever the type of parser you use?
This is easy to do with XSLT.
XSLT 1.0 solution:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pTarget" select="'hello'"/>
<xsl:param name="pReplacement" select="'welcome'"/>
<xsl:variable name="vtargetLength" select=
"string-length($pTarget)"/>
<xsl:variable name="vUpper" select=
"'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:variable name="vLower" select=
"'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()" name="replace">
<xsl:param name="pText" select="."/>
<xsl:variable name="vLowerText" select=
"translate($pText,$vUpper,$vLower)"/>
<xsl:choose>
<xsl:when test=
"not(contains(concat(' ', $vLowerText, ' '),
concat(' ',$pTarget,' ')
)
)">
<xsl:value-of select="$pText"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="vOffset" select=
"string-length(
substring-before(concat(' ', $vLowerText, ' '),
concat(' ', $pTarget,' ')
)
)"/>
<xsl:value-of select="substring($pText, 1, $vOffset)"/>
<xsl:value-of select="$pReplacement"/>
<xsl:call-template name="replace">
<xsl:with-param name="pText" select=
"substring($pText, $vOffset + $vtargetLength+1)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the provided XML document:
<div>
<p>
Hello world!
</p>
<p> <a href="example.com"> Hello and Hello again this is an example</a></p>
</div>
the wanted, correct result is produced:
<div>
<p>
welcome world!
</p>
<p>
<a href="example.com"> welcome and welcome again this is an example</a>
</p>
</div>
My assumption is that the matching and replacement is case-insensitive (i.e. "hello" and "heLlo" should both be replaced with "welcome"). In case a case-sensitive match is required, the transformation can be considerably simplified.
XSLT 2.0 Solution:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="pTarget" select="'hello'"/>
<xsl:param name="pReplacement" select="'welcome'"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()[matches(.,$pTarget, 'i')]">
<xsl:variable name="vEnlargedRep" select=
"replace(concat(' ',.,' '),
concat(' ',$pTarget,' '),
concat(' ',$pReplacement,' '),
'i')"/>
<xsl:variable name="vLen" select="string-length($vEnlargedRep)"/>
<xsl:sequence select=
"substring($vEnlargedRep,2, $vLen -2)"/>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the provided XML document (shown above), again the wanted, correct result is produced:
<div>
<p>
welcome world!
</p>
<p>
<a href="example.com"> welcome and welcome again this is an example</a>
</p>
</div>
Explanation: Use of the standard XPath 2.0 functions matches()
and replace()
specifying as the third argument "i"
-- a flag for case-insensitive operation.