Look at <bookmark/>
. The desired output I want is the id with value 5
<xsl:value-of select="//bookmark/ancestor::*[@id][1]/@id"/>
only gives me 4
<?xml version="1.0" encoding="UTF-8"?>
<content>
<section id="1">
<section id="2"/>
<section id="3"/>
<section id="9"/>
</section>
<section id="4">
<section>
<section id="10"/>
<section id="5"/>
<section>
<bookmark/>
<section id="6">
<section id="7">
<section id="8"/>
</section>
</section>
</section>
</section>
</section>
</content>
We will then find the XPath of the parent element node of the current node using parent syntax, as shown below screenshot. XPath of Parent node: //input[@id = 'text']//parent::span. It will then select the parent node of the input tag having Id = 'text'. XPath(Parent node): //input[@id = 'text']//parent::*.
Different XPath Axes Used In Selenium Testing ancestor-or-self: This one indicates the context node and all the ancestors relative to the context node, and includes the root node. attribute: This indicates the attributes of the context node. It can be represented with the “@” symbol.
XPath is a mini-language that describes a node pattern to select a set of nodes. Descendant is declared as ' // ' so a child can be defined as a// b, to go deeper into the XML tree descendant gives away.
The way I understand the question and the provided XML document is that the nearest id
attribute can also happen on an ancestor (section
) element.
In any such case the expressions using ony the preceding::
axis (as specified in the other answers to this question) don't select any node.
One correct XPath expression, which selects the wanted id
attribute is:
(//bookmark/ancestor::*[@id][1]/@id
|
//bookmark/preceding::*[@id][1]/@id
)
[last()]
If the id
attribute is also allowed on the bookmark
element itself, the above XPath expression needs to be modified slightly to accomodate this:
(//bookmark/ancestor-or-self::*[@id][1]/@id
|
//bookmark/preceding::*[@id][1]/@id
)
[last()]
Do note: The ancestor::
and the preceding::
axes do not intersect (do not overlap).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With