Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 636
Louis Rhys Avatar asked Dec 03 '25 12:12

Louis Rhys


2 Answers

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>
like image 64
Petru Gardea Avatar answered Dec 06 '25 06:12

Petru Gardea


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>
like image 36
AbdElRaheim Avatar answered Dec 06 '25 06:12

AbdElRaheim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!