I am parsing some XML something like this:
<root> <some_gunk/> <dupe_node> ... stuff I want ... </dupe_node> <bits_and_pieces/> <other_gunk/> <dupe_node> ... stuff I don't want ... </dupe_node> <more_gunk/> </root>
An XPath of '//dupe_node'
will give me two instances of dupe_node
to play with. I only want to traverse the first. Can I do this with XPath?
Simplified Xpath syntax:/e1 -- selects the first <e1> document element (child element of the document node). /e1/e2 -- selects the first <e2> child element of the first <e1> document element. /e1[2]/e2[3] -- selects the third <e2> child element of the second <e1> document element. /e1[1]/e2[1] -- same as /e1/e2 .
For example if both text fields have //input[@id='something'] then you can edit the first field xpath as (//input[@id='something'])[1] and the second field's xpath as (//input[@id='something'])[2] in object repository.
The indexOf() method returns the position of the first occurrence of a value in a string.
Locating Strategies- (By XPath- Using last())"last() method" selects the last element (of mentioned type) out of all input element present.
/descendant::dupe_node[1]
//dupe_node[1]
is generally wrong, although it produces an identical result in this particular case. See docs:
The location path //para[1] does not mean the same as the location path /descendant::para[1]. The latter selects the first descendant para element; the former selects all descendant para elements that are the first para children of their parents.
Given the following XML:
<foo> <bar/> <foo> <bar/> </foo> </foo>
//bar[1]
will produce two nodes, because both bars are first children of their respective parents.
/descendant::bar[1]
will give only one node, which is the first of all the bars in the document.
//dupe_node[1]
XPath counts from 1, not 0 in this case. You can use this tool to try things out in your browser:
http://www.xmlme.com/XpathTool.aspx?mid=82
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