StAX
  1. StAX
  2. STAX-61

Missing namespace scope for empty elements in XMLStreamWriter

    Details

    • Type: Bug Bug
    • Status: Open Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Labels:
      None
    • Environment:
      StAX 1.2.0, JDK 1.5
    • Testcase included:
      yes
    • Number of attachments :
      1

      Description

      Writing empty elements through XMLWriterBase does not open a scope for namespaces (if the option XMLOutputFactory.IS_REPAIRING_NAMESPACES is used). The following test results in invalid XML being written:

      out.writeStartDocument();
      out.writeStartElement("a");

      out.writeEmptyElement("http://foo", "b");
      out.writeEmptyElement("http://foo", "b");

      out.writeEndElement();
      out.writeEndDocument();

      Should produce:

      <?xml version='1.0' encoding='utf-8'?>
      <a>
      <ns1:b xmlns:ns1="http://foo"/>
      <ns2:b xmlns:ns2="http://foo"/>
      </a>

      Instead, it produces:

      <?xml version='1.0' encoding='utf-8'?>
      <a>
      <ns1:b xmlns:ns1="http://foo"/>
      <ns1:b />
      </a>

      This is invalid XML since the prefix ns1 is not bound to any namespace in the second <b/> element.

      The runnable test:

      public class TestXMLStreamWriter extends TestCase {

      public void testEmptyElementNamespaces() throws Exception

      { XMLOutputFactory factory = XMLOutputFactory.newInstance(); factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true); StringWriter buffer = new StringWriter(); XMLStreamWriter out = factory.createXMLStreamWriter(buffer); out.writeStartDocument(); out.writeStartElement("a"); out.writeEmptyElement("http://foo", "b"); out.writeEmptyElement("http://foo", "b"); out.writeEndElement(); out.writeEndDocument(); out.flush(); assertEquals( "<?xml version='1.0' encoding='utf-8'?>" + "<a>" + "<ns1:b xmlns:ns1=\"http://foo\"/>" + "<ns2:b xmlns:ns2=\"http://foo\"/>" + "</a>", buffer.toString()); }

      }

        Activity

        Hide
        Bernhard Haumacher added a comment -

        Test case that shows the problem of missing namespace scopes.

        Show
        Bernhard Haumacher added a comment - Test case that shows the problem of missing namespace scopes.

          People

          • Assignee:
            Unassigned
            Reporter:
            Bernhard Haumacher
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated: