[jdom-interest] Problem using XPath with Namespaces

Benjamin Kopic benjamin.kopic at panContext.com
Fri Nov 14 02:13:21 PST 2003


Hi Laurent,

Thank you for the reply it worked perfectly. Do you think it might be
worthwhile adding additional comment to addNamespace() method telling
the user to create a dummy prefix if an empty prefix is defined with a
Namespace? It wasn't clear to me from the JavaDoc that I could use a
dummy namespace prefix.

Best regards

Ben


On Thu, 2003-11-13 at 17:51, Laurent Bihanic wrote:

> 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());
> > }
> 
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com

-- 
benjamin kopic
m: +44 (0)780 154 7643
t: +44 (0)20 7794 3090
e: benjamin.kopic at panContext.com
w: http://www.panContext.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20031114/46771d76/attachment.htm


More information about the jdom-interest mailing list