Hi Dane

Actually I applied this as a bug fix, thinking it was now doing the right
thing ;-).

Basically if an XPath expression starts with a "/" then I think its meant to
start at the document, if it doesn't start with a "/" then the path starts
with the current node. i.e. absolute addressing should start at the
document, to differentiate itself from relative addressing that doesn't.

I'll try explain in more detail in place...

From: "Dane Foster" <[EMAIL PROTECTED]>
> Hello all.  I have already sent this message to James but since I've
> committed myself to dom4j I've joined the mailing list and sending it to
> everyone.  Anyway, I think a bug has been introduced into the .4 release
of
> dom4j.  Prior to .4, if you made a selection from an Element using an
XPath
> xpression, the xpression evaluated with the Element as the current context
> node.  That no longer works.  I think the expression is being evaluated
> using the root node as the context node.  For example:
>
> /* XML snippet */
> <root>
>  <element1>
>   <element2>some data</element2>
>  </element1>
> </root>
>
>
> /* Java snippet */
> Element root = document.getRootElement();
>
>  // This no longer works
> Element element2 = (Element)root.selectSingleNode( "/element1/element2" );

If you want to navigate relative to yourself then you could use relative or
absolute addressing as:-

// using relative addressing from 'root'
Element element2 = (Element)root.selectSingleNode( "element1/element2" );

// using absolute addressing from 'document'
Element element2 = (Element)root.selectSingleNode(
"/root/element1/element2" );
Element element2 = (Element)root.selectSingleNode( "//element1/element2" );

> // This works
> Element element3 = (Element)root.selectSingeNode( "//element1/element2" );

> I went back and downloaded dom4j-0.3 and changed my classpath.  I re-ran
my
> program and it works as expected to.  So I'm sure that the bug has
something
> to do with the .4 release.  Any help is appreciated because I would prefer
> not having to go back and change other code that I have that expects the
> current element to be the context node for an xpath xpression.


So I think the 0.4 release is behaving correctly according to the XPath
spec - that if you start your expression with "/" that it
should start at the document. Let me know if you don't think its so but I
think this is correct behaviour now, its just the 0.3 release didn't handle
absolute addressing from a child node properly

James


_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to