lemminx icon indicating copy to clipboard operation
lemminx copied to clipboard

Bind to grammar will add another namespace declaration even when there is an existing one

Open datho7561 opened this issue 4 years ago • 1 comments

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>

double-namespace

This happens for binding with XSD and binding with xml-model

datho7561 avatar Jun 11 '21 14:06 datho7561

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)

angelozerr avatar Jun 11 '21 17:06 angelozerr