tags:

views:

78

answers:

5

I have the following XML:

<users>
 <user>
  <location>ny</location>
  <status>1</status>
 </user>
 <user>
  <location>nj</location>
  <status>1</status>
 </user>
 <user>
  <location>pa</location>
  <status>50</status>
 </user>
</users>

What I actually need is all users in ny or nj who also have a status of 1.

Based on the previous answers and some additional Googling I came up with:

/users/user[location='ny' and status='1']|/users/user[location='nj' and status='1'

Is there a less verbose to write this? i.e. more along the lines of:

/users/user[location='ny' | 'pa']  and /users/user[status='1']
+3  A: 

The XPath you're looking for is:

/users/user[location='ny']|/users/user[status='1']

You can test it here:

http://www.futurelab.ch/xmlkurs/xpath.en.html

PS: Your XML above is incorrect (status is incorrectly closed with a height tag)

Paul Mason
XPath for the second version of the question: /users/user[(location='ny' or location='nj') and status=1]
Paul Mason
+1  A: 

The following would also work:

    /users/user[location='ny' | status='1']
bryanbcook
This XPath does not work - at least .Net
druta
A: 

//user[location='ny' | status='1'] //.[location='ny' | status='1']”

I'd have made location and status as attributes of and fished for those instead. They're just wasting space as they are. Also, consider for your text if you keep the current structure.

inked
A: 

(moved to original question)

druta
You could do: /users/user[(location='ny' or location='nj') and status=1]
Paul Mason
Perfect - this is exactly what I needed. Thank you very much!
druta
Please post it an answer so I can accept it.
druta
Moved the comment into the original answer so you can mark that :) Thanks.
Paul Mason
A: 

It should be

  /users/user[(location='ny' or location='nj') and (status = '1') ]

I don't think '|' is valid in predicate, as it represents union expression in XPath syntax

vtd-xml-author