0

I'm trying to read existing xml file, that has duplicated namespace declarations. It is important to me to leave this declarations for further processing. Earlier I was using XERCES implementation of org.w3c.dom api to read such a documents, and all declarations left, but when I started to use JDOM, it started to omit redundant namespace declarations.

Here is the sample xml file:

<?xml version="1.0" encoding="UTF-8"?>
<document xmlns:ns="namespace_uri">
  <element1>...</element1>
  <element2>
    <ns:child>
      text
    </ns:child>
  <element3 xmlns:ns="namespace_uri">
    <ns:child>
      content
    </ns:child>
  </element3>
</document>

I'm building JDOM Document with this code:

SAXBuilder builder = new SAXBuilder();
File inputXml = new File(inputFile);
jdomInDoc = builder.build(inputXml);

Is there any way to force JDOM not to omit redundant namespace declarations?

Thank you in advance for yor answers.

2 Answers2

0

Currently there is no way in JDOM to leave the redundant namespace declarationa. It is possible, even likely that if you want you can override the XMLOutputter to output the duplicates. I can look in to that.

I have opened issue 83 https://github.com/hunterhacker/jdom/issues/83

This s the sort of thing that can potentially be added to the JDOM Format system

Regardless, I will look in to whether this will be possible in JDOM with customization...

rolfl
  • 17,539
  • 7
  • 42
  • 76
  • Thank you for your answer and clues. I decided to override AbstractDOMOutputProcessor with my own class to force it to do, what I need. It works for me, but it is rather inelegant solution and it will be nice, if JDOM will someday allow to customize outputter by some parameter to leave redundant namespaces. – user1395648 May 17 '12 at 09:08
  • Hi, I have 'tried' to put in a comprehensive system for tracking the Namespaces from input through output. It is not easy to do while maintaining compatibility. It seems you may have done a better job than me.... The SAX Parsing of XML documents when there is a child namespace 'redundantly' declared is ignored.... Hard to put this all in one comment. Please consider adding your solution to https://github.com/hunterhacker/jdom/issues/83 (which I have just updated... I have spent some hours investigating). – rolfl May 17 '12 at 11:51
0

What kind of software is going to receive this XML and treat it differently depending whether the redundant namespace declarations are present or not? The same kind of software, perhaps, as will only handle attributes if they are delimited by single quotes, or will only handle text if wrapped in a CDATA section?

General-purpose XML software like Xerces or JDOM is entitled to assume that the recipient is a well-behaved XML consumer, which means it doesn't attach meaning to accidents of the lexical XML representation such as the choice of quotes, the use of CDATA, or the presence of redundant namespace bindings.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • But isn't this exactly whats needed when processing XML schema imports? If I define type T in namespace 'tns', and import it in schema S, I must have a namespace binding between a prefix and 'tns' to be able to refer to the type T. However, no element in this schema file may use this binding. – forty-two May 15 '12 at 21:21