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:
when this transformation is applied on the provided XML document:
the wanted, correct result is produced:
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:
when this transformation is applied on the provided XML document (shown above), again the wanted, correct result is produced:
Explanation: Use of the standard XPath 2.0 functions
matches()
andreplace()
specifying as the third argument"i"
-- a flag for case-insensitive operation.