[jdom-interest] JDOM, XPath and Namespaces

Laurent Bihanic laurent.bihanic at atosorigin.com
Mon Aug 18 00:47:13 PDT 2003


Jon Sharp wrote:
> 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);

The key point to remember is that there is no default namespace in XPath. That 
is, whether your document uses namespace prefixes or not, you have to use 
prefixes when referencing namespaces in XPath expressions. These prefixes do 
not have to match the ones used in the document if you declare the namespace 
using XPath.addNamespace().

For example:
    Namespace ns=Namespace.getNamespace("foo", "http://www.xxxx.com/envelope");
    XPath xp = XPath.newInstance("/foo:TopLevElement");
    xp.addNamespace(ns);
    List xlist = xp.selectNodes(tdoc);

Laurent




More information about the jdom-interest mailing list