lemminx
lemminx copied to clipboard
Bind to grammar will add another namespace declaration even when there is an existing one
If you have an XML document with a namespace declaration:
<root xmlns="http://example.org/schemaNamespace">
<aaa />
</root>
And then use the CodeLens to bind to a grammar with targetNamespace eg.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://foo" xmlns:ns="http://foo">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element
ref="ns:aaa"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="aaa" />
</xs:schema>
Then you will end up with two xmlns in the XML document:
<?xml-model href="document.xsd"?>
<root xmlns="http://example.org/schemaNamespace" xmlns="http://foo" >
<aaa />
</root>

This happens for binding with XSD and binding with xml-model
To fix that we have 2 solutions:
- don't generate the namespace if XML declares already a namespace
- replace the existing namespace with the new namespace.
Perhaps the first solution could be enough (and easy to implement)