OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

DOM level 2: creating elements/attributes in namespaces without prefix



Hi,

Consider:

    javax.xml.parsers.DocumentBuilderFactory dbf =
javax.xml.parsers.DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    Document doc = dbf.newDocumentBuilder().newDocument();

    Element root = doc.createElementNS ("ns1", "bar");
    doc.appendChild (root);
    root.setAttributeNS ("ns2", "attr", "val");

As I understand, this will create a document element in the namespace "ns1"
with node name "bar". The name parameter to createElementNS is a qualified
name in terms of XMLNS [1], meaning that the name may be qualified with a
prefix, but doesn't have to.

    System.out.println ("ns: " + root.getNamespaceURI() + ", localName: " +
root.getLocalName());

-> ns: ns1, localName: bar

shows that the node indeed has been created this way. Similarily,

    Attr a = root.getAttributeNodeNS ("ns2", "attr");
    if (a != null)
      System.out.println ("ns: " + a.getNamespaceURI() + ", localName: " +
a.getLocalName());

-> ns: ns2, localName: attr

shows that the attribute has been created as desired.

So based on,

1) the DOM recommendation and
2) the behaviour of Xerces and MSXML

I conclude that this is a perfectly legal way to build a DOM. If this
wouldn't be the case, the create* methods should raise DOMExceptions, right?

However, as the attribute on the document element is in a namespace
different from it parent element's, it's obviously non-trivial to serialize
the DOM (yes, I know that serialization is not part of DOM level 2).

There are several different ways for a serializer to react, some of them
are:

a) Refuse to do the serialization because it's impossible to serialize the
attribute without namespace prefix rewriting,

b) Create a different prefix for the attribute such as

    <bar xmlns="ns1" x:attr="val" xmlns:x="ns2" />

   (done by MSXML)

c) Do not automatically create namespace declarations at all, resulting in a
serialization with wrong namespace information that possibly is not
well-formed (once we have two attributes with identical local names but
differing namespaces).

   <bar attr="val"/>

   (done by the serializers in Xerces and Xalan).


It seems that the Xerces and Xalan serializers create namespace declarations
if and only if there are explicit xmlns attributes in the DOM. This allows a
program to add namespace declarations programatically when needed, but it
can lead to a great amout of code duplication and inefficient code.

Looking at DOM level 3 [2] , there will be a user option to select a
behaviour similar to (a).

Finally, here's the code I used to invoke the Xerces and Xalan serializers,
in case something I'm doing is incorrect..:

    org.apache.xml.serialize.OutputFormat of =
      new org.apache.xml.serialize.OutputFormat (doc, null, true);
    java.io.StringWriter out = new java.io.StringWriter();

    org.apache.xml.serialize.XMLSerializer ser =
      new org.apache.xml.serialize.XMLSerializer(out, of);

    ser.serialize (doc.getDocumentElement());

    System.out.println (out.toString());

    javax.xml.transform.Transformer tf =
      javax.xml.transform.TransformerFactory.newInstance().newTransformer();

    tf.transform (new javax.xml.transform.dom.DOMSource (doc),
      new javax.xml.transform.stream.StreamResult (out));

    System.out.println (out.toString());


Any feedback appreciated.


[1] <http://www.w3.org/TR/1999/REC-xml-names-19990114/#ns-qualnames>
[2] <http://www.w3.org/TR/2001/WD-DOM-Level-3-CMLS-20010419/load-save.html>