1

I have the following XML-snippet.

<grade term="One" type="simple">4.7</grade>
<grade term="Two" type="complex">2.1</grade>

The attribute type can either be "simple" or "complex". The simple type can contain all decimals from 1.0 to 5.0 and the complex type can only contain the values match the pattern [123]\.[037]|5\.0|4\.[07].

How can I build an XSD to validate the content of the grade depending on the selected type (either simple or complex) ?

I've come up with:

<xs:simpleType name="complex">
    <xs:restriction base="xs:decimal">
        <xs:minInclusive value="1.0"/>
        <xs:maxInclusive value="5.0"/>
    </xs:restriction>
</xs:simpleType>

<xs:simpleType name="simple">
    <xs:restriction base="xs:decimal">
        <xs:pattern value="[123]\.[037]|5\.0|4\.[07]"/>
    </xs:restriction>
</xs:simpleType>

<xs:complexType name="gradeType">
  <xs:simpleContent>
    <xs:extension base="complex">
      <xs:attribute name="term" use="required" type="xs:NCName"/>
      <xs:attribute name="type" use="required" type="complex"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>

but this generates a lot of errors:

Cvc-datatype-valid.1.2.1: 'simple' Is Not A Valid Value For 'decimal'., Line '8', Column '66'. Cvc-attribute.3: The Value 'simple' Of Attribute 'type' On Element 'grade' Is Not Valid With Respect To Its Type, 'complex'., Line '8', Column '66'. Cvc-datatype-valid.1.2.1: 'simple' Is Not A Valid Value For 'decimal'., Line '9', Column '66'. Cvc-attribute.3: The Value 'simple' Of Attribute 'type' On Element 'grade' Is Not Valid With Respect To Its Type, 'complex'., Line '9', Column '66'. Cvc-datatype-valid.1.2.1: 'simple' Is Not A Valid Value For 'decimal'., Line '10', Column '66'. Cvc-attribute.3: The Value 'simple' Of Attribute 'type' On Element 'grade' Is Not Valid With Respect To Its Type, 'complex'., Line '10', Column '66'.

Thanks a lot :-)

Alex
  • 1,857
  • 3
  • 36
  • 51
  • What you want to do requires XSD 1.1. See duplicate link for an example. – kjhughes Nov 14 '16 at 04:44
  • Not even with type inheritance ? I'm only allowed to use 1.0 so if there is no other solution this is unsolvable. Thanks. – Alex Nov 14 '16 at 04:57
  • Conditional Type Assignment requires XSD 1.1. In XSD 1.0, you differentiate type by element name: `simpleGrade` and `complexGrade`. – kjhughes Nov 14 '16 at 05:34

0 Answers0