title: How to set a nested use of an XML namespace
body: <p>We have defined a namespace <code>xmlns:abc="http://example.org/ns/abc"</code> and an XML schema for it. This XML schema defines elements and data types.</p>
  
  <p>This <code>abc</code> XML schema includes </p>
  
  <pre><code>&lt;xs:any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded"&gt;
  </code></pre>
  
  <p>which allows to use elements from an other namespace.</p>
  
  <p>Next we have defined a namespace <code>xmlns:testing="http://example.org/ns/testing"</code> and an XML schema for it. This testing XML Schema imports the abc namespace and its XML schema by <code>&lt;xs:import namespace="http://example.org/ns/abc" schemaLocation="abc.xsd"/&gt;</code> as some of the data types defined in the abc namespace should be used for elements of the testing namespace.</p>
  
  <p>Finally we've created an XML document making use of both namespaces and XML Schemas:</p>
  
  <pre><code>&lt;rootElement xmlns="http://example.org/ns/abc" xmlns:testing="http://example.org/ns/abc" xsi:schemaLocation="http://example.org/ns/abc abc.xsd http://example.org/ns/testing testing.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
      &lt;abcElement1&gt;Some text&lt;/abcElement1&gt;
      &lt;testing:element1/&gt;
  &lt;/rootElement&gt;
  </code></pre>
  
  <p>Validating this XML document we get errors "...element name... is a already declared" pointing at <a href="https://www.w3.org/TR/xmlschema-1/#sch-props-correct" rel="nofollow noreferrer">https://www.w3.org/TR/xmlschema-1/#sch-props-correct</a> </p>
  
  <p>This sounds like it is not possible to import a namespace with its schema into another namespace if this other namespace is used on the context of the first namespace/schema - a kind of nested reuse of it.</p>
  
  <p>Is there a better way of nesting the abc namespace/schema or is the only alternative to split the abc namespace into one holding only elements and another, new one holding data type definitions and importing this data-type-namespace into the testing namespace/schema?</p>
  
  <p>Here are the full examples:
  abc.xsd</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://example.org/ns/abc" targetNamespace="http://example.org/ns/abc" elementFormDefault="qualified" attributeFormDefault="unqualified"&gt;
      &lt;xs:element name="rootElement"&gt;
          &lt;xs:annotation&gt;
              &lt;xs:documentation&gt;Comment describing your root element&lt;/xs:documentation&gt;
          &lt;/xs:annotation&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element name="abcElement1" type="xs:string" minOccurs="0"/&gt;
                  &lt;xs:element name="abcEntity" minOccurs="0" maxOccurs="unbounded"/&gt;
                  &lt;xs:any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded"/&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
      &lt;xs:complexType name="EntityType1"&gt;
          &lt;xs:sequence&gt;
              &lt;xs:element name="identifier" type="xs:string"/&gt;
              &lt;xs:element name="name" type="xs:string" minOccurs="0" maxOccurs="unbounded"/&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>testing.xsd</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://example.org/ns/testing" xmlns:abc="http://example.org/ns/abc" targetNamespace="http://example.org/ns/testing" elementFormDefault="qualified" attributeFormDefault="unqualified"&gt;
      &lt;xs:import namespace="http://example.org/ns/abc" schemaLocation="abc.xsd"/&gt;
      &lt;xs:element name="element1"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element name="person" type="abc:EntityType1" minOccurs="0"/&gt;
                  &lt;xs:element name="company" type="abc:EntityType1" minOccurs="0"/&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>Example document 1 - referencing the schemas as local files:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;rootElement xmlns="http://example.org/ns/abc" xmlns:testing="http://example.org/ns/testing"  xsi:schemaLocation="http://example.org/ns/abc abc.xsd http://example.org/ns/testing testing.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
      &lt;abcElement1&gt;Some text&lt;/abcElement1&gt;
      &lt;testing:element1&gt;
          &lt;testing:person&gt;
              &lt;identifier&gt;http://example.com/entity/134134234&lt;/identifier&gt;
          &lt;/testing:person&gt;
      &lt;/testing:element1&gt;
  &lt;/rootElement&gt;
  </code></pre>
  
  <p>... and this document can be validated, no error is thrown.</p>
  
  <p>Then I uploaded the abc.xsd to one of my web sites and replace the local file name by its URL:</p>
  
  <pre><code>xsi:schemaLocation="http://example.org/ns/abc http://data.newsit.biz/fortesting/abc.xsd http://example.org/ns/testing testing.xsd"
  </code></pre>
  
  <p>... and the example document with this xsi:schemaLocation throws the error mentioned above. 
  Why makes having an XML Schema as local file or as web resource a difference?</p>
  
  <p>Software used for validation: XMLspy 2018 rel2 sp1</p>
  
  <p>The error messages:</p>
  
  <ul>
  <li>File exampleDocument2.xml is not valid</li>
  <li>The schemas referenced by xsi:schemaLocation or xsi:noNamespaceSchemaLocation attributes at element  are not valid.</li>
  <li>'abc:rootElement' is already declared.</li>
  <li>a link to <a href="https://www.w3.org/TR/xmlschema-1/#sch-props-correct" rel="nofollow noreferrer">https://www.w3.org/TR/xmlschema-1/#sch-props-correct</a></li>
  <li>This "... already declared and the link is shown also for abc:EntityType1 </li>
  </ul>