Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Base URI isn't output (Larry Levin) #9
I've been using JDom for quite some time with no probelems but just hit a snag when trying to create an OWL ontology using it. This issue is that while I set a baseURI for the document, it isnt showing up when the document is written using the XMLOutputer. The code is as follows:
Namespace rdfNs = Namespace.getNamespace("rdfs", "http://www.w3.org/2000/01/rdf-schema#"); Element root = new Element("RDF", rdfNs); Document doc = new Document(root); doc.setBaseURI("http://foo.bar/owlTest");
After building the document, I write it as follows:
Format myFormat = Format.getPrettyFormat(); XMLOutputter outputter = new XMLOutputter(myFormat); FileOutputStream fo = new FileOutputStream("out.txt"); outputter.output(doc, fo); fo.flush(); fo.close();
The problem is that the xml base decalaration xml:base="http://foo.bar/owlTest" must explicitly appear in the output but doesn't. Any help would be greatly appreciated.
I have investigated this issue from the perspective of fixing it in 1.1.2
the xml:base="" concept is specified in the XMLBase spec: http://www.w3.org/TR/xmlbase/
This concept is much broader than just a baseURI on the document-element as described above.
Further, the actual value set in the Document.setBaseURI() is essentially ignored for all purposes. There is no code in JDOM itself which reads the value set... 'third-party' code can set and read the value in Document.setBaseURI(), but JDOM itself does nothing with it. It is not output in any way.
Based on what I have understood of the XMLBase spec, JDOM is in a no-man's-land of support for XMLBase. On one hadnd, JDOM fully supports setting the xml:base="..." attribute, just do:
On the other hand, it does not 'associate' the Document.getBaseURI() with anything in the xml:base="..." form.
Since the 'correct' way for JDOM to behave is somewhat unspecified, and the work-around is easy, I don't think anything should be done in 1.1.2
For example, Larry's original problem would be easily solved with:
Namespace rdfNs = Namespace.getNamespace("rdfs", "http://www.w3.org/2000/01/rdf-schema#"); Element root = new Element("RDF", rdfNs); Document doc = new Document(root); root.setAttribute("base", "http://foo.bar/owlTest", Namespace.XML_NAMESPACE);
On the other hand, I think JDOM2 can do a better job at implementing proper support for xml:base in a way that makes sense according to the XMLBase spec.
Probably best done by adding something to the Element class's API for reading the current baseURI, and setting a new URI.
My evaluation is that this is not a candidate for a 1.1.2 fix.
This issue is more complex than at first appears.
The complexity stems from two competing concepts of BaseURI.
In XML Parsing the URI's needed to parse a document are relative to the document source URI, it's 'Base' URI.
Relative URL's in for example the DocType, etc. are resolved relative to this base.
This Base URI is what is recorded in the JDOM Document's BaseURI.
Once the document is fully parsed, the XML itself can have meaning linked to the xml:base="" attribute. This meaning is independant of the Document's BaseURI, and the 'business logic' meaning that the XMLBase specification gives to the xml:base attribute is completely independant to the Document's BaseURI (as defined for JDOM).
The two items are not comparable.
JDOM has full support for the XMLBase specification in the sense that anyone can easily add xml:base attributes to elements (which is all the XMLBase specification requires). Since JDOM never actually processes any linked documents or resolves any referrences in the post-parsing document, JDOM does not need any special intelligence to process the xml:base attribute. The application using JDOM is what needs that intelligence.
All JDOM needs to do is to support setting and retrieving the xml:base attribute values, and that is already supported with:
String baseuri = element.getAttributeValue("base", Namespace.XML_NAMESPACE); element.setAttribute("base", "myuri", Namespace.XML_NAMESPACE);
While JDOM does not natively need to be changed to support the XMLBase specification, it woul dbe useful to be able to track the base URI in accordance with the XMLBase rules. I ill thus add a support class in the org.jdom2.util package that enables the XMBase URI to be calculated, with some restrictions, in a useful way.
The comments/commit process does not make it obvious. See the new class XMLBase here:
This class makes it easy to calculate the effective XMLBase ui for a given Element