Skip to content Skip to sidebar Skip to footer

How To Match A Text Node Then Follow Parent Nodes Using Xpath

I'm trying to parse some HTML with XPath. Following the simplified XML example below, I want to match the string 'Text 1', then grab the contents of the relevant content node. <

Solution 1:

Do you want that?

//title[text()='Text 1']/../content/text()

Solution 2:

Use:

string(/*/*/title[. = 'Text 1']/following-sibling::content)

This represents at least two improvements as compared to the currently accepted solution of Johannes Weiß:

  1. The very expensive abbreviation "//" (usually causing the whole XML document to be scanned) is avoided as it should be whenever the structure of the XML document is known in advance.

  2. There is no return back to the parent (the location step "/.." is avoided)

Post a Comment for "How To Match A Text Node Then Follow Parent Nodes Using Xpath"