javaxmlstaxxmlstreamwriter

XMLStreamWriter missing exceptions?


I'm currently looking into writing a loose port of XMLStreamWriter to another environment, and trying to get a handle on its namespace logic. Oracle's documentation for this class seems to specify that with namespace repairing disabled, attempting to write an element/attribute using a namespace URI already bound to some prefix, using a different prefix, should cause an XMLStreamException. But I don't see this behaviour. With the following code:

public void run() throws Exception {
    String BOB = "http://www.bob.com/"
    XMLOutputFactory fac = XMLOutputFactory.newInstance();
    fac.setProperty("javax.xml.stream.isRepairingNamespaces", false); // default
    XMLStreamWriter writer = fac.createXMLStreamWriter(new FileWriter("tst.xml"));

    writer.writeStartElement("bob", "root", BOB);
    writer.setPrefix("bob", BOB);
    writer.writeNamespace("bob", BOB);
    writer.writeAttribute("notbob", BOB, "attr", "value"); // Should fail?
    writer.writeStartElement("notbob", "firstinner", BOB); // Should fail?
    writer.writeEndElement();
    writer.writeEndElement();
    writer.writeEndDocument();

    writer.flush();
    writer.close();
}

I would expect both lines using the "notbob" prefix for the same namespace URI already bound to "bob" to throw exceptions. Instead, the code completes normally, and tst.xml contains

<bob:root xmlns:bob="http://www.bob.com" notbob:attr="value">
    <notbob:firstinner></notbob:firstinner>
</bob:root>

Am I misunderstanding, or is this behaviour incorrect?


Solution

  • attempting to write an element/attribute using a namespace URI already bound to some prefix, using a different prefix, should cause an XMLStreamException.

    XML allows binding multiple different prefixes to the same namespace URI. Therefore throwing an XMLStreamException in that situation would violate the XML specification.