Supplying a schema

exercise No. 19

Integrity constraints

Q:

Create a suitable XML schema enforcing the following integrity constraints:

  • All elements are required.

  • The element <price> is of decimal type having two fractional digits.

  • The elements <year> and @id are of integer type.

  • The attribute @id defines a key.

A:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
		  
		  <xs:simpleType name="money">
		  <xs:restriction base="xs:decimal">
		  <xs:fractionDigits value="2"/>
		  </xs:restriction>
		  </xs:simpleType>
		  
		  <xs:element name="catalog">
		  <xs:complexType>
		  <xs:sequence>
                  <xs:element ref="cd" minOccurs="0" maxOccurs="unbounded"/>
		  </xs:sequence>
		  </xs:complexType>
		  <xs:key name="uniqueId">
		  <xs:selector xpath="cd"/>
		  <xs:field xpath="@id"/>
		  </xs:key>
		  </xs:element>
		  
		  <xs:element name="cd">
		  <xs:complexType>
		  <xs:sequence>
                  <xs:element name="title" type="xs:string"/>
                  <xs:element name="artist" type="xs:string"/>
                  <xs:element name="country" type="xs:string"/>
                  <xs:element name="company" type="xs:string"/>
                  <xs:element name="price" type="money"/>
                  <xs:element name="year" type="xs:int"/>
		  </xs:sequence>
		  <xs:attribute name="id" type="xs:int" use="required"/>
		  </xs:complexType>
		  </xs:element>
		  
		  </xs:schema>