Specifying predicate queries with XPath?

Discussion in 'XML' started by Duncan Smith, Mar 21, 2007.

  1. Duncan Smith

    Duncan Smith Guest

    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.
    Duncan Smith, Mar 21, 2007
    #1
    1. Advertising

  2. 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/
    Martin Honnen, Mar 21, 2007
    #2
    1. Advertising

  3. Duncan Smith

    roy axenov Guest

    On Mar 21, 7:45 pm, "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/>


    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
    roy axenov, Mar 21, 2007
    #3
  4. Duncan Smith

    Duncan Smith Guest


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


    That's great, thanks.
    Duncan Smith, Mar 22, 2007
    #4
  5. Duncan Smith

    Duncan Smith Guest


    > > <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.
    Duncan Smith, Mar 22, 2007
    #5
  6. Duncan Smith

    Guest

    On Mar 22, 11:18 am, "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 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
    , Mar 22, 2007
    #6
  7. >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
    Joseph Kesselman, Mar 22, 2007
    #7
  8. 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
    Joseph Kesselman, Mar 22, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sabba
    Replies:
    0
    Views:
    626
    Sabba
    May 9, 2006
  2. Replies:
    1
    Views:
    473
    Martin Honnen
    Feb 24, 2007
  3. Ziphims

    XPath predicate problem

    Ziphims, Jan 11, 2008, in forum: XML
    Replies:
    6
    Views:
    1,769
    P. Lepin
    Jan 12, 2008
  4. Wabiloo
    Replies:
    1
    Views:
    827
    Martin Honnen
    Jan 25, 2008
  5. Phantom
    Replies:
    5
    Views:
    755
Loading...

Share This Page