[jdom-interest] Problem using XPath with Namespaces

Benjamin Kopic benjamin.kopic at panContext.com
Thu Nov 13 09:00:15 PST 2003


Hi

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:

 public static final String XML_DOI_BATCH_A_XMLNS_VAL =
"http://www.crossref.org/schema/2.0.5";
 public static final String XML_DOI_BATCH_A_XMLNS_XSI = "xsi";
 public static final String XML_DOI_BATCH_A_XMLNS_XSI_VAL =
"http://www.w3.org/2001/XMLSchema-instance";

	private void assertJournalTitle(String xmlDoc) throws Exception {
		StringReader inReader = new StringReader(xmlDoc);
		SAXBuilder saxBuilder = new SAXBuilder(false);
		Document doc = saxBuilder.build(inReader);
		
		assertNotNull(doc);
		
		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());
	}


This is the XML I am trying to test (please note that this may fail if
you turn ON the validation):

<?xml version="1.0" encoding="UTF-8"?>
<doi_batch xmlns="http://www.crossref.org/schema/2.0.5"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0.5">
  <head>
    <batch_id>extenza66</batch_id>
    <timestamp>20031113164919</timestamp>
    <doi_batch>
      <name>Extenza</name>
      <email_address>test1 at bsg.co.uk</email_address>
    </doi_batch>
    <registrant>Swets and Zeitlinger Publishers</registrant>
  </head>
  <body>
    <journal>
      <journal_metadata language="en">
        <full_title>Aquatic Insects &amp; some &lt;xml&gt;</full_title>
        <abbrev_title>Aquat insects</abbrev_title>
        <issn media_type="electronic">0165-0424</issn>
      </journal_metadata>
      <journal_issue>
        <publication_date media_type="print">
          <year>2002</year>
        </publication_date>
        <journal_volume>
          <volume>23</volume>
        </journal_volume>
        <issue>106</issue>
      </journal_issue>
    </journal>
  </body>
</doi_batch>

I am using JDOM 1.0-b9.

Could someone please help me figure out why the node in question is
never selected?

Best regards,

Ben
-- 
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/20031113/9824cff4/attachment.htm


More information about the jdom-interest mailing list