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)`
Share:
81,232
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    For example:

    //person[@id='abc123']/@haircolor|/@weight"
    

    PS. there are lots of "person" records

  • Michael Kay
    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
    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
    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
    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.