Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the XPath to select a range of nodes?

Tags:

xml

xpath

I have an XML file that's structured like this:

 <foo>      <bar></bar>      <bar></bar>      ... </foo> 

I don't know how to grab a range of nodes. Could someone give me an example of an XPath expression that grabs bar nodes 100-200?

like image 872
Shawn Avatar asked Jul 28 '10 16:07

Shawn


People also ask

What will be the XPath expression to select?

XPath uses path expressions to select nodes or node-sets in an XML document. The node is selected by following a path or steps.

How do you select an element by XPath?

Go to the First name tab and right click >> Inspect. On inspecting the web element, it will show an input tag and attributes like class and id. Use the id and these attributes to construct XPath which, in turn, will locate the first name field.

What is an XPath node?

Xpath Node is defined as a point where the path address initiates, as it follows a concept of nodes. In simple terms they are the individual elements of the Xpath hierarchical structure which are termed as a node and enable an XSL processing. Xpath expressions could be done with HTML and XML.

What is an index XPath?

Definition of XPath Index. XPath index is defined as per specific nodes within the node-set for index by using selenium webdriver. We can also mention the index-specific node with the help of a number of indexes enclosed by []. The tag name element UI contains multiple child elements with tag name as li.


2 Answers

Use:

/*/bar[position() >= 100 and not(position() > 200)] 

Do note:

  1. Exactly the bar elements at position 100 to 200 (inclusive) are selected.

  2. The evaluation of this XPath expressions can be many times faster than an expression using the // abbreviation, because the latter causes a complete scan of the tree whose root is the context node. Always try to avoid using the // abbreviation in cases when this is possible.

like image 105
Dimitre Novatchev Avatar answered Oct 01 '22 03:10

Dimitre Novatchev


//foo/bar[100 <= position() and position() < 200] 
like image 37
kennytm Avatar answered Oct 01 '22 05:10

kennytm