Backtestsjsouprun-20261006-181938#2504

Issue #2504 Merged fix PR # Run 6 Oct 2026 · run-20261006-181938 jhy/jsoup

W3CDom serializes plain XML with the reserved XML namespace

Better

Our patch was judged better than the merged fix.

In this judgment, Patch A = our agent, Patch B = the merged fix.

Both patches fix the root cause the same way. When the W3C element is created, they map jsoup's internal plain-XML sentinel namespace (Parser.NamespaceXml) to null. Explicit namespaces are kept, and xml:* attributes are left alone. The main difference is how they treat prefixed elements that sit in the sentinel namespace without a declaration (e.g. `<foo:bar>`).

- **Patch A** nulls the namespace unless the tag name starts with `xml:`. An undeclared `foo:bar` then goes through the existing imputed-empty-namespace path instead of being bound to the reserved XML namespace, and `xml:baz` correctly keeps the reserved namespace.
- **Patch B** only nulls when `tag().prefix()` is empty. An undeclared `foo:bar` therefore still asks for createElementNS(XML-namespace, "foo:bar"). That is invalid per the Namespaces spec, so it either throws and falls back or keeps the reserved namespace on the element. Patch B also depends on `Tag.prefix()`, which is not shown in the diff.

**Tests.** Patch A's tests are broader. They cover:
- the exact issue example, round-tripped through a namespace-aware parser;
- the static `convert` path;
- xml:lang preserved alongside explicit default and prefixed namespaces;
- undeclared prefixes and `xml:`-prefixed elements.

Patch B's tests are solid but narrower: plain XML with xml:lang, and explicit default namespaces including `xmlns=""`. They do not cover the undeclared-prefix edge.

**Other differences.** Patch B adds a CHANGES entry and a tidy helper method with @Nullable, which is nice craft but secondary. Overall, Patch A handles more of the edge cases correctly and verifies them more thoroughly.

The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.

Solve

18m 44s

Wall-clock time in the container

Full test suite

No new failures

2,018 passed · 0 skipped · 2,018 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

82vs 59 merged

Changed lines, ours vs merged

Plain XML parsed with Parser.xmlParser() should round-trip through the default namespace-aware W3CDom conversion as parseable XML. But for an ordinary no-namespace XML document, W3CDom currently serializes jsoup's internal XML parser namespace as the document's default namespace.

For example, converting <root><child>One</child></root> through new W3CDom().fromJsoup(doc) and W3CDom.asString(..., W3CDom.OutputXml()) emits <root xmlns="http://www.w3.org/XML/1998/namespace">.... That namespace is reserved for the xml prefix and cannot be bound as the default namespace, so a normal XML parser rejects the output.

Fix is to keep explicit source namespaces as W3C DOM namespaces, but map our internal plain-XML sentinel namespace to no namespace when creating unprefixed W3C elements. xml:* attributes such as xml:lang should continue to use the reserved XML namespace.

The two patches

Our patch+82 −0
src/main/java/org/jsoup/helper/W3CDom.javaindex b26cb93f..d505961e 100644@@ -4,6 +4,7 @@ import org.jsoup.internal.Normalizer; import org.jsoup.internal.StringUtil; import org.jsoup.nodes.Attribute; import org.jsoup.parser.HtmlTreeBuilder;+import org.jsoup.parser.Parser; import org.jsoup.select.NodeVisitor; import org.jsoup.select.Selector; import org.w3c.dom.Comment;@@ -360,6 +361,9 @@ public class W3CDom {                 org.jsoup.nodes.Element sourceEl = (org.jsoup.nodes.Element) source;                 String namespace = namespaceAware ? sourceEl.tag().namespace() : null;                 String tagName = Normalizer.xmlSafeTagName(sourceEl.tagName());+                // the XML parser's internal default namespace is reserved for the xml: prefix; other elements without a declared namespace are in no namespace+                if (Parser.NamespaceXml.equals(namespace) && !tagName.startsWith("xml:"))+                    namespace = null;                 try {                     // use an empty namespace if none is present but the tag name has a prefix                     String imputedNamespace = namespace == null && tagName.contains(":") ? "" : namespace;src/test/java/org/jsoup/helper/W3CDomTest.javaindex b9d1d330..4acdab36 100644@@ -518,6 +518,84 @@ public class W3CDomTest {         assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><html xmlns=\"http://www.w3.org/1999/xhtml\"><head/><body><div xmlns:v-bind=\"undefined\" v-bind:class=\"test\"><span v-bind:style=\"color:red\"/></div></body></html>", xml);     } +    private static Document reparseNamespaceAware(String xml) throws Exception {+        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();+        factory.setNamespaceAware(true);+        return factory.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));+    }++    @Test void plainXmlHasNoNamespaceAndRoundTrips() throws Exception {+        org.jsoup.nodes.Document jdoc = Jsoup.parse("<root><child>One</child></root>", "", Parser.xmlParser());+        Document w3cDoc = new W3CDom().fromJsoup(jdoc);++        org.w3c.dom.Element root = w3cDoc.getDocumentElement();+        assertNull(root.getNamespaceURI());+        assertNull(root.getFirstChild().getNamespaceURI());++        String out = W3CDom.asString(w3cDoc, W3CDom.OutputXml());+        assertFalse(out.contains("xmlns"), out);+        assertTrue(out.contains("<root><child>One</child></root>"), out);++        Document reparsed = reparseNamespaceAware(out); // would throw if the reserved xml namespace was bound as default+        assertEquals("root", reparsed.getDocumentElement().getLocalName());+        assertNull(reparsed.getDocumentElement().getNamespaceURI());+        assertEquals("One", reparsed.getDocumentElement().getTextContent());+    }++    @Test void plainXmlViaStaticConvertRoundTrips() throws Exception {+        org.jsoup.nodes.Document jdoc = Jsoup.parse("<root><child>One</child></root>", "", Parser.xmlParser());+        Document w3cDoc = W3CDom.convert(jdoc);+        assertNull(w3cDoc.getDocumentElement().getNamespaceURI());+        String out = W3CDom.asString(w3cDoc, W3CDom.OutputXml());+        assertFalse(out.contains(Parser.NamespaceXml), out);+        reparseNamespaceAware(out);+    }++    @Test void plainXmlKeepsXmlPrefixedAttributesAndExplicitNamespaces() throws Exception {+        String xml = "<root xml:lang=\"en\"><a xmlns=\"urn:a\"><b/></a><x:c xmlns:x=\"urn:x\"/><d/></root>";+        org.jsoup.nodes.Document jdoc = Jsoup.parse(xml, "", Parser.xmlParser());+        Document w3cDoc = new W3CDom().fromJsoup(jdoc);++        org.w3c.dom.Element root = w3cDoc.getDocumentElement();+        assertNull(root.getNamespaceURI());+        assertEquals("en", root.getAttributeNS(Parser.NamespaceXml, "lang"));++        NodeList kids = root.getChildNodes();+        assertEquals("urn:a", kids.item(0).getNamespaceURI());+        assertEquals("urn:a", kids.item(0).getFirstChild().getNamespaceURI());+        assertEquals("urn:x", kids.item(1).getNamespaceURI());+        assertNull(kids.item(2).getNamespaceURI());++        String out = W3CDom.asString(w3cDoc, W3CDom.OutputXml());+        assertFalse(out.contains("xmlns=\"" + Parser.NamespaceXml), out);+        Document reparsed = reparseNamespaceAware(out);+        org.w3c.dom.Element rRoot = reparsed.getDocumentElement();+        assertNull(rRoot.getNamespaceURI());+        assertEquals("en", rRoot.getAttributeNS(Parser.NamespaceXml, "lang"));+        assertEquals("urn:a", rRoot.getChildNodes().item(0).getNamespaceURI());+        assertEquals("urn:x", rRoot.getChildNodes().item(1).getNamespaceURI());+        assertNull(rRoot.getChildNodes().item(2).getNamespaceURI());+    }++    @Test void plainXmlUndeclaredPrefixNotBoundToXmlNamespace() {+        org.jsoup.nodes.Document jdoc = Jsoup.parse("<root><foo:bar>x</foo:bar><xml:baz/></root>", "", Parser.xmlParser());+        Document w3cDoc = new W3CDom().fromJsoup(jdoc);++        NodeList kids = w3cDoc.getDocumentElement().getChildNodes();+        Node fooBar = kids.item(0);+        assertEquals(Node.ELEMENT_NODE, fooBar.getNodeType());+        assertEquals("foo:bar", fooBar.getNodeName());+        assertNotEquals(Parser.NamespaceXml, fooBar.getNamespaceURI());++        Node xmlBaz = kids.item(1); // the xml: prefix keeps its reserved namespace+        assertEquals("xml:baz", xmlBaz.getNodeName());+        assertEquals(Parser.NamespaceXml, xmlBaz.getNamespaceURI());++        String out = W3CDom.asString(w3cDoc, W3CDom.OutputXml());+        assertFalse(out.contains(Parser.NamespaceXml), out);+        assertTrue(out.contains("<root><foo:bar>x</foo:bar><xml:baz/></root>"), out);+    }+     private static Stream<Arguments> parserProvider() {         return Stream.of(             Arguments.of(Parser.htmlParser()), 
Merged fix · PR #+58 −1
CHANGES.mdindex aa3a1545..2a654efe 100644@@ -10,6 +10,7 @@  ### Bug Fixes * Fixed HTML parsing of mixed-case RCDATA end tags after tag-shaped text. For example, `<title><p>Foo</TiTLE>` and `<textarea><img src=x></TeXtArEa>` now keep the tag-shaped content as text instead of promoting it to markup. [#2503](https://github.com/jhy/jsoup/issues/2503)+* Fixed `W3CDom` XML conversion so plain XML elements don't serialize with the reserved XML namespace as the default namespace. Explicit XML namespaces and `xml:*` attributes are still preserved. [#2504](https://github.com/jhy/jsoup/issues/2504)  ### Build Changes * Cleaned up the Maven build for the multi-release JAR so Java 8 and Java 11+ sources compile as separate source sets. This avoids spurious Java 8 compiler warnings from newer-language overlay sources, keeps long-running parser checks behind an explicit profile, and preserves the same published artifacts and runtime behavior.src/main/java/org/jsoup/helper/W3CDom.javaindex b26cb93f..f4d2150f 100644@@ -4,6 +4,7 @@ import org.jsoup.internal.Normalizer; import org.jsoup.internal.StringUtil; import org.jsoup.nodes.Attribute; import org.jsoup.parser.HtmlTreeBuilder;+import org.jsoup.parser.Parser; import org.jsoup.select.NodeVisitor; import org.jsoup.select.Selector; import org.w3c.dom.Comment;@@ -358,7 +359,7 @@ public class W3CDom {         public void head(org.jsoup.nodes.Node source, int depth) {             if (source instanceof org.jsoup.nodes.Element) {                 org.jsoup.nodes.Element sourceEl = (org.jsoup.nodes.Element) source;-                String namespace = namespaceAware ? sourceEl.tag().namespace() : null;+                @Nullable String namespace = namespaceAware ? w3cNamespace(sourceEl) : null;                 String tagName = Normalizer.xmlSafeTagName(sourceEl.tagName());                 try {                     // use an empty namespace if none is present but the tag name has a prefix@@ -390,6 +391,14 @@ public class W3CDom {             }         } +        private static @Nullable String w3cNamespace(org.jsoup.nodes.Element sourceEl) {+            // In W3C DOM, plain XML elements have no namespace; XML namespace is reserved for the {@code xml} prefix+            String namespace = sourceEl.tag().namespace();+            if (Parser.NamespaceXml.equals(namespace) && sourceEl.tag().prefix().isEmpty())+                return null;+            return namespace;+        }+         private void append(Node append, org.jsoup.nodes.Node source) {             append.setUserData(SourceProperty, source, null);             dest.appendChild(append);src/test/java/org/jsoup/helper/W3CDomTest.javaindex b9d1d330..c689db55 100644@@ -146,6 +146,53 @@ public class W3CDomTest {         assertEquals("img", img.getNodeName());     } +    @Test+    public void plainXmlElementsDoNotUseReservedXmlNamespace() {+        String input = "<root xml:lang=\"en\"><child>One</child></root>";+        org.jsoup.nodes.Document jsoupDoc = Jsoup.parse(input, "", Parser.xmlParser());++        Document w3Doc = new W3CDom().fromJsoup(jsoupDoc);+        org.w3c.dom.Element root = w3Doc.getDocumentElement();+        org.w3c.dom.Element child = (org.w3c.dom.Element) root.getFirstChild();++        assertNull(root.getNamespaceURI());+        assertNull(child.getNamespaceURI());+        assertEquals("en", root.getAttributeNS(Parser.NamespaceXml, "lang"));++        String xml = W3CDom.asString(w3Doc, W3CDom.OutputXml());+        assertFalse(xml.contains("xmlns=\"" + Parser.NamespaceXml + "\""), xml);++        Document roundTrip = parseXml(xml, true);+        org.w3c.dom.Element roundRoot = roundTrip.getDocumentElement();+        assertNull(roundRoot.getNamespaceURI());+        assertEquals("en", roundRoot.getAttributeNS(Parser.NamespaceXml, "lang"));+    }++    @Test+    public void explicitXmlNamespacesRoundTripThroughW3cDom() {+        String input = "<root xmlns=\"urn:example\"><child>One</child><plain xmlns=\"\">Two</plain></root>";+        org.jsoup.nodes.Document jsoupDoc = Jsoup.parse(input, "", Parser.xmlParser());++        Document w3Doc = new W3CDom().fromJsoup(jsoupDoc);+        org.w3c.dom.Element root = w3Doc.getDocumentElement();+        org.w3c.dom.Element child = (org.w3c.dom.Element) root.getFirstChild();+        org.w3c.dom.Element plain = (org.w3c.dom.Element) child.getNextSibling();++        assertEquals("urn:example", root.getNamespaceURI());+        assertEquals("urn:example", child.getNamespaceURI());+        assertNull(plain.getNamespaceURI());++        String xml = W3CDom.asString(w3Doc, W3CDom.OutputXml());+        Document roundTrip = parseXml(xml, true);+        org.w3c.dom.Element roundRoot = roundTrip.getDocumentElement();+        NodeList plainEls = roundRoot.getElementsByTagName("plain");++        assertEquals("urn:example", roundRoot.getNamespaceURI(), xml);+        assertEquals(1, roundRoot.getElementsByTagNameNS("urn:example", "child").getLength(), xml);+        assertEquals(1, plainEls.getLength(), xml);+        assertNull(plainEls.item(0).getNamespaceURI(), xml);+    }+     @Test     public void handlesInvalidAttributeNames() {         String html = "<html><head></head><body style=\"color: red\" \" name\"></body></html>";