How to select two attributes from the same node with one expression in XPath?
81,232
Try this:
//person[@id='abc123']/@*[name()='weight' or name()='haircolor']
If you're using an XPath 2.0 processor, you may also use a prettier option:
//person[@id='abc123']/(@haircolor|@weight)`
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on July 09, 2022Comments
-
Admin almost 2 years
For example:
//person[@id='abc123']/@haircolor|/@weight"
PS. there are lots of "person" records
-
Michael Kay about 13 years
//person[@id='abc123']/(@haircolor|@weight)
is valid XPath - but you need an XPath 2.0 processor to run it. -
Kobi about 13 years@Michael - Thanks, good to know! Unfortunately, I usually deal with the older version. Anyway, I'll take your word for it and update the answer. Thanks again.
-
Vilius about 11 years@Kobi - is it also possible to concatinate the output? Because running my XPath expression based on your answer leads me to a double output.
-
Kobi about 11 years@Vilius - It shouldn't duplicate anything, as far as I know. I'm not sure what you mean by "concatinate"... It is probably best is you ask a new question with the exact the details.