Search code examples
xmlparsingxpathhtml-parsingdomxpath

Getting anchor that contains bold tags with xPath?


I am using xpath and I am trying to get the anchor text from a link that looks like this:

<a href="http://link.com"><b>anchor</b> text</a>

I tried getting the anchor text with the xPath:

.//a/text()

But I only get "text" as result, not "anchor text".

How do I get the full text?


Solution

  • .//a/text()
    

    The reason that the text node containing "anchor" is because this text node is child of b -- not child of a. The above expression selects only text nodes that are children of any a that is a descendant of the current context node.

    Use:

    .//a//text()
    

    This selects all text-nodes that are descendants of all a elements that are descendents of the current context node.