{"record":{"id":"5bcaff4e7cd42918","repo":"apache/maven","slug":"unexpected-namespace-for-element-s-found-s","errorCode":null,"errorMessage":"Unexpected namespace for element '%s': found '%s' but expected '%s'","messagePattern":"Unexpected namespace for element '(.+?)': found '(.+?)' but expected '(.+?)'","errorType":"exception","errorClass":"XMLStreamException","httpStatus":null,"severity":"error","filePath":"src/mdo/reader-stax.vm","lineNumber":523,"sourceCode":"        ${classLcapName}.${field.name}(${field.name});\n    #end\n  #end\n  #foreach ( $field in $allFields )\n    #if ( $Helper.xmlFieldMetadata( $field ).format )\n        ${classLcapName}.${field.name}($Helper.xmlFieldMetadata( $field ).format);\n    #end\n  #end\n        return ${classLcapName}.build();\n    }\n\n #end\n#end\n\n    private void checkNamespace(XMLStreamReader parser, boolean strict, String namespace) throws XMLStreamException {\n        if (strict) {\n            String ns = parser.getNamespaceURI();\n            if (!Objects.equals(namespace, ns)) {\n                throw new XMLStreamException(\n                    String.format(\"Unexpected namespace for element '%s': found '%s' but expected '%s'\",\n                        parser.getLocalName(),\n                        ns != null ? ns : \"no namespace\",\n                        namespace),\n                    parser.getLocation(),\n                    null\n                );\n            }\n        }\n    }\n\n    private String checkDuplicate(String tagName, XMLStreamReader parser, Set<String> parsed) throws XMLStreamException {\n#set( $aliases = { } )\n#set( $flats = { } )\n#foreach( $class in $model.allClasses )\n  #foreach ( $field in $class.getFields($version) )\n    #set ( $fieldTagName = $Helper.xmlFieldMetadata( $field ).tagName )\n    #if ( ! $fieldTagName )","sourceCodeStart":505,"sourceCodeEnd":541,"githubUrl":"https://github.com/apache/maven/blob/e4093d4e120eac99d6bdce5ba67cace2f3085c97/src/mdo/reader-stax.vm#L505-L541","documentation":"checkNamespace(parser, strict, namespace) runs for namespace-aware models: in strict mode each element's effective NamespaceURI must equal the namespace the generated parser carries (the model's target namespace), compared with Objects.equals. A null found-namespace is reported as 'no namespace'. It rejects documents in the wrong namespace - or none - before their fields are read.","triggerScenarios":"Strict read of a document whose elements carry no namespace (e.g. a legacy Maven 1 project.xml), or whose namespace URI differs from what the reader's model declares (4.0.0 vs 4.1.0 style version skew between document and generated reader).","commonSituations":"Missing xmlns declaration on the root element; typo'd or outdated namespace URI; hand-written XML without namespaces; a document written for a newer model parsed by a reader generated from an older one.","solutions":["Add or correct the default namespace on the root element to the model's namespace URI (e.g. xmlns=\"http://maven.apache.org/POM/4.0.0\").","Use a reader generated from the model version that matches the document's namespace.","For legacy non-namespaced documents, use the dedicated legacy reader or read with strict=false if acceptable."],"exampleFix":"<!-- before -->\n<project>\n  <modelVersion>4.0.0</modelVersion>\n</project>\n\n<!-- after -->\n<project xmlns=\"http://maven.apache.org/POM/4.0.0\">\n  <modelVersion>4.0.0</modelVersion>\n</project>","handlingStrategy":"validation","validationCode":"byte[] bytes = in.readAllBytes();\nXMLInputFactory f = XMLInputFactory.newFactory();\nXMLStreamReader peek = f.createXMLStreamReader(new ByteArrayInputStream(bytes));\nwhile (peek.hasNext()) {\n    if (peek.next() == XMLStreamReader.START_ELEMENT) break;\n}\nString ns = peek.getNamespaceURI();\npeek.close();\nif (!\"http://maven.apache.org/POM/4.0.0\".equals(ns)) {\n    throw new IllegalArgumentException(\"Unexpected namespace: \" + ns);\n}","typeGuard":"static boolean isNamespaceMismatch(XMLStreamException e) {\n    return e.getMessage() != null && e.getMessage().startsWith(\"Unexpected namespace\");\n}","tryCatchPattern":"try {\n    Model model = reader.read(parser, true);\n} catch (XMLStreamException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Unexpected namespace\")) {\n        // document is in the wrong/absent namespace: route it to the right reader version\n    }\n    throw e;\n}","preventionTips":["Pin document namespace versions and reader model versions together.","Validate the root namespace URI before strict reads.","Reject namespace-less XML early at intake."],"tags":["xml","stax","namespace","strict-mode","validation"],"backgroundTag":"xml-namespace-mismatch","analyzedSha":"e4093d4e120eac99d6bdce5ba67cace2f3085c97","analyzedAt":"2026-08-21T22:58:24.034Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}