Where to declare a simpleType so that it can be used in several elemets in the same XSD?

4.5k views Asked by At

Now I have an XSD file (e.g. FOO_SCHEMA.xsd) that looks something like this:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="FOO">
        <xs:annotation>
            <xs:documentation>Comment</xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element name="BAR1" type="xs:string" />
                <xs:element name="BAR2" type="xs:string" />
                <xs:element name="BAR3" type="xs:string" />
                <xs:element name="BAR4" type="xs:string" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

I want to modify two of the elements (say BAR1 and BAR2) to use a single time. I know how to use it for one of them,

<xs:element name="BAR1">
   <xs:simpleType>
       <!-- my type definition here -->
   </xs:simpleType>
</xs:element>

But I want to use it in two (and maybe more) elements, and I am not sure how I can do this without copying the same simple type definition everywhere. From googling, it seems that I need to declare the type with a name

But I don't know where to put this declaration. I try to put it in the same level in as xs:complexType and several other level, but it was rejected by the schema validator. Any idea where to put this declaration? Any idea?

2

There are 2 answers

3
Petru Gardea On BEST ANSWER

All referenceable components in an XSD file must be placed directly under the xs:schema element. In other words, your global simple type declarations must be siblings of the FOO element.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="FOO">
        <xs:annotation>
            <xs:documentation>Comment</xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element name="BAR1" type="BAR" />
                <xs:element name="BAR2" type="BAR" />
                <xs:element name="BAR3" type="xs:string" />
                <xs:element name="BAR4" type="xs:string" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:simpleType name="BAR">
        <xs:restriction base="xs:string">
            <xs:pattern value="BAR"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>
0
AbdElRaheim On

Define global and use the type attribute to specify the type.

  <xs:simpleType name="test">
    <xs:annotation>
      <xs:documentation>
        Defines a string that is between 1 and 300 chars
      </xs:documentation>
    </xs:annotation>
    <xs:restriction base="xs:string">
      <xs:minLength value="1" />
      <xs:maxLength value="300" />
    </xs:restriction>
  </xs:simpleType>

  <xs:element name="foo">
    <xs:complexType>
      <xs:attribute type="test" name="bar"/>
    </xs:complexType>   
  </xs:element>