Search code examples
xmlxsdrestriction

Add restrictions on attributes of different type


I want to do something totally simple and obvious, but I can't find any example about it on internet. All the examples I found always involve a single attribute.

I want to create an element like this :

I have the following XSD, but I'm getting an error saying that the restriction element is misplaced or appears too often :

<xs:element name="blabla">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Images" minOccurs="0" maxOccurs="1">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="image" type="imageType" minOccurs="0" maxOccurs="unbounded" />
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:complexType name="imageType">
    <xs:sequence>
        <xs:element name="Image" minOccurs="0" maxOccurs="unbounded">
            <xs:complexType>
                <xs:complexContent>
                    <xs:restriction base="xs:integer">
                        <xs:attribute name="id" type="xs:integer" use="required" />
                    </xs:restriction>
                    <xs:restriction base="xs:string">
                        <xs:attribute name="src" type="xs:anyURI" use="required" />
                    </xs:restriction>
                </xs:complexContent>
            </xs:complexType>
        </xs:element>
    </xs:sequence>
</xs:complexType>

How can I add a different restriction for each attribute ?


Solution

  • Yes, you can certainly have attributes with different restrictions, e.g:

      <xs:complexType name="imageType">
        <xs:sequence>
          <xs:element name="Image" minOccurs="0" maxOccurs="unbounded">
            <xs:complexType>
              <xs:attribute name="id" use="required">
                <xs:simpleType>
                  <xs:restriction base="xs:integer">
                    <xs:minExclusive value="0"/>
                    <xs:maxExclusive value="100"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:attribute>
              <xs:attribute name="src" use="required">
                <xs:simpleType>
                  <xs:restriction base="xs:anyURI">
                    <xs:maxLength value="200"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:attribute>
            </xs:complexType>
          </xs:element>
        </xs:sequence>
      </xs:complexType>