[jdom-interest] JDOM, XPath and Namespaces

Jon Sharp jon.sharp at j2ssolutions.com
Thu Aug 14 03:32:53 PDT 2003


I've been loading XML documents (with Namespaces defined) and running XPath 
queries over them.
I've not been able to make the XPath queries return the correct nodes, 
until I've stripped out the namespace references before loading the file 
(ie the namespace.prefix and namespace.uri members for the Elements are 
"").  I then reset them recursively with  Element.setNamespace(Namespace ns);

I downloaded the jaxen libs and tried their samples, which use web.xml  as 
an input file.
The XPath bits worked fine until I added a Namespace to web.xml.

I tried XPath.addNamespace() but without any joy, eg following retuns 0 
items in List:
Namespace ns=Namespace.getNamespace("http://www.xxxx.com/envelope");
XPath xp = XPath.newInstance("/TopLevElement");
xp.addNamespace(ns);
List xlist = xp.selectNodes(tdoc);

Looking at an Element of this example, .namespace.prexif="" and 
.namespace.uri="http://www.xxxx.com/envelope", so it looks like there 
should be a match.

Can anyone provide some sample code to do XPath in the presence of 
non-empty Namespaces?
thanks
Jon


Jon Sharp    tel: +44 1273 504004    mobile: +44 7739 430793
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20030814/4296db2a/attachment.htm


More information about the jdom-interest mailing list