[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

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

  • From: Julian Reschke <julian.reschke@g...>
  • To: xml-dev@l...
  • Date: Fri, 11 May 2001 09:49:22 +0200

org.apache.xml.serialize.outputformat
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>



PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.