commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Louis Calisi" <>
Subject JXPath problem with the content.iterate method over XML
Date Tue, 01 Apr 2003 21:48:06 GMT

     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 

<jxpath:forEach id="i" select="repository//actionKey[transactionKey]">
    '<jxpath:out select="$i/transactionKey" />'<br>

But if I do the following:

<jxpath:forEach id="i" select="xml//level">
   '<jxpath:out select="$i/name" />'<br>

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 = StringReader(this.xml));
        return this.document;

    } catch (Exception e) {
        logger.warn("Parsing this xml threw an exception: ");
        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 =;
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.

View raw message