[jdom-interest] A little more info on DOMOutputter and Xalan transformation problems...

philip.nelson at omniresources.com philip.nelson at omniresources.com
Fri Mar 16 14:31:34 PST 2001


> That's probably due to the fact that the JDOM has a 
> NO_NAMESPACE with ""
> as its URI.  It's carrying through to DOM.  At minimum DOMOutputter
> should be smarter.  The right solution is probably to have a 
> better way
> of handling no namespaces.  Need to investigate if there's any issues
> with just using "null".

Yes!  I looked first at the code for XMLOutputter but is seemed to me to be
a lot
of tricky code already and it still isn't quite right in this regard. I
think a distiction needs to be made between "the element has no namespace"
and "the element has an empty namespace" and it makes XMLOutputter easier to
code.  If you don't have a namespace, don't write it.  If it's empty, write
it.

I am attaching my revised Namespace class and a diff for XMLOutputter that I
*think* is based on your current version.  I in fact do use null internally
but just as a marker to distinguish NO_NAMESPACE from EMPTY_NAMESPACE.  In
both cases, they return "" from getPrefix and getURI but 
Namespace.NO_NAMESPACE != Namespace.EMPTY_NAMESPACE

The tests I checked into jdom-test today have some code to test this but
it's commented out.  A synopsis is this:

Here is an usage example

Normal case: (no change but now it works)
Element element = new Element("element",
Namespace.getNamespace("http://foo"));
Element child1 = new Element("child1");
Element child2 = new Element("child2");

element.addContent(child1);
element.addContent(child2);

XMLOutputter op = new XMLOutputter("     ", true);
op.output(element, System.err);

It outputs:
<element xmlns="http://foo">
      <child1 />
      <child2 />
</element>


for children with an explicit empty namespace:
Element child1 = new Element("child1", Namespace.EMPTY_NAMESPACE);
Element child2 = new Element("child2", Namespace.EMPTY_NAMESPACE);

element.addContent(child1);
element.addContent(child2);

<element xmlns="http://foo">
      <child1 xmlns="" />
      <child2 xmlns="" />
</element>



******* diff for Namespace*****

100a99,101
> 	/** Define a <code>Namespace</code> for when in an empty namespace
*/
> 	public static final Namespace EMPTY_NAMESPACE = new Namespace(null,
null);
> 
122a124,125
> 		namespaces.put("", NO_NAMESPACE);
> 		namespaces.put("", EMPTY_NAMESPACE);
124d126
<         namespaces.put("&", NO_NAMESPACE);
259c275
<         return prefix;
---
> 		return prefix == null? "" : prefix;
270c286
<         return uri;
---
> 		return uri == null? "" : uri;
289a306
> 			
290a308,310
> 			//special case for empty namspace
> 			if (ns == Namespace.EMPTY_NAMESPACE || uri == null) 
> 				return ns == this;
326d345

******diff for XMLOutputter******
956c954
<             if (!ns.getURI().equals(uri)) { // output a new namespace decl
---
> 			if ((!ns.getURI().equals(uri)) && ns !=
Namespace.NO_NAMESPACE ) { // output a new namespace decl

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Namespace.java
Type: application/octet-stream
Size: 11532 bytes
Desc: not available
Url : http://jdom.org/pipermail/jdom-interest/attachments/20010316/252213f7/Namespace.obj


More information about the jdom-interest mailing list