Schema 'Restriction'

Hi,
I'm currently writing an XSD schema file and I want to use a simple type with a restriction that can be reused by any attribute/element in the schema.
This is the type that I want to be reused:

<xs:simpleType name="color">

<xs:restriction base="xs:string">

<xs:pattern value="#([0-9a-fA-F]){6}" />

<xs:length value="7" />

</xs:restriction>

</xs:simpleType>

And this an example of an element and its attribute that uses it as a type:

<xs:element name="Finish">

<xs:complexType>

<xs:attribute name="Color" type="color" use="required" />

</xs:complexType>

</xs:element>

However, I get the error that the namespace "is not available to be referenced in the schema". I have tried researching the problem and it still confuses me. Please let me know what I'm doing wrong here. Thanks...

Regards,
Alex



Answer this question

Schema 'Restriction'

  • mminor8

    Sounds like you need a crash course in how namespaces work in XSD.  The solution resides in your use of namespaces and targetNamespaces on the <xsd:schema> declaration.  For example, suppose you have:

    <xs:schema targetNamespace="uri:colors"
                     xmlns="uri:colors"
                     xmlns:xs=http://www.w3.org/2001/XMLSchema>
      <
    xs:simpleType name="color">
        <
    xs:restriction base="xs:string">
          <
    xs:pattern value="#([0-9a-fA-F]){6}" />
          <
    xs:length value="7" />
        </
    xs:restriction>
      </
    xs:simpleType>
    </
    xs:schema>


    This schema defines the simpleType "color" in the namespace "uri:colors".  Now the schema that is going to use this definition needs to import the schema correctly, and reference the type using proper namespace qualification as follows:

    <xs:schema targetNamespace="uri:drawing"
                     xmlns="uri:drawing"
                     xmlns:c="uri:colors"
                     xmlns:xs=http://www.w3.org/2001/XMLSchema>
        <
    xs:import namespace="uri:colors" schemaLocation="color.xsd"/>
        <
    xs:element name="Finish">
            <
    xs:complexType>
                <
    xs:attribute name="Color" type="c:color" use="required" />
            </
    xs:complexType>
        </
    xs:element>
    </
    xs:schema>

    Now, XSD also supports something like the #include from C or C++ where the included schema inherits the namespace of the parent schema, then you don't need to qualify the color attribute, as follows:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <
    xs:simpleType name="color">
        <
    xs:restriction base="xs:string">
          <
    xs:pattern value="#([0-9a-fA-F]){6}" />
          <
    xs:length value="7" />
        </
    xs:restriction>
      </
    xs:simpleType>
    </
    xs:schema>

    and

    <xs:schema targetNamespace="uri:drawing"
                     xmlns="uri:drawing"
                     xmlns:xs=http://www.w3.org/2001/XMLSchema>
      <
    xs:include schemaLocation="color2.xsd"/>
      <
    xs:element name="Finish">
        <
    xs:complexType>
          <
    xs:attribute name="Color" type="color" use="required" />
        </
    xs:complexType>
      </
    xs:element>
    </
    xs:schema>




  • mole

    thanks for the explanation there :) it's helped me understand...even though i actually managed to fix the document anyway (wthout then knowing how).

  • kateryn

    From further investigation I understand that the validator thinks that the 'point' type either wasn't declared or isn't a SimpleType. Maybe this will help.
  • Schema 'Restriction'