Reputation: 19528
I have some html mixed with text and links and I would like to extract the text as it is with the linked words without having to remove the links and do some crazy thing later to put it back in at the same place.
The HTML looks like this:
<div id="i want what is inside here">
<h3>some text</h3>
<div>more text with a <a href="url">link</a></div>
<p>some more text<br />
<a href="url">another link</a> here...</p>
</div>
And the output I wanted is:
some text
more text with a <a href="url">link</a>
some more text
<a href="url">another link</a> here...
I know how to extract the text using HTMLAgilityPack recently I've learned lots of new things on how to use ancestor, preceed on the xpath and some other things and it made me wonder:
Is it possible using xpath to get the desired output mentioned above or what how should I do it ?
If there is no xpath condition that is usable to this I was thinking of extracting the text then extracting the links and replace the link inner text matchs on the text with links (not reliable I assume) but it was one way I thinked that could be done, what would you advise me to do ?
Upvotes: 0
Views: 210
Reputation: 32323
XPath, the XML Path Language, is a query language for selecting nodes from an XML document.
You need to transform your document according to your rules. You could use xpath to select nodes which should be transformed, but xpath couldn't be used to perform this transformation.
To do it, you could iterate through the document nodes from the most deep to the root node, and if this is not an <a>
tag, replace it with the inner HTML of it.
Fortunately, it seems that AgilityPack enumerates nodes in the order the nodes found in the document. This means the necessary order can be retrieved by reversing the list of nodes. See:
// getting the non-anchor nodes in the reversed order
var nodes = doc.DocumentNode.SelectNodes("//*[name()!='a']")
.Reverse()
.ToList();
// replacing with the inner html
foreach (var node in nodes)
{
var replacement = doc.CreateTextNode(node.InnerHtml);
node.ParentNode.ReplaceChild(replacement, node);
}
// and getting the output
var output = doc.DocumentNode.OuterHtml;
This will get you something like this:
some text
more text with a <a href='url'>link</a>
some more text
<a href='url'>another link</a> here...
But note, after the transformation the document became the whole text node. If you need to work with it as HTML fragment, you'll have to create a new document from the modified one.
Upvotes: 2