Reputation: 167
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 ?
Upvotes: 1
Views: 755
Reputation: 96
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>
Upvotes: 2
Reputation: 6016
You can't have in an element both a type attribute and a complexType (or simpleType) child.
TURN_LEFT could be a element of type turn (attribute type="turn"), OR TURN_LEFT should be a element of type the complexType defined inside the element.
Look at this simple examaple:
<xs:element name="myElement" type="xs:boolean">
<xs:restriction base="float">
</xs:restriction>
</xs:element>
If you define an element like that the XML Schema its not correct because is ambigous (how do we know if myElement its of boolean type or float type if you have defined both times the type?).
I think this is what you want to do:
<xs:element name = "TURN_LEFT" nillable="false">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="turn">
<xs:attribute type="xs:string" name="description" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
The base (the type you are extending) its the turn type. This will validate this element:
<TURN_LEFT description="cool">12</TURN_LEFT>
Upvotes: 1