[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

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 

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",
Element child1 = new Element("child1");
Element child2 = new Element("child2");


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

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

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


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

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

> 	/** Define a <code>Namespace</code> for when in an empty namespace
> 	public static final Namespace EMPTY_NAMESPACE = new Namespace(null,
> 		namespaces.put("", NO_NAMESPACE);
> 		namespaces.put("", EMPTY_NAMESPACE);
<         namespaces.put("&", NO_NAMESPACE);
<         return prefix;
> 		return prefix == null? "" : prefix;
<         return uri;
> 		return uri == null? "" : uri;
> 			//special case for empty namspace
> 			if (ns == Namespace.EMPTY_NAMESPACE || uri == null) 
> 				return ns == this;

******diff for XMLOutputter******
<             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