16

How can I use XPath to select an XML-node based on its content?

If I e.g. have the following xml and I want to select the <author>-node that contains Ritchie to get the author's full name:

<books> <book isbn='0131103628'> <title>The C Programming Language</title> <authors> <author>Ritchie, Dennis M.</author> <author>Kernighan, Brian W.</author> </authors> </book> <book isbn='1590593898'> <title>Joel on Software</title> <authors> <author>Spolsky, Joel</author> </authors> </book> </books> 

3 Answers 3

23
/books/book/authors/author[contains(., 'Ritchie')] 

or

//author[contains(., 'Ritchie')] 
Sign up to request clarification or add additional context in comments.

5 Comments

For XPath newbies such as myself it might be valuable to note that the . is a shorter alias for text() - so yes, this answer does the right thing.
where could I read a complete and friendly reference of these functions?
@Oliver — This mention of text() did confuse me much. It is wrong. The . is simply the current element.
@NicolasBarbulesco - Thanks for your comment and the link to the example. At the very end of the page they also write this: Whenever you need to select an element and use a predicate on that same element you will need to use a period "." to access that element's value. So yes, my mention of text() was not correct. As per w3.org: . selects the context node and text() selects all text node children of the context node. And even more examples using text().
It doesn't seem to work when the target text has multiple lines.
4

The XPath for this is:

/books/book/authors/author[contains(., 'Ritchie')] 

In C# the following code would return "Ritchie, Dennis M.":

xmlDoc.SelectSingleNode("/books/book/authors/author[contains(., 'Ritchie')]").InnerText; 

Comments

4
//author[contains(text(), 'Ritchie')] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.