[jdom-interest] Problem using XPath with Namespaces

Laurent Bihanic laurent.bihanic at atosorigin.com
Thu Nov 13 09:51:23 PST 2003


Hi,

There is no default namespace in XPath. Hence, all namespaces must have a 
prefix in the XPath expressions. The prefix does not have to match the one in 
the document.

XPath journalTitleXPath = 
XPath.newInstance("/foo:doi_batch/foo:body/foo:journal/foo:journal_metadata/foo:full_title");
journalTitleXPath.addNamespace(Namespace.getNamespace("foo", 
JournalConstants.XML_DOI_BATCH_A_XMLNS_VAL));

should work.

Laurent


Benjamin Kopic wrote:
> I am having some problem using JDOM XPath class with Namespaces. The 
> following JUnit test case code fails due to the value returned by 
> 'selectSingleNode()' is always null:
> 
> XPath journalTitleXPath = 
> XPath.newInstance("/doi_batch/body/journal/journal_metadata/full_title");
> 
> journalTitleXPath.addNamespace(Namespace.getNamespace(JournalConstants.XML_DOI_BATCH_A_XMLNS_VAL));
> journalTitleXPath.addNamespace(Namespace.getNamespace(
> JournalConstants.XML_DOI_BATCH_A_XMLNS_XSI,
> JournalConstants.XML_DOI_BATCH_A_XMLNS_XSI_VAL));
> 
> Element fullTitle = (Element) journalTitleXPath.selectSingleNode(doc);
>   
>    // the line below fails the assertion, i.e. fullTitle is null value
> assertNotNull(fullTitle);
> assertEquals(expectedJournalTitle, fullTitle.getText());
> }




More information about the jdom-interest mailing list