XPath filters equivalence

A

Altu

Hi,

Is [p1][p2][p3] the same as [p1 AND p2 and p3] in XPath?

Also if I have a predicate that starts with "/" or "//", does it
matter where in the XPath expression it is?

For example, are these equal:

/a[/a/b]/c/d
/a/c[/a/b]/d
/a/c/d[/a/b]

or these three:

/a[//a/b]/c/d
/a/c/[//a/b]/d
/a/c/d[//a/b]

Thanks in advance,
Al
 
J

Joe Kesselman

Altu said:
Is [p1][p2][p3] the same as [p1 AND p2 and p3] in XPath?

Depends on the predicate. If p2 or p3 tests position() or last(), _NO_
-- the latter form would test position in the original set, while the
former would test position in the set that has gotten past the
preceeding filter(s).
Also if I have a predicate that starts with "/" or "//", does it
matter where in the XPath expression it is?
/a[/a/b]/c/d
/a/c[/a/b]/d
/a/c/d[/a/b]

In these examples: no, since an absolute XPath by definition ignores its
context and since if it fails in any of these the result is that we find
no nodes. (Though for performance reasons in typical processors, you
will probably want to test it earlier rather than later).

In more complicated XPaths, maybe.
 
A

Altu

Hi Joe,
Depends on the predicate. If p2 or p3 tests position() or last(), _NO_
-- the latter form would test position in the original set, while the
former would test position in the set that has gotten past the
preceeding filter(s).

p1, p2, and p3 are simple paths that only have ., /, //, and * like

./a/b//c/d//*

or

//a/b//c//*
In these examples: no, since an absolute XPath by definition ignores its
context and since if it fails in any of these the result is that we find
no nodes. (Though for performance reasons in typical processors, you
will probably want to test it earlier rather than later).
In more complicated XPaths, maybe.

Other than performance difference, if my predicates are also simple
predicates like the case above, so I think they all return the same
result, is this correct?

Regards,
Al
 
P

Peter Flynn

Altu said:
Hi Joe,


p1, p2, and p3 are simple paths that only have ., /, //, and * like

./a/b//c/d//*

or

//a/b//c//*


Other than performance difference, if my predicates are also simple
predicates like the case above, so I think they all return the same
result, is this correct?

At this stage, why not download a copy of XPathTester and try it for
yourself?

///Peter
 
A

Altu

Hi again,

I just had a look at XPathTester. Looks like it is not
capable of finding out if two different queries are equivalent
or not, no matter what XML file they operate on.

-Al
 
D

David Carlisle

Altu said:
Hi,

Is [p1][p2][p3] the same as [p1 AND p2 and p3] in XPath?

as others have said [][] isn't the same as and if any of the predicates
use last() or position()
Also if I have a predicate that starts with "/" or "//", does it
matter where in the XPath expression it is?

For example, are these equal:

/a[/a/b]/c/d
/a/c[/a/b]/d
/a/c/d[/a/b]

If a b and c are just element names then the above are all equal and
equivalent to the xpath2
if(/a/b) then /a/c/d else ()
but if a b c d are standing for arbitrary XPath steps then these may not
be equivalent as / depends on the current context as it means the
document node ancestor of the _current node_.
so for example the xpath2
/a[/a/b]/doc('c'xml')/d
the predicate [/a/b] tests in the current docuement
/a/doc('c'xml')[/a/b]/d
here [/a/b] tests in c.xml

David


or these three:

/a[//a/b]/c/d
/a/c/[//a/b]/d
/a/c/d[//a/b]

Thanks in advance,
Al
 
P

Peter Flynn

Altu said:
I just had a look at XPathTester. Looks like it is not capable of
finding out if two different queries are equivalent or not, no matter
what XML file they operate on.

I'm a little surprised that you would expect it to.

It's a tool for identifying the node referenced by a given XPath
statement (that is, what node or nodes, or none, a given XPath statement
will reference, for a given document instance).

To compare two XPath statements, you'd need some kind of comparative
parser or tracer, which would identify the nodes referenced by each
segment of the statement until the end of the statement was reached, or
failure occurred. I'm not aware of any such tool, although it may well
exist.

///Peter
 
P

Phil Fearon

Peter said:
To compare two XPath statements, you'd need some kind of >comparative
parser or tracer, which would identify the >nodes referenced by each
segment of the statement until >the end of the statement was reached, or
failure >occurred. I'm not aware of any such tool, although it may >well
exist.

SketchPath is such an XPath tool that includes a 'tracer' to identify
the nodes reference by each statement segment as you describe.

There may be other such tools but I haven't come across one myself.

Also, because SketchPath allows XPath expressions to be reused as
$variables within other XPath expressions it would be possible to write
one XPath expression that shows the intersect (or inverse) of two other
XPath referenced expressions.

SketchPath (a .NET application) is in late Beta, and is available for
free download from http://www.sketchpath.com if you want to give it a
try.

Phil Fearon
 

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
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top