Using Jaxen XPath

A

Anna

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
 
M

Martin Honnen

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
 
A

Anna Afonchenko

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top