0

Is there a way to build a XPath query that finds a node at a certain position AND with a certain attribute value?

Consider the following sample xml:

<Item Type="Book">
<!--1st Param node in a Book item is always the autors last name-->
<Param Value="Updike" />
<!--2nd Param node in a Book item is always the autors first name-->
<Param Value="John" />
<!--3rd Param node in a Book item is always the book title-->
<Param Value="Toward the End of Time" /></Item>

Now can I build a single query that finds the following:

Find all Item nodes of Type "Book" where the 2nd Param node has a Value of "John". So I would like to find all books where the authors frist name is "John".

Note that I am using .NET XPathDocument.

3 Answers 3

5

What about the requirement to have only the Item that are Books?

Try this:

/Item[@Type='Book'][Param[2][@Value='John']]
Sign up to request clarification or add additional context in comments.

Comments

2

Note that I am using .NET XPathDocument.

So limited to XPath V1.

You can include (relative and absolute) paths in a predicate. So something like:

//Item[@Type='Book'][./Param[2][@Value = 'John']]

(I would try and avoid "//", as it requires a search of the whole DOM, but can't provide a better axis without more context.)

1 Comment

Your expression is not limited to books.
0

The expression would be:

//Item/Param[2][@Value='John']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.