XML Schema (XSD) from an XML document. Namespaces

I've been working for two days now. The only problem is how to allocate certain element attributes to a separate namespace. That all this would normally pass validation

There is an XML of the form:

<?xml version="1.0" encoding="utf-8"?>
<main xmlns:ns="myNamespace">
  <institution name="Green_River" ns:type="college">
    <faculty number="27015" ns:specialty="international_elationships">
      <student firstName="Mike" lastName="Junior" age="18" ns:location="NY" />
    </faculty>
  </institution>
</main>

I can't figure out how to make a diagram out of all this. So far, it has turned out like this:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="unqualified"
           attributeFormDefault="unqualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:ns="myNamespace"
    targetNamespace="myNamespace"
>
  <xs:element name="main">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="institution" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="faculty" maxOccurs="unbounded">
                <xs:complexType>
                 <xs:sequence>
                    <xs:element name="student" maxOccurs="unbounded">
                      <xs:complexType>
                        <xs:attribute name="firstName" type="xs:string" use="required"/>
                        <xs:attribute name="lastName" type="xs:string" use="required"/>
                        <xs:attribute name="age" type="xs:positiveInteger"/>
                        <xs:attribute ref="ns:location"/>
                      </xs:complexType>
                    </xs:element>
                 </xs:sequence>
                 <xs:attribute name="number" type="xs:positiveInteger" use="required"/>
                  <xs:attribute ref="ns:specialty" use="required"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="name" type="xs:string" use="required"/>
            <xs:attribute ref="ns:type"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

      <xs:attribute name="location" type="xs:string"/>
      <xs:attribute name="specialty" type="xs:string"/>
      <xs:attribute name="type" type="xs:string"/>
</xs:sсhema>

With this scheme, the validator (VS) requires that all tags and elements are assigned to the ns space. Can you tell me what the problem is?

 0
Author: Данила, 2018-01-22

1 answers

The trick is that the elementFormDefault="unqualified" setting does not apply to elements that are declared in the schema itself, only nested elements inside the complexType can be unqualified.

You need to create two schemas. One will be without the target namespace and will describe the root tag. The second will describe the attributes that require the namespace to be specified:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns="myNamespace" targetNamespace="myNamespace">
  <xs:attribute name="location" type="xs:string"/>
  <xs:attribute name="specialty" type="xs:string"/>
  <xs:attribute name="type" type="xs:string"/>
</xs:schema>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns="myNamespace">
  <xs:import namespace="myNamespace" schemaLocation="..." />

  <xs:element name="main">
    <xs:complexType>
      <!-- ... -->
    </xs:complexType>
  </xs:element>
</xs:schema>

In the document itself, you can leave a hint for the editors (for example, for the same studio) about the location schemes:

<ns:main xmlns:ns="myNamespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="...">
 1
Author: Pavel Mayorov, 2018-01-26 06:50:11