In the latest post to his blog Thomas Weinert takes a look at a simple challenge someone asked him about - highlighting a section of HTML based on a search string - and his solution.
The challenge is to wrap given words in text content with a span and add a class to the span depending on the word. Do not touch elements, attributes, comments or processing instructions. Do it case insensitive and do it the safe way.
He uses the FluentDOM tool to get the job done. It allows him to create an XPath expression to single out the item to be highlighted (in this case a single or series of words) and wrap them in a matching span tag with the correct styles attached.