You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Peter Neumcke <pn...@virbus.de> on 2003/02/12 17:44:24 UTC

[JXPATH] parentContext not used for XPath starting with '../'

Hi people,
I have a question, which I couldn't answer by reading the docs.

What happens, if the XPath uses '../' at the beginning to go back one step?
Will the parentContext be used to evaluate the XPath? 

Here's my testcode (adapted from the original test code):

    public void testRootHandling(){
        if (!enabled){
            return;
        }
        System.out.println("testing root handling start");
        JXPathContext context = JXPathContext.newContext(bean);
        JXPathContext nestedContext = JXPathContext.newContext(context,
bean.getNestedBean());
        testGetValue(nestedContext, "/boolean", Boolean.FALSE);
        System.out.println("accessing /boolean in nested context worked!");
        testGetValue(nestedContext, "../boolean", Boolean.FALSE);
        System.out.println("accessing ../boolean in nested context
worked!");
    }

The corresponding output:
test.jxpath:
     [echo] Running JXPath tests ...
     [java] .testing root handling start
     [java] accessing /boolean in nested context worked!
     [java] E.....
     [java] Time: 1.578
     [java] There was 1 error:
     [java] 1) testRootHandling(org.virbus.websheet.jxpath.JXPathTestCase)
     [java] org.apache.commons.jxpath.JXPathException: No value for xpath:
../boolean
     [java]     at
org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.getValue(JXPathConte
xtReferenceImpl.java:206)
     [java]     at
org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.getValue(JXPathConte
xtReferenceImpl.java:200)
     [java]     at
org.virbus.websheet.jxpath.JXPathTestCase.testGetValue(JXPathTestCase.java:3
99)
     [java]     at
org.virbus.websheet.jxpath.JXPathTestCase.testRootHandling(JXPathTestCase.ja
va:178)
     [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native
Method)
     [java]     at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39
)
     [java]     at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
.java:25)


It looks like it's possible to go back using '/' at the beginning, but using
'../' doesn't work. Has anyone got some advice?
I'm using commons-jxpath-1.0

I'm new to JXPath, so excuse me, if I understood something wrong or if
there's an obvious solution.

Mit freundlichen Gruessen,

Peter Neumcke

--
Software Development
VIRBUS AG
Fon   +49(0)341-979-7421
Fax   +49(0)341-979-7409
pneumcke@virbus.de
www.virbus.de

Re: [JXPATH] parentContext not used for XPath starting with '../'

Posted by Dmitri Plotnikov <dp...@yahoo.com>.
Peter,

The parent-child relationship between contexts has nothing to do with
the parent-child relationship between beans.  The child context
inherits variables, extension functions, locale etc from the parent
context, but nothing about the root bean. 

In JXPath there is no mechanism for the traversal from the root bean to
its ancestors.

Here's an excerpt from the documentation that explains how the parent::
axis is implemented:

<quote>
   In DOM/JDOM the definition of a node's parent is clear: a Node
always  points to its parent. XML is a strict tree, so there always
exactly one parent for every node except the root.

   With other models the situation is more complex. The model can no
longer be described as a tree.  In many cases it is a complicated graph
with many paths to the same node and even referential cycles where node
A is node B's child, but also node B is node A's child. Even if the
graph is a strict tree, a node of that tree may not have a pointer to
its parent.

   Because of all these issues, JXPath abandons the static notion of a
parent/child relationship in favor of a dynamic one.  When an XPath is
evaluated, the engine performs a series of searches and computations in
so called evaluation contexts.  For example, when the "/foo/bar" path
is evaluated, JXPath first looks for a node named "foo" in the root
evaluation context. If such a node is found, the interpreter forms a
new context for the discovered node and searches for a node named "bar"
in that context.

   This chain of contexts is used in JXPath to define the parent-child
relationship. Parent is the base node of the previous evaluation
context in the chain.  A more appropriate name for the "parent::" axis
would then be "step back".

   Consider this example.  The evaluated path is "foo//bar/../baz".  In
the process of evaluating of this path, the engine will walk the graph
forming chains of context like "/foo/a/b/c/bar".  Once a node with the
name "bar" is found, the engine will "step back": in our case it will
go back to the "/foo/a/b/c" context and then look for the node with the
name "baz" in that context.
</quote>

I hope this explanation helps.

- Dmitri


--- Peter Neumcke <pn...@virbus.de> wrote:
> Hi people,
> I have a question, which I couldn't answer by reading the docs.
> 
> What happens, if the XPath uses '../' at the beginning to go back one
> step?
> Will the parentContext be used to evaluate the XPath? 
> 
> Here's my testcode (adapted from the original test code):
> 
>     public void testRootHandling(){
>         if (!enabled){
>             return;
>         }
>         System.out.println("testing root handling start");
>         JXPathContext context = JXPathContext.newContext(bean);
>         JXPathContext nestedContext =
> JXPathContext.newContext(context,
> bean.getNestedBean());
>         testGetValue(nestedContext, "/boolean", Boolean.FALSE);
>         System.out.println("accessing /boolean in nested context
> worked!");
>         testGetValue(nestedContext, "../boolean", Boolean.FALSE);
>         System.out.println("accessing ../boolean in nested context
> worked!");
>     }
> 
> The corresponding output:
> test.jxpath:
>      [echo] Running JXPath tests ...
>      [java] .testing root handling start
>      [java] accessing /boolean in nested context worked!
>      [java] E.....
>      [java] Time: 1.578
>      [java] There was 1 error:
>      [java] 1)
> testRootHandling(org.virbus.websheet.jxpath.JXPathTestCase)
>      [java] org.apache.commons.jxpath.JXPathException: No value for
> xpath:
> ../boolean
>      [java]     at
>
org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.getValue(JXPathConte
> xtReferenceImpl.java:206)
>      [java]     at
>
org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.getValue(JXPathConte
> xtReferenceImpl.java:200)
>      [java]     at
>
org.virbus.websheet.jxpath.JXPathTestCase.testGetValue(JXPathTestCase.java:3
> 99)
>      [java]     at
>
org.virbus.websheet.jxpath.JXPathTestCase.testRootHandling(JXPathTestCase.ja
> va:178)
>      [java]     at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native
> Method)
>      [java]     at
>
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39
> )
>      [java]     at
>
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
> .java:25)
> 
> 
> It looks like it's possible to go back using '/' at the beginning,
> but using
> '../' doesn't work. Has anyone got some advice?
> I'm using commons-jxpath-1.0
> 
> I'm new to JXPath, so excuse me, if I understood something wrong or
> if
> there's an obvious solution.
> 
> Mit freundlichen Gruessen,
> 
> Peter Neumcke
> 
> --
> Software Development
> VIRBUS AG
> Fon   +49(0)341-979-7421
> Fax   +49(0)341-979-7409
> pneumcke@virbus.de
> www.virbus.de
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 


__________________________________________________
Do you Yahoo!?
Yahoo! Shopping - Send Flowers for Valentine's Day
http://shopping.yahoo.com