Xpath to search for an element with a condition that matches the attribute and child value of node

I have two elements with the same attribute, but with different child node values. Can I request a search for a specific element that matches the attribute, as well as a child value of node. To be specific, this is an example xml that I use for a query (each element in the original xml has more than 10 child nodes).

<Book size="2"> <Title>abc</Title> <Price>10</Price> </Book> <Book size="2"> <Title>xyz</Title> <Price>20</Price> </Book> <Book size="4"> <Title>Harry</Title> <Price>10</Price> </Book> 

So now I want to find the Book element that has @size = "2" and Title = xyz .

Is this possible with the SelectSingleNode method? If not how to request it?

thanks

+6
source share
2 answers

It:

 //Book[@size='2'][Title='xyz'] 

Or that:

 //Book[@size='2' and Title='xyz'] 

Note that using // not recommended when your schema is known.

+12
source

It works?

 //Book[@size='2']//Title[text() = "xyz"]/.. 
+1
source

Source: https://habr.com/ru/post/886272/


All Articles