Parallel data traversal

Discussion in 'XML' started by Vojta, Sep 10, 2007.

  1. Vojta

    Vojta Guest

    Hi,

    I have some xml data looking like this
    <root>
    <items1>
    <i1>..</i1>
    <i1>..</i1>
    </items1>
    <items2>
    <i2>..</i2>
    <i2>..</i2>
    </items2>
    </root>

    If my context node is the k-th node in items1 is to possible to create
    XPath that finds a k-th node in items2? Something like "../../
    items2[position() = position(.)]" - which does not work because "." is
    bound to some "i2" node inside the square brackets.

    Some motivation: we use a "repeater" component that traverses child
    nodes of one node. The current node in the repeater is the context
    node. The user then uses xpaths relative to the current node to get
    the data. And I need some hack to be able to reference nodes at the
    same position but inside a different node.

    Obviously the most natural solution is to do some data preprocessing
    to "zip" items1 and items2 together or modify our repeater to be able
    to have multiple context nodes. But isn't there any pure xpath way to
    do this?

    Thanks
    Vojta, Sep 10, 2007
    #1
    1. Advertising

  2. Vojta

    Pavel Lepin Guest

    Vojta <> wrote in
    <>:
    > <root>
    > <items1>
    > <i1>..</i1>
    > <i1>..</i1>
    > </items1>
    > <items2>
    > <i2>..</i2>
    > <i2>..</i2>
    > </items2>
    > </root>
    >
    > If my context node is the k-th node in items1 is to
    > possible to create XPath that finds a k-th node in items2?
    > Something like "../../ items2[position() = position(.)]" -
    > which does not work because "." is bound to some "i2" node
    > inside the square brackets.
    >
    > Obviously the most natural solution is to do some data
    > preprocessing to "zip" items1 and items2 together or
    > modify our repeater to be able to have multiple context
    > nodes.


    That is the most natural solution indeed. And it's a trivial
    transformation, too.

    > But isn't there any pure xpath way to do this?


    Short answer: no, there isn't any pure XPath way of doing
    that. You need either variables (that is, you have to
    generate the XPath expression on the fly, it cannot be
    static), or XPath extensions (XSLT defines the current()
    function which returns the current node; but the notion of
    the current node is defined by XSLT spec as well, and isn't
    by itself meaningful outside of XSLT context).

    --
    This chickenus crossed the roadus while yodelingus.
    Pavel Lepin, Sep 10, 2007
    #2
    1. Advertising

  3. Vojta wrote:
    > Hi,
    >
    > I have some xml data looking like this
    > <root>
    > <items1>
    > <i1>..</i1>
    > <i1>..</i1>
    > </items1>
    > <items2>
    > <i2>..</i2>
    > <i2>..</i2>
    > </items2>
    > </root>
    >
    > If my context node is the k-th node in items1 is to possible to create
    > XPath that finds a k-th node in items2? Something like "../../
    > items2[position() = position(.)]" - which does not work because "." is
    > bound to some "i2" node inside the square brackets.
    >
    > Some motivation: we use a "repeater" component that traverses child
    > nodes of one node. The current node in the repeater is the context
    > node. The user then uses xpaths relative to the current node to get
    > the data. And I need some hack to be able to reference nodes at the
    > same position but inside a different node.
    >
    > Obviously the most natural solution is to do some data preprocessing
    > to "zip" items1 and items2 together or modify our repeater to be able
    > to have multiple context nodes. But isn't there any pure xpath way to
    > do this?
    >
    > Thanks
    >


    not pure xpath1 you need xslt1 or a similar host language that can bind
    xpath variables.

    however in xpath2 you can express this, for example

    for $p in 1+count(preceding-sibling::i1) return
    .../preceding-sibling::items2/i2[$p]

    David
    --
    http://dpcarlisle.blogspot.com
    David Carlisle, Sep 10, 2007
    #3
    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. Wynan James
    Replies:
    1
    Views:
    1,584
    Miguel De Anda
    Oct 6, 2003
  2. Big Jim

    Fous Traversal Policy

    Big Jim, Jan 27, 2006, in forum: Java
    Replies:
    0
    Views:
    402
    Big Jim
    Jan 27, 2006
  3. Soren
    Replies:
    4
    Views:
    1,255
    c d saunter
    Feb 14, 2008
  4. Vivek Menon
    Replies:
    5
    Views:
    3,344
    Paul Uiterlinden
    Jun 8, 2011
  5. Vivek Menon
    Replies:
    0
    Views:
    1,763
    Vivek Menon
    Jun 10, 2011
Loading...

Share This Page