Include / import local schemas that have namespaces

Here is a schematic file midi.xsd

that defines the type note

used to store MIDI note values:

<?xml version="1.0" encoding="utf-8"?>

<xs:schema elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:simpleType name="note">
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="0"/>
      <xs:maxInclusive value="127"/>
    </xs:restriction>
  </xs:simpleType>

</xs:schema>

      

Here is another schema file octaves.xsd

that it uses midi.xsd

to help define the layout to be applied in the XML file that contains the octave data:

<?xml version="1.0" encoding="utf-8"?>

<xs:schema elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:include schemaLocation="midi.xsd"/>

  <xs:element name="octaves">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="octave">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="name" type="xs:string" />

              <xs:element name="midi">
                <xs:complexType>
                  <xs:sequence>

                    <xs:element name="value" type="xs:integer" />
                    <xs:element name="from" type="note" />
                    <xs:element name="to" type="note" />

                  </xs:sequence>
                </xs:complexType>
              </xs:element>

            </xs:sequence>

            <xs:attribute name="index" type="xs:integer" />

          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>

      

This is fine and works exactly as you'd expect, except that I have another requirement: I want to note

be in its own namespace midi

, so

<xs:element name="from" type="note" />

      

becomes

<xs:element name="from" type="midi:note" />

      

Try as best I could, I can't seem to get this to work. My attempts have included using the attribute targetNamespace

in different places, element import

and liberal use xmlns:midi="..."

, but to no avail. I would bet one of these attempts if it wasn't that worth eating.

Can some soul point me in the right direction? I'm pretty sure the problem is with what midi.xsd

is the local file
; it has never been and never will be hosted on a web server.

+3


source to share


1 answer


Change midi.xsd

as follows:

<xs:schema elementFormDefault="qualified"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       targetnamespace="/my/midi/namespace">

      

And then change octaves.xsd

to say:

<xs:schema elementFormDefault="qualified"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:midi="/my/midi/namespace">

   <xs:import namespace="/my/midi/namespace" schemaLocation="midi.xsd"/>

   ...

   <xs:element name="from" type="midi:note" />

      



Pay attention to the use xs:import

, not <xs:include>

. Both meanings are very different: you use import

to enter other namespaces and include

to include other schema files inline in the current namespace.

Note that it /my/midi/namespace

can be anything you want, it is an arbitrary identifier.

I'm pretty sure the problem is because midi.xsd is a local file

No, it doesn't matter.

+3


source







All Articles