XPath - context node evaluation problem

Discussion in 'XML' started by Thomas Schmidt, Jan 9, 2007.

  1. Given this document:

    <doc>
    <head>
    <someElementWithID id="X"/>
    <someElementWithID id="Y"/>
    </head>
    <body>
    <element>
    <otherElement>
    <elementWithIDREF idref="X"/>
    </otherElement>
    </element>
    </body>
    </doc>

    With the <elementWithIDREF> as a context node, I want to use an XPath
    to get to the <someElementWithID> with the corresponding id-attribute.
    If I use

    //head/someElementWithID[@id=@idref]

    I don't get what I want, because the idref-attribute is evaluated with
    someElementWithID as the context. In XSLT, I use

    //head/someElementWithID[@id=current()/@idref]

    But "current()" is an XSLT function. Is there some way to do this in
    "pure" XPath?

    Any help will be greatly appreciated,

    Thomas
     
    Thomas Schmidt, Jan 9, 2007
    #1
    1. Advertising

  2. hi,

    Thomas Schmidt wrote:
    >
    > //head/someElementWithID[@id=current()/@idref]
    >
    > But "current()" is an XSLT function. Is there some way to do this in
    > "pure" XPath?


    The XPath context consist on a node, a pair of integer (size and
    position), a set of namespace bindings, a set of functions, and a set of
    variables

    If you dont' want to augment the set of functions, you can use a
    variable instead :

    //head/someElementWithID[@id=$current/@idref]

    but you have to set $current before, that depends on the XPath engine
    and the host language you use

    --
    Cordialement,

    ///
    (. .)
    --------ooO--(_)--Ooo--------
    | Philippe Poulard |
    -----------------------------
    http://reflex.gforge.inria.fr/
    Have the RefleX !
     
    Philippe Poulard, Jan 9, 2007
    #2
    1. Advertising

  3. * Thomas Schmidt wrote in comp.text.xml:
    >I don't get what I want, because the idref-attribute is evaluated with
    >someElementWithID as the context. In XSLT, I use
    >
    >//head/someElementWithID[@id=current()/@idref]
    >
    >But "current()" is an XSLT function. Is there some way to do this in
    >"pure" XPath?


    Well, can you use the id() function, or variables, or insert the value
    into the expression before you have it evaluated? Otherwise there is no
    way, you could only check whether there is some such element using

    //head/someElementWithID/@id = @idref

    which is not what you want.
    --
    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, Jan 9, 2007
    #3
  4. If you can uniquely select the element with the "idref" attribute" then use
    the following expression:

    //someElementWithID[@id=$the-id-ref-elemennt-selecting-expression-here/@ideref]

    for example, for the provided xml document this could be as simple as:

    //someElementWithID[@id = /*/*/*/elementWithIDREF/@idref]


    Cheers,
    Dimitre Novatchev


    "Thomas Schmidt" <> wrote in message
    news:...
    > Given this document:
    >
    > <doc>
    > <head>
    > <someElementWithID id="X"/>
    > <someElementWithID id="Y"/>
    > </head>
    > <body>
    > <element>
    > <otherElement>
    > <elementWithIDREF idref="X"/>
    > </otherElement>
    > </element>
    > </body>
    > </doc>
    >
    > With the <elementWithIDREF> as a context node, I want to use an XPath
    > to get to the <someElementWithID> with the corresponding id-attribute.
    > If I use
    >
    > //head/someElementWithID[@id=@idref]
    >
    > I don't get what I want, because the idref-attribute is evaluated with
    > someElementWithID as the context. In XSLT, I use
    >
    > //head/someElementWithID[@id=current()/@idref]
    >
    > But "current()" is an XSLT function. Is there some way to do this in
    > "pure" XPath?
    >
    > Any help will be greatly appreciated,
    >
    > Thomas
    >
     
    Dimitre Novatchev, Jan 9, 2007
    #4
  5. Thomas Schmidt wrote:

    > With the <elementWithIDREF> as a context node, I want to use an XPath
    > to get to the <someElementWithID> with the corresponding id-attribute.
    > If I use
    >
    > //head/someElementWithID[@id=@idref]
    >
    > I don't get what I want, because the idref-attribute is evaluated with
    > someElementWithID as the context. In XSLT, I use
    >
    > //head/someElementWithID[@id=current()/@idref]
    >
    > But "current()" is an XSLT function. Is there some way to do this in
    > "pure" XPath?
    >
    > Any help will be greatly appreciated,
    >
    > Thomas
    >


    (in xslt its usually better to use a key rather than
    /head/someElementWithID[@id=current()/@idref] not that that's relevant
    to the problem here)

    i suspect it's not possible in xpath1 unless you bind a variable in an
    external language. or as Dimitre suggested, know a path to your
    referencing node.

    if xpath2 is a possibility, then you could use

    for $id in @idref return
    //head/someElementWithID[@id=$id]

    David
     
    David Carlisle, Jan 10, 2007
    #5
    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. Replies:
    0
    Views:
    1,648
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,522
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Ilias Lazaridis
    Replies:
    2
    Views:
    406
    Ilias Lazaridis
    Apr 24, 2005
  4. Tjerk Wolterink
    Replies:
    2
    Views:
    1,506
    Dimitre Novatchev
    Aug 24, 2006
  5. Tim Hallwyl

    Context Node in XPath 1.0

    Tim Hallwyl, Nov 11, 2007, in forum: XML
    Replies:
    9
    Views:
    935
    Tim Hallwyl
    Nov 25, 2007
Loading...

Share This Page