Next, we slice the node in two. We do this by creating a new text
node and inserting it before the existing one. After we set the text
values of each node, the first will contain everything before the
word "monkeys", and the other will
have everything after the word. Note the use of the
XML::DOM::Document object as a factory to create
the new text node. This DOM feature takes care of many administrative
tasks behind the scenes, making the genesis of new nodes painless.
After that step, we create an <a> element
and insert it between the text nodes. Like all good links, it needs a
place to put the URL, so we set it up with an href
attribute. To have something to click on, the link needs text, so we
create a text node with the word
"monkeys" and append it to the
element's child list. Then the routine will recurse
on the text node after the link in case there are more instances of
"monkeys" to process.
Does it work? Running the program on this file:
<html>
<head><title>Why I like Monkeys</title></head>
<body><h1>Why I like Monkeys</h1>
<h2>Monkeys are Cute</h2>
<p>Monkeys are <b>cute</b>. They are like small, hyper versions of
ourselves. They can make funny facial expressions and stick out their
tongues.</p>
</body>
</html>