user2148983
user2148983

Reputation: 177

How to restrict content of an xsd:complexType with attributes?

I'm having trouble trying to add a minLength and maxLength restriction to this complex type. Here's my original code:

          <xs:element minOccurs="0" name="Division">
            <xs:complexType>
              <xs:sequence>
                <xs:element minOccurs="0" maxOccurs="unbounded" name="ID">
                  <xs:complexType>
                    <xs:simpleContent>
                      <xs:extension base="xs:string">
                        <xs:attribute ref="wd:type" use="optional" />
                      </xs:extension>
                    </xs:simpleContent>
                  </xs:complexType>
                </xs:element>
              </xs:sequence>
              <xs:attribute ref="wd:Descriptor" use="optional" />
            </xs:complexType>
          </xs:element>

I want to add this restriction but I don't know how to do that.

<xs:restriction base="xs:string">
  <xs:minLength value="0"/>
  <xs:maxLength value="100"/>
</xs:restriction>                        

I know how to do this for a simpletype, but in a complextype I don't know how to do it. Can anyone help?

Upvotes: 0

Views: 989

Answers (1)

kjhughes
kjhughes

Reputation: 111541

You need to define and name a xs:simpleType and extend that in your xs:complexType.

Here is a complete, working example (with dangling xs:attribute/@ref occurrences replaced with temporary xs:attribute/@names so the example can stand on its own):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           version="1.0">
  <xs:element name="Division">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="unbounded" name="ID">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="IDType">
                <xs:attribute name="attr1" use="optional" />
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="attr2" use="optional" />
    </xs:complexType>
  </xs:element>
  <xs:simpleType name="IDType">  
    <xs:restriction base="xs:string">
      <xs:minLength value="0"/>
      <xs:maxLength value="100"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

Upvotes: 1

Related Questions