xpath: predicate to choose only elements that match a one of a list of values

Discussion in 'XML' started by Phantom, Jul 3, 2008.

  1. Phantom

    Phantom Guest

    I totally need the help of the XML experts up here, I can't figure this
    one out, I've been working on it for hours today, googling my brains
    out.

    I need to write an xpath query string that returns one or more specific
    elements on the fly, in this example, apple and/or orange. *this* works
    for JUST apple:

    select xpath_eval('//apple',
    xtree_doc('<fruit><apple>7</apple><orange>11</orange><banana
    /></fruit>'))


    BUT, how do I match on not just //apple but also //orange ?? (I have to
    accomplish this with an XPATH, not a complex XQUERY 'when' clause
    unfortunately.)


    I even had it convoluted to this working version:

    select xpath_eval('//*[local-name()=("apple")]',
    xtree_doc('<fruit><apple>7</apple><orange>7</orange><banana
    /></fruit>'))

    .... hoping I could then try to add orange with a pipe (|) operator:

    local-name()=("apple"|"orange") ... no dice.


    Both the source and the match value are coming in at run time, so a
    series of OR clauses in the predicate won't work:

    noGoExample: [local-name()="apple" or [local-name()="orange"]



    I KNOW there's a simple representation for this, but just try looking
    for it online when you don't know what you're looking for. like looking
    for a word in the dictionary you don't know how to spell.


    I thought of using an XML set, something like this:
    [local-name() in {"apple|orange"}]

    .... but none of the dozen combinations of syntax and quoted variations worked.

    I AM LOSING MY MIND!

    any help folks? save my mind, thanks!
    p.
     
    Phantom, Jul 3, 2008
    #1
    1. Advertising

  2. * Phantom wrote in comp.text.xml:
    >I need to write an xpath query string that returns one or more specific
    >elements on the fly, in this example, apple and/or orange. *this* works
    >for JUST apple:
    >
    >select xpath_eval('//apple',


    So use `//apple | //orange`.

    >Both the source and the match value are coming in at run time, so a
    >series of OR clauses in the predicate won't work:
    >
    >noGoExample: [local-name()="apple" or [local-name()="orange"]


    Well here you have more [ than ] which usually is a syntax error. Use

    //*[local-name() = 'apple' or local-name() = 'orange']

    >I KNOW there's a simple representation for this, but just try looking
    >for it online when you don't know what you're looking for. like looking
    >for a word in the dictionary you don't know how to spell.


    http://www.w3.org/TR/xpath has many examples, and contains everything
    there is to know about XPath 1.0.
    --
    Björn Höhrmann · mailto: · http://bjoern.hoehrmann.de
    Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
    68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
     
    Bjoern Hoehrmann, Jul 3, 2008
    #2
    1. Advertising

  3. Phantom

    Phantom Guest

    On 2008-07-03 02:16:28 -0700, Bjoern Hoehrmann <> said:

    > * Phantom wrote in comp.text.xml:
    >> I need to write an xpath query string that returns one or more specific
    >> elements on the fly, in this example, apple and/or orange. *this* works
    >> for JUST apple:
    >>
    >> select xpath_eval('//apple',

    >
    > So use `//apple | //orange`.


    nope, that only returns the first hit for me (apple).

    >
    >> Both the source and the match value are coming in at run time, so a
    >> series of OR clauses in the predicate won't work:
    >>
    >> noGoExample: [local-name()="apple" or [local-name()="orange"]

    >
    > Well here you have more [ than ] which usually is a syntax error. Use
    >
    > //*[local-name() = 'apple' or local-name() = 'orange']


    I mean, it would function, but I can't go there... there might be 20
    fields posted, I need something more straightforward than assembling an
    OR predicate each time I call the xpath.

    >
    >> I KNOW there's a simple representation for this, but just try looking
    >> for it online when you don't know what you're looking for. like looking
    >> for a word in the dictionary you don't know how to spell.

    >
    > http://www.w3.org/TR/xpath has many examples, and contains everything
    > there is to know about XPath 1.0.


    thanks for the lead, but they don't cover this example, and they don't
    provide much in the way of examples.
     
    Phantom, Jul 3, 2008
    #3
  4. Phantom

    Phantom Guest

    On 2008-07-03 02:53:18 -0700, "szomiz"
    <> said:

    > Uzytkownik "Phantom" <> napisal w wiadomosci
    > news:2008070216590916807-phantom@examplecom...
    >
    >> local-name()=("apple"|"orange") ... no dice.

    >
    > contains(" apple, orange,",concat(" ",local-name(),","))
    >
    > ;>
    >
    > sz.


    slick 8^) however, won't this also match on crabapple and applebys?
     
    Phantom, Jul 3, 2008
    #4
  5. Re: xpath: predicate to choose only elements that match a one ofa list of values

    Phantom wrote:
    > slick 8^) however, won't this also match on crabapple and applebys?


    Nope. Note that he's concatenating a few non-name characters (space and
    comma) before doing the test.

    However, I *really* hate relying on testing localname and losing proper
    namespace-awareness. Bad habit to get into.


    If you really want to do it via a predicate, I'd suggest
    "//*[self::apple or self::eek:range]"
    which will walk the tree and return only those nodes which are apple or
    orange elements.

    However,
    "//apple | //orange"
    really should work. XPath defines the '|' character as the union
    operator. This expression should return every node that is either an
    apple or an orange. You say you're only getting the first hit -- but I
    think that means either you're misusing your XPath API by failing to
    retrieve results after the first, or it's broken and you should consider
    switching implementations. (You didn't show us the surrounding code, and
    I don't recognize the syntax, so I can't say much beyond that.)
     
    Joseph J. Kesselman, Jul 4, 2008
    #5
  6. Phantom

    Guest

    Re: xpath: predicate to choose only elements that match a one of alist of values

    On Jul 3, 4:38 pm, "Joseph J. Kesselman" <>
    wrote:
    > However,
    >         "//apple | //orange"
    > really should work. XPath defines the '|' character as the union
    > operator. This expression should return every node that is either an
    > apple or an orange. You say you're only getting the first hit -- but I
    > think that means either you're misusing your XPath API by failing to
    > retrieve results after the first, or it's broken and you should consider
    > switching implementations. (You didn't show us the surrounding code, and
    > I don't recognize the syntax, so I can't say much beyond that.)



    The problem here is that the xpath_eval statement defaults a third
    parameter which indicates that you only want the FIRST node returned.
    From the Virtuoso Documentation....


    xpath_eval (in xpath_expression varchar, in xml_tree XML Entity, [in
    index integer], [in named_params vector]);

    Description
    This function returns the result of applying the XPath expression to
    the context node. By default only the first result is returned, but
    supplying a third argument allows you to specify an index for the
    value; the default assumes a value of 1 here. A value of 0 returns an
    array of 0 or more elements, one for each value selected by the XPath
    expression.


    Always pays to check the documentation!!


    Nick
     
    , Jul 7, 2008
    #6
    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:
    638
    Sabba
    May 9, 2006
  2. Replies:
    1
    Views:
    486
    Martin Honnen
    Feb 24, 2007
  3. Duncan Smith
    Replies:
    7
    Views:
    497
    Joseph Kesselman
    Mar 22, 2007
  4. Ziphims

    XPath predicate problem

    Ziphims, Jan 11, 2008, in forum: XML
    Replies:
    6
    Views:
    1,790
    P. Lepin
    Jan 12, 2008
  5. Wabiloo
    Replies:
    1
    Views:
    849
    Martin Honnen
    Jan 25, 2008
Loading...

Share This Page