After some careful debugging Iíve discovered the issue. If JXPath evaluates
Java objects and the iterate function is called, JXPath returns the Java
object. For XML, JXPath returns the value of the node, not the object
itself. Therefore, it is impossible (using iterate) to select all the
elements matching a pattern and for each iteration create a variable. Each
node can then be further processed.
This functionality sort of does work with the getValue method. The getValue
method returns the node itself which can be further evaluated. The problem
is it only returns the first node, not all the nodes like in iterate. I
guess to preserve backwards compatibility could an iterateNodes method be
added to the JXPathContext object? For Java objects, it would work exactly
the same. For XML/ JDOM it would return the Element object instead of
evaluating the node. I would be happy to add the method and directly mail my
changes to be approved.
Thanks,
Louis Calisi
>From: "Louis Calisi" <lcalisi@hotmail.com>
>Reply-To: "Jakarta Commons Users List" <commons-user@jakarta.apache.org>
>To: commons-user@jakarta.apache.org
>Subject: JXPath problem with the content.iterate method over XML
>Date: Tue, 01 Apr 2003 16:48:06 -0500
>
>Hello:
>
> First, I wanted to say a big thank you for writing JXPath. I've found
>it to be incredibly powerful and flexible. Iíve decided to write a JSP tag
>library, similar to Xpath. It works great over Java Objects but Iím having
>problems with XML.
>
>For instance, I can perform the following with Java objects and it works
>perfectly.
>
><jxpath:forEach id="i" select="repository//actionKey[transactionKey]">
> '<jxpath:out select="$i/transactionKey" />'<br>
></jxpath:forEach>
>
>But if I do the following:
>
><jxpath:forEach id="i" select="xml//level">
> '<jxpath:out select="$i/name" />'<br>
></jxpath:forEach>
>
>I get the following error for each iteration through the loop:
>
>ďNo value for xpath: $i/nameĒ
>
>XML does work if I attempt to access the node directly. The following piece
>of code produces the correct answer:
>
><jxpath:out select="xml//level[1]/name"/>
>
>Which leads me to believe itís either the way Iím opening the XML or using
>the context.iterator function. Iím not sure because they both work in some
>situations as explained above.
>
>
>I have function returns the XML document. Due to some compatibility
>problems with our app server, I canít use the DOM so this is the reason for
>using JDOM. The implemented is below:
>
>public Container getXml() {
> if (this.document == null)
> this.document = new JDOMDocumentContainer(getRepository());
> return document;
>}
>
>
>The getValue method is implemented below. My assumption is this function
>should return the root document.
>
>public Object getValue() {
> if (this.document != null)
> return this.document;
>
> try {
> SAXBuilder builder = new SAXBuilder();
> this.document = builder.build(new StringReader(this.xml));
> return this.document;
>
> } catch (Exception e) {
> logger.warn("Parsing this xml threw an exception: ");
> logger.warn(xml);
> throw new JXPathException("Cannot read XML from the
> session repository ", e);
> }
>}
>
>
>The part of the foreach tag which retrieves the Iterator below:
>
>JXPathContext context = getContext(repository);
>
>Iterator iterator = context.iterate(path);
>if (iterator == null)
> logger.debug("The value returned by the path '" + path
> + "' was null");
>return iterator;
>
>
>I also tried using iteratorPointer but it didnít work either.
>
>
>The part which creates a variable for the out tag is below:
>
>
>if (!iterator.hasNext())
> return false;
>
>this.current = iterator.next();
>getContext().getVariables().declareVariable(id, current);
>
>
>Finally, the part of the out tag which retrieves the object is below:
>
>JXPathContext context = getContext(repository);
>Object result = context.getValue(path);
>
>
>If you need any other code then please let me know. I can also help to fill
>in any gaps I may have left out. To summarize Iím not sure why the same
>foreach / out tag works as expected with Java code but not with XML. Also,
>the reason why JXPath fails with iterating over a result but not accessing
>the XML node directly.
>
>
>Louis Calisi
>
>
>_________________________________________________________________
>Tired of spam? Get advanced junk mail protection with MSN 8.
>http://join.msn.com/?page=features/junkmail
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
_________________________________________________________________
Tired of spam? Get advanced junk mail protection with MSN 8.
http://join.msn.com/?page=features/junkmail