Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select the first element with a specific attribute using XPath

Tags:

xpath

The XPath bookstore/book[1] selects the first book node under bookstore.

How can I select the first node that matches a more complicated condition, e.g. the first node that matches /bookstore/book[@location='US']

like image 550
ripper234 Avatar asked Jun 17 '09 10:06

ripper234


People also ask

How do I select the first child in XPath?

The key part of this XPath is *[1] , which will select the node value of the first child of Department .

Which button is used to find the first occurrence of the data?

To open the Find function, use the shortcut Ctrl+F or navigate to Home>Editing>Find.


1 Answers

Use:

(/bookstore/book[@location='US'])[1] 

This will first get the book elements with the location attribute equal to 'US'. Then it will select the first node from that set. Note the use of parentheses, which are required by some implementations.

Note, this is not the same as /bookstore/book[1][@location='US'] unless the first element also happens to have that location attribute.

like image 179
Jonathan Fingland Avatar answered Sep 17 '22 16:09

Jonathan Fingland