commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Nestler <>
Subject [JXPath] Context Namespace-Relative Queries (v1.2)
Date Thu, 07 Apr 2005 17:08:28 GMT

I recently had to switch from JXPath 1.1 to 1.2 in order to benefit
from some bug fixes. I am using JDOM 1.0. It appears that JXPath's
behavior regarding namespaces in JDOM trees changed and became less
convenient and flexible.

I have documents like this:

<html xmlns="">
      <td><a href="/Home"/></td>
      <td>Home Page</td>
      <td><a href="/ToolBar"/></td>
      <td>Tool Bar</td>

The namespace of the root element is variable and might be the XHTML
namespace, another namespace, or no namespace at all. The table
structure is always the same, and cell values vary. And I have some
code that executes the following query:

SAXBuilder sax = new SAXBuilder();
Document doc = StringReader(...);
Element rootElement = doc.getRootElement();
JXPathContext ctx = JXPathUtil.newContext(rootElement);
String value = ctx.getValue("/table/tr[2]/td[2]");

This worked fine with JXPath 1.1, but it doesn't work anymore with
1.2. The new JXPath version throws an exception:

org.apache.commons.jxpath.JXPathException: No value for xpath:
	at org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.getValue(
	at org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.getValue(

Why would this not work, considering that all the DOM nodes live in
the same namespace - the namespace of the context bean (root element)?
It appears that I can register the XHTML namespace, but then I have to
use a prefix for every element name in my query - and it won't work if
the namespace is not set or is different (that's possible in my app).

Is there a solution to this problem, i.e. is there a way to make
JXPath interpret the root element's namespace as the default namespace
requiring no prefix in XPath queries?

Thanks in advance,

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message