XPath: alternatives in node-test?

K

kj

Suppose I wanted to select all the element nodes whose tags were
either foo or bar. One way would be to join the results of //Foo
and //bar. Is there a way to achieve the same with a single
expression that included the two alternatives? Something like
(pushing the analogy with Unix shell conventions) //{foo,bar} ?

Thanks,

kj
 
K

kj

In said:
//*[self::foo or self::bar]


Boy, you're fast!!!

Right after I posted it occurred to me to search for one more thing
in the recommendations page, namely the string "'|'" (minus the
enclosing double-quotes). I found about "UnionExpr"; so this also
works:

//foo|//bar

Thanks,

kj
 
D

Dimitre Novatchev [MVP XML]

kj said:
In <[email protected]> "Dimitre Novatchev [MVP
XML] said:

//*[self::foo or self::bar]


Boy, you're fast!!!

Right after I posted it occurred to me to search for one more thing
in the recommendations page, namely the string "'|'" (minus the
enclosing double-quotes). I found about "UnionExpr"; so this also
works:

//foo|//bar

Yes, but I understood your question as if you wanted the "//" abbreviation
to be used only once.

Also, do note that some not smart XPath engines will evaluate:
//*[self::foo or self::bar]

twice as fast as;

//foo | //bar


Cheers,

Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top