Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Specifying predicate queries with XPath?

Reply
Thread Tools

Specifying predicate queries with XPath?

 
 
Duncan Smith
Guest
Posts: n/a
 
      03-21-2007
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.

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      03-21-2007
Duncan Smith wrote:
> 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]

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
roy axenov
Guest
Posts: n/a
 
      03-21-2007
On Mar 21, 7:45 pm, "Duncan Smith"
<(E-Mail Removed)> wrote:
> 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?

--
roy axenov

 
Reply With Quote
 
Duncan Smith
Guest
Posts: n/a
 
      03-22-2007

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


That's great, thanks.

 
Reply With Quote
 
Duncan Smith
Guest
Posts: n/a
 
      03-22-2007

> > <something>
> > <number value="2"/>
> > <something/>

>
> 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.

 
Reply With Quote
 
p.lepin@ctncorp.com
Guest
Posts: n/a
 
      03-22-2007
On Mar 22, 11:18 am, "Duncan Smith"
<(E-Mail Removed)> wrote:
> 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.

--
Pavel Lepin

 
Reply With Quote
 
Joseph Kesselman
Guest
Posts: n/a
 
      03-22-2007
>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.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
 
Reply With Quote
 
Joseph Kesselman
Guest
Posts: n/a
 
      03-22-2007
Duncan Smith wrote:
> 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...


--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XPATH Predicate Syntax question Sabba XML 0 05-09-2006 10:48 AM
xslt queries in xml to SQL queries Ian Roddis Python 3 02-26-2006 06:49 PM
get position after spec. predicate awebguynow XML 1 01-27-2006 08:42 AM
so many queries within queries I'm confused Abby Lee ASP General 11 08-06-2004 07:56 PM
Predicate by attribute? Jacques XML 2 06-24-2004 11:45 AM



Advertisments