Search code examples
attributesxsdelementrestriction

XSD element restricted with attribute


Hi I have problem with schema, I try to create restricted element with attribute like this:

    <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

      <xs:simpleType name="turn">
        <xs:restriction base="xs:unsignedShort">
          <xs:minInclusive value="0"/>
          <xs:maxInclusive value="360"/>
        </xs:restriction>
      </xs:simpleType>

      <xs:element name = "TURN_LEFT" type="turn" nillable="false">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="xs:unsignedShort">
              <xs:attribute type="xs:string" name="description" use="optional"/>
            </xs:extension>
          </xs:simpleContent>
        </xs:complexType>
      </xs:element>

      <xs:element name = "FORWARD" nillable="false">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="xs:unsignedShort">
              <xs:attribute type="xs:string" name="description" use="optional"/>
            </xs:extension>
          </xs:simpleContent>
        </xs:complexType>
      </xs:element>

    </xs:schema>

Error description on open tag of TURN_LEFT "type attribute can not be present together with the element of simpleType or complexType".

How to use restriction with attribute ?


Solution

  • Appears that element "turn" is used by TURN_LEFT in the schema . However there are two explicit declarations of the same element which are not needed . You can try the below correction:

    <xs:element name = "TURN_LEFT" type="turn" nillable="false">
        <!-- Commented TURN_LEFT declaration -->
        <!--<xs:complexType>
            <xs:simpleContent>
                <xs:extension base="xs:unsignedShort">
                    <xs:attribute type="xs:string" name="description" use="optional"/>
                </xs:extension>
            </xs:simpleContent>
        </xs:complexType>-->
    </xs:element>
    
    <xs:simpleType name="turn">
        <xs:restriction base="xs:unsignedShort">
            <xs:minInclusive value="0"/>
            <xs:maxInclusive value="360"/>
        </xs:restriction>
    </xs:simpleType>
    
    
    
    <xs:element name = "FORWARD" nillable="false">
        <xs:complexType>
            <xs:simpleContent>
                <xs:extension base="xs:unsignedShort">
                    <xs:attribute type="xs:string" name="description" use="optional"/>
                </xs:extension>
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>