How to link XML to XSD using schemaLocation or noNamespaceSchemaLocation?

How to link an XSD to an XML document depends upon whether the XML document is using namespaces or not…

Without namespaces

Use xsi:noNamespaceSchemaLocation to provide a hint as to the XSD to be used:

  • XML

    <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="example.xsd">
      <!-- ... -->
    </root>
    
  • XSD

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <xsd:element name="root">
        <!-- ... -->
      </xsd:element>
    </xsd:schema>
    

With namespaces

Use xsi:schemaLocation to provide a hint as to the XSD to be used:

  • XML

    <ns:root xmlns:ns="http://example.com/ns"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://example.com/ns example-ns.xsd">
      <!-- ... -->
    </ns:root>
    
  • XSD

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://example.com/ns">
      <xsd:element name="root">
        <!-- ... -->
      </xsd:element>
    </xsd:schema>
    

Leave a Comment