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

StackOverflow https://stackoverflow.com/questions/13150465

  •  22-07-2021
  •  | 
  •  

Question

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?

Était-ce utile?

La solution

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>

Autres conseils

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>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top