XPATH Multiple Element Filters
Solution 1
This is a very fundamental XPath feature: composing a number of conditions with the logical operators and
, or
, and the function not()
.
and
has a higher priority than or
and both operators have lower priority than the relational and equality operators (=
, !=
, >
, >=
, <
and <=
).
So, it is safe to write: A = B and C = D
Some most frequent mistakes made:
People write
AND
and/orOR
. Remember, XPath is case-sensitive.People use the
|
(union) operator instead ofor
Lastly, here is my solution:
/SavingAccounts/SavingAccount
[ServiceOnLine='yes' or ServiceViaPhone='yes']
Solution 2
/SavingAccounts/SavingAccount[(ServiceOnLine='yes') or (ServiceViaPhone='yes')]
Solution 3
Will
/SavingAccounts/SavingAccount[ServiceOnline/text()='yes' or ServiceViaPhone/text()='yes']
do the trick?
I have no XPath evaluator handy at the moment.
EDIT:
If I remember correctly, you don't need the text(), so
[ServiceOnline='yes' or ServiceViaPhone='yes']
should be sufficient, and more readable.
EDIT:
Yes, of course, 'or' for predicate expressions, my bad.
Related videos on Youtube
Alex Angas
Updated on July 09, 2022Comments
-
Alex Angas almost 2 years
I have the following sample XML structure:
<SavingAccounts> <SavingAccount> <ServiceOnline>yes</ServiceOnline> <ServiceViaPhone>no</ServiceViaPhone> </SavingAccount> <SavingAccount> <ServiceOnline>no</ServiceOnline> <ServiceViaPhone>yes</ServiceViaPhone> </SavingAccount> </SavingAccounts>
What I need to do is filter the 'SavingAccount' nodes using XPATH where the value of 'ServiceOnline' is 'yes' or the value of 'ServiceViaPhone' is yes.
The XPATH should return me two rows!! I can filter 'SavingAccount' nodes where both of the element values are yes like the following XPATH sample, but what I want to do is an or element value comparison???
/SavingAccounts/SavingAccount/ServiceOnline[text()='yes']/../ServiceViaPhone[text()='yes']/..
-
gilles27 over 15 yearsSo the XPATH was correct, except you need "or" instead of "|". But that might be a peculiarity of the .NET XML/XPATH parser.
-
Jon L. over 10 years@DimitreNovatchev, you mean, the parens?
-
Dimitre Novatchev over 10 years@JonL. Yes, aren't "brackets" and "parentheses" synonyms?
-
Jon L. over 10 years@DimitreNovatchev, when you're having to differentiate between ( and [, then using the term bracket to refer to both would be misleading/confusing.
-
Dimitre Novatchev over 10 years@JonL., I am using explicitly the term "square brackets", so there cannot be any confusion.
-
Jon L. over 10 years@DimitreNovatchev, specifying square brackets would solve it indeed, although then I wonder why square brackets wouldn't be needed in the above example? (Assuming I understand correctly)
-
Dimitre Novatchev over 10 years@JonL., I am actually saying that normal, non-square brackets aren't needed.
-
Darryl about 9 years@DimitreNovatchev, most native English speakers do not consider "brackets" and "parentheses" to be synonyms. I would certainly never call parentheses brackets. Like others, I was confused by your initial comment, especially since "brackets" without a qualifier is usually understood to mean square braces.
-
Dimitre Novatchev about 9 years@Darryl, Thanks for the English lesson :)