Search code examples
xmlxsdxsd-validationxml-validation

xsd:complexType with children, attributes, and restrictions


I'm working on creating a schema, but am getting stuck near my root element with defining it as a complexType that has children, attributes, and restrictions on those attributes

This is what I have tried so far.... (venetian blind format)

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsd:element name="foos">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="foo" type="FooType" minOccurs="1" maxOccurs="unbounded"/>
        </xsd:sequence>         
    </xsd:complexType>
</xsd:element>
<xsd:complexType name="FooType">
    <xsd:attribute name="exchangeType" type="xsd:string" use="required">
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
                <xsd:enumeration value="S" />
                <xsd:enumeration value="T" />
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:attribute>
    <xsd:sequence>
        <xsd:element name="thing1" type="Thing1Type" />
        <xsd:element name="thing2" type="Thing2Type" />
    </xsd:sequence>
</xsd:complexType>
</xsd:schema>

I've been having trouble finding a way to incorporate this attribute and its restrictions

Any thoughts?


Solution

  • Two main corrections:

    1. An xsd:attribute declaration cannot have both a local xsd:simpleType and a @type attribute; deleted the @type attribute.
    2. An xsd:attribute declaration cannot appear before xsd:sequence; move it after.

    XSD with corrections applied:

    This XSD has the above corrections and a few other minor fixes applied and is now valid:

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <xsd:element name="foos">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="foo" type="FooType" 
                         minOccurs="1" maxOccurs="unbounded"/>
          </xsd:sequence>         
        </xsd:complexType>
      </xsd:element>
      <xsd:complexType name="FooType">
        <xsd:sequence>
          <xsd:element name="thing1" type="Thing1Type" />
          <xsd:element name="thing2" type="Thing2Type" />
        </xsd:sequence>
        <xsd:attribute name="exchangeType" use="required">
          <xsd:simpleType>
            <xsd:restriction base="xsd:string">
              <xsd:enumeration value="S" />
              <xsd:enumeration value="T" />
            </xsd:restriction>
          </xsd:simpleType>
        </xsd:attribute>
      </xsd:complexType>
      <xsd:complexType name="Thing1Type"/>
      <xsd:complexType name="Thing2Type"/>
    </xsd:schema>