7

I have 2 elements with same attribute but with different child node values. Can I query to find a specific element which matches the attribute and also the child node value. To be specific, this is the sample xml i am using to query(each element in original xml has more than 10 childe 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 which has the @size = "2" and Title = xyz.

Is this possible by using SelectSingleNode method? If not how to query this?

Thanks

Wayne
  • 59,728
  • 15
  • 131
  • 126
anamik
  • 73
  • 1
  • 1
  • 5
  • possible duplicate of [Xpath expression with multiple predicates](http://stackoverflow.com/questions/568713/xpath-expression-with-multiple-predicates) – Don Roby Apr 19 '11 at 21:55

2 Answers2

18

This:

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

Or this:

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

Note that the use of // is discouraged when your schema is known.

Wayne
  • 59,728
  • 15
  • 131
  • 126
1

Does this work?

//Book[@size='2']//Title[text() = "xyz"]/..
Steve Wellens
  • 20,506
  • 2
  • 28
  • 69