[jdom-interest] JDOM Issue #5 - DTD-aware Attribute output

Rolf Lear jdom at tuis.net
Mon Mar 26 08:20:03 PDT 2012


Hi Paul, all.

Just checking that we have things 'straight'.... since I have not heard
back from you.

Three things...
1. I said that SAX parser will ignore the DTD unless you 'DTD-validate'...
but I was wrong, (at least) Xerces SAX parser will apply the default
attribtues.
2. You said "Removing the validation, sadly disables the passing of
attribute presence info, it seems." which I think you were saying because I
had said that before to you.... but this is wrong .. .see above. Do you
have 'evidence' that this is true though? Or are you basing this on what I
told you before...?
3. It seems (at least with the Xerces 2.11 parser) that using
non-validating SAXBuilder will produce the exact results you want... (with
the exception of Namespace declarations). Can you confirm this?

I have been thinking about the Namespace problem, and, I don't think that
there is a 'good' solution on the 'output' side of things, but, there is
value in having a tool that:
- scans the entire 'Element' tree
- 'explicitly' declares namespaces at the highest common node in the tree

This 'tool' will thus 'declare' the namespace at a 'high' level, and the
outputter will not need to re-declare them at the lower levels.... which, I
would guess, would result in removing many low-level Namespace
declarations, and replace them with a single high-level declaration. This
would 'solve' your problem in one sense.

Another alternative for you would be to manually remove the not-specified
attributes just prior to output:

	for (Element e : doc.getDescendants(Filters.element())) {
		if (e.hasAttributes()) {
			for (Iterator<Attribute> it = e.getAttributes().iterator();
it.hasNext();) {
				Attribute a = it.next();
				if (!a.isSpecified()) {
					it.remove();
				}
			}
		}
	}

if you physically remove the (default) attributes from the document, then
they will not affect the namespace calculations for the output either.

Rolf


> In fact, going back to the original example, I have the following code:
> 
> 	public static void main(String[] args) throws JDOMException,
IOException {
> 		String xml = 
>
"http://svn.activemath.org/LeAM-calculus/LeAM_calculus/oqmath/contin.oqmath";
> 		SAXBuilder builder = new SAXBuilder();
> 		Document doc = builder.build(xml);
> 		for (Element e : doc.getDescendants(Filters.element())) {
> 			if (e.hasAttributes()) {
> 				for (Attribute a : e.getAttributes()) {
> 					if (!a.isSpecified()) {
> 						System.out.println("Attribute was defaulted " + a);
> 					}
> 				}
> 			}
> 		}
> 		Format speconly = Format.getPrettyFormat();
> 		speconly.setSpecifiedAttributesOnly(true);
> 		XMLOutputter xout = new XMLOutputter(speconly);
> 		xout.output(doc, System.out);
> 	}
> 
> And it does exactly what you want.... (except for the namespaces).
> 
> Rolf
> 


More information about the jdom-interest mailing list