Reputation: 3331
Given this:
<details>
<summary>1</summary>
<from>2</from>
<heading>WHAT YOU WANT</heading>
<body>No not this</body>
</details>
I'm trying to grab WHAT YOU WANT strictly by first finding a match of "1" in the summary, then finding "2" in the from, and lastly grabbing the node just after the one with "2". (Just take the conditions of requiring checking 1 and 2 for granted - this is a simplified example of a larger problem.)
I tried this but it only matches "Node from 2":
//details[summary[text()="1"]]//following-sibling::*[contains(text(), "2")]
Even though my understanding from https://www.w3schools.com/xml/xpath_axes.asp tells me this should work?
Upvotes: 1
Views: 1669
Reputation: 111541
This XPath,
//details[1]/summary[.='1']
/following-sibling::from[.='2']
/following-sibling::heading
will select the heading
element following the from
element whose string value equals 2 following the summary
element whose string value equals 1, all contained within the first details
element in the document.
Responding to follow-up questions in the comments:
From w3schools, the / indicates that we're looking for children of the previous node, so a / after summary, to me, means that we're looking at children of summary, for which there is nothing... Why?
Oh I see - if you use an axis right after the /, the meaning is not necessarily child anymore; it's whatever the axis specifies, like following-sibling.
Right, your original confusion stemmed from the meaning of /
in XPath.
/
separates location steps. A location step consists of an axis, a node test, and zero or more predicates. The default axis is child
, but others are possible. In the above XPath, the following-sibling
axis is used.
Upvotes: 1