tags:

views:

537

answers:

2

How do I select elements that have two attributes?

I can do

@name | @area

which is "name or area", but how do I do "name and area"?

+6  A: 

Well, don't use @name | @area - it's not 'or', it's a join which happens to work incidentally.

For 'or', do foo[@name or @area]. For 'and', do foo[@name and @area].

alamar
I might call using "@name | @area" that way roundabout, but not incidental. Effectively, it says "does this expression return any results?" The answer of course is yes if either or both attributes are present. It's a reasonable alternative for expressing the same condition.
Evan Lenz
+2  A: 

XPath does provide you with an and operator which can be used to evaluate multiple conditions using predicates:

Possibles:

//MyElement[@name][@area]
//MyElement[@name and @area]
Cerebrus
Please don't do the former.
alamar
I don't, but only out of habit. Is there a valid reason not to?
Cerebrus
Well, because it's ugly? I can't think of a more valid reason, frankly :)
alamar
You have a sense of humour! :P
Cerebrus
Heh, I often prefer multiple predicates over explicit logical operators ("and"). I suppose it's a matter of taste. Depending on how complex the pattern or expression is, I'll also break it up into multiple lines and align one predicate above the next one.
Evan Lenz
A valid concern!
alamar