Specifying predicate queries with XPath?

D

Duncan Smith

Suppose I have the following XML doc:

<root>
<tag id="1">
<number value="1"/>
</tag>
<tag id="2">
<something/>
</tag>
<tag id="3">
<something>
<number value="2"/>
<something/>
</tag>
</root>

I'm interested (a) 'tags that have a number element whose value
attribute > 0' and also (b) 'tags that have a something element that
have a number element whose value > 0'

I could run an x-path query for either (a) or (b), i.e. for (a):

'/root/tag[number/@value> 0]'

but what I really want is to somehow OR the two together so I end up
with:

<root>
<tag id="1">
<number value="1"/>
</tag>
<tag id="3">
<something>
<number value="2"/>
<something/>
</tag>
</root>

Can't seem to find the right syntax, any pointers?

Many thanks,

Duncan.
 
M

Martin Honnen

Duncan said:
Suppose I have the following XML doc:

<root>
<tag id="1">
<number value="1"/>
</tag>
<tag id="2">
<something/>
</tag>
<tag id="3">
<something>
<number value="2"/>
<something/>
</tag>
</root>

I'm interested (a) 'tags that have a number element whose value
attribute > 0' and also (b) 'tags that have a something element that
have a number element whose value > 0'

/root/tag[number/@value > 0 or something/number/@value > 0]
 
R

roy axenov

Suppose I have the following XML doc:

<root>
<tag id="1">
<number value="1"/>
</tag>
<tag id="2">
<something/>
</tag>
<tag id="3">
<something>
<number value="2"/>
<something/>

That's not well-formed and therefore is not XML. Are you
sure you want help?
</tag>
</root>

I'm interested (a) 'tags that have a number element whose
value attribute > 0' and also (b) 'tags that have a
something element that have a number element whose
value > 0'

I could run an x-path query for either (a) or (b), i.e.
for (a):

'/root/tag[number/@value> 0]'

but what I really want is to somehow OR the two together

Naturally, you should use 'or'.

/root/tag
[number/@value > 0 or something/number/@value > 0]
Can't seem to find the right syntax, any pointers?

Have you tried reading the docs?
 
D

Duncan Smith

That's not well-formed and therefore is not XML. Are you
sure you want help?

Sorry about the typo, I will validate before sending in the future.
Naturally, you should use 'or'.
Have you tried reading the docs?

I was trying 'or', but my mistake was in starting the 2nd predicate
from root again rather than being relative to the opening bracket.

Yes, I'd tried reading the docs before posting - the XPath
specification from w3c.org, but to be honest, it's a little heavy
going in places without any examples... Maybe you know of a more
digestible document?

Many thanks,

Duncan.
 
P

p.lepin

Yes, I'd tried reading the docs before posting - the
XPath specification from w3c.org, but to be honest, it's
a little heavy going in places without any examples...

It certainly is that, and the Specs are quite unusable as
introductory reading for us mere mortals, but in all
fairness to W3C, it is normally possible to fish out the
information you need on a specific question out of their
specs (and being able to do that is a very valuable skill
to acquire).
Maybe you know of a more digestible document?

For my needs I normally use either the W3C specs or
reference materials on developer.mozilla.org (note that
those wouldn't've helped you solve your original problem,
for example, since there's no info on XPath operators there
that I'm aware of). Basically, where operators are
concerned, use your common sense with a few caveats (div
instead of /, because / is used differently in XPath, '|'
and 'or' being two vastly different things etc.)

Since this is the Net, there's a lot of reference and
tutorial materials freely available. GIYF. Maybe that's
just me, but stay away from w3schools.
 
J

Joseph Kesselman

Maybe you know of a more digestible document?

For XSLT, the best hardcopy book I've seen is still Mike Kay's. In
softcopy... standard pointer to material at http://www.ibm.com/xml,
among many others.
 
J

Joseph Kesselman

Duncan said:
Yes, I'd tried reading the docs before posting - the XPath
specification from w3c.org, but to be honest, it's a little heavy
going in places

The W3C specs are written by experts for experts, and are not generally
recommended as a starting place for anyone who doesn't have the
background and patience needed to chase implications back and forth
through the document. Which is why tutorials get written...
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top