How to Select First Element via Xpath

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

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.

How to select first element via XPath?

Any of these XPath expressions will select the first a element:

  • (//a)[1] selects first a in the whole document.
  • (/div/ul/li/figure/a)[1] selects first a with shown heritage.
  • (//div[@class='carousel']/ul/li/figure/a)[1] restricts heritage.
  • (//div[@class='carousel']//a)[1] abstracts away some heritage.

Choose depending upon the context of your shown XML in your actual document and whether you wish to restrict the a elements to only those under certain other elements.

Common Mistake

Note that //a[1] actually selects multiple a elements:

<a id="one"/>
<a id="two"/>

because //a[1] means select the a elements that are the first child of its parent.

You must use parentheses (//a)[1] to select

<a id="two"/>

alone as the first a in the document.

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

The issue with your xpath is that //book is selecting all book elements.

Try this instead: /bookstore/category[book='A2']

xpath get first element based on condition

It should be

/root/entry[.//field1='B' or .//field1='C'][1]

Note that entry[//field1='B'][1] means return first entry node if field1 node with value 'B' exists (anywhere in XML) while entry[.//field1='B'][1] means return first entry node if it has a descendant field1 node with value 'B'

Also you can simplify expression as

/root/entry[field1='B' or field1='C'][1]

if field1 always appears as direct child of entry

Get first element Xpath

This will give you the first book title

"(//book)[1]//div[@class='title']"

And this gives the first book year

"(//book)[1]//div[@class='year']"

What is the XPath expression to find only the first occurrence?

The correct answer (note the brackets):

(//span[@class='Big'])[1]

The following expression is wrong in the general case:

//span[@class='Big'][1]

because it selects every span element in the document, that satisfies the condition in the first predicate, and that is the first such child of its parent -- there can be many such elements in an XML document and all of them will be selected.

For more detailed explanation see: https://stackoverflow.com/a/5818966/36305

Xpath only selecting first instance of element

You can modify your evaulate() method like below.

You can get all the nodes with tag name a using xpath and then iterate through the nodes to get text inside tag.

NodeList node = (NodeList) xPath.evaluate( "/Pearent/a", xml, XPathConstants.NODESET );

StringBuilder stringBuilder = new StringBuilder();

for ( int i = 0; i < node.getLength(); i++ ) {

stringBuilder.append( node.item( i ).getTextContent() );
}

System.out.println( stringBuilder.toString() );

Output:

helloworld

XPath to select first element in a dropdown list

(//ul[@class='dropdown-list']/li[@class='active']/a)[1]

or

(//div[@class='dropdown-menu']/ul[@class='dropdown-list']/li[@class='active']/a)[1]

References:

XPath Get first element of subset

XPath: Select first element with a specific attribute



Related Topics



Leave a reply



Submit