Using Jaxen XPath

Discussion in 'XML' started by Anna, Apr 4, 2004.

  1. Anna

    Anna Guest

    Hi all.
    I am using Jaxen to evaluate XPath expressions in Java.
    I encountered problems when comparing results returned by jaxen with
    results
    returned by other XPath implementation - I was using Saxon.
    Here is my example:
    <p>
    <span>text1</span>
    </p>
    <div>text2</div>

    I want to get the first preceding element of the div element.
    I apply the following XPath - //div/preceding::*[1]
    Saxon results in span element matched.
    Jaxen results in p element matched.

    What I want is the span element.

    From what I understood (I may be wrong), the difference in results is
    because
    Jaxen uses bredth first search, when saxon uses depth first search.

    I can maybe construct something more specific to match the element I
    want for Jaxen,but the problem is that my queries are already
    complicated, and adding a lot more predicates slows the whole
    evaluation drastically.

    So I wondered if there is any elegant way to match nodes in Jaxen by
    document order, in particular, match the closest preceding element to
    a context element.

    Sorry if this has been asked before, I've been searching for an answer
    for a while, and didn't find anything.

    Thank you very much for help.

    Anna
    Anna, Apr 4, 2004
    #1
    1. Advertising

  2. Anna wrote:


    > I am using Jaxen to evaluate XPath expressions in Java.
    > I encountered problems when comparing results returned by jaxen with
    > results
    > returned by other XPath implementation - I was using Saxon.
    > Here is my example:
    > <p>
    > <span>text1</span>
    > </p>
    > <div>text2</div>
    >
    > I want to get the first preceding element of the div element.
    > I apply the following XPath - //div/preceding::*[1]
    > Saxon results in span element matched.
    > Jaxen results in p element matched.
    >
    > What I want is the span element.
    >
    > From what I understood (I may be wrong), the difference in results is
    > because
    > Jaxen uses bredth first search, when saxon uses depth first search.


    I think that Jaxen is wrong here, it should yield the first preceding
    element in reverse document order.

    > I can maybe construct something more specific to match the element I
    > want for Jaxen,but the problem is that my queries are already
    > complicated, and adding a lot more predicates slows the whole
    > evaluation drastically.
    >
    > So I wondered if there is any elegant way to match nodes in Jaxen by
    > document order, in particular, match the closest preceding element to
    > a context element.


    Well, with Jaxen then using
    [last()]
    instead of
    [1]
    as the predicate should give you what you want but I think it is better
    to file a bug report on Jaxen, citing
    http://www.w3.org/TR/xpath#predicates
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Apr 4, 2004
    #2
    1. Advertising

  3. I think Jaxen is giving the wrong answer too, that's why I posted.
    Jaxen list seems to be not very active lately, that's why I first tried
    here. I'll post to them anyway then.
    By the way, for my example //div/preceding::*[last()] will return p, but
    for a full page, say
    <html>
    <head>
    </head>
    <body>
    <p>
    <span>text1</span>
    </p>
    <div>text2</div>
    </body>
    </html>

    this XPath in Jaxen will return head element as a match!

    Thank you for your help and advice.

    Anna

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Anna Afonchenko, Apr 4, 2004
    #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. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    1,955
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,366
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    506
  4. goog
    Replies:
    0
    Views:
    493
  5. Tjerk Wolterink

    XPath: efficiency in xpath expressions

    Tjerk Wolterink, Nov 13, 2004, in forum: XML
    Replies:
    1
    Views:
    1,609
    Richard Tobin
    Nov 13, 2004
Loading...

Share This Page