XSD

Exercises

XSD Exercises

Try to explore all the possibilities offered by the XSD files and don’t forget to validate your documents using, for example: http://www.freeformatter.com/xml-validator-xsd.html

1.

Describe a group of five people in a valid XML document according to the following XSD:

a)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="group">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="person" minOccurs="5" maxOccurs="20" type="xs:string"/>
      </xs:sequence>
      <xs:attribute name="name" use="required" type="xs:string"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

b)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="group">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="person" minOccurs="5" maxOccurs="20" type="xs:string"/>
      </xs:sequence>
      <xs:attribute name="name" use="required" type="xs:string"/>
      <xs:attribute name="description" use="required" type="xs:string">
        <xs:annotation>
          <xs:documentation>The element xs:annotation can be used to explain the element or attribute being defined</xs:documentation>
          <xs:documentation>The attribute description describes the group.</xs:documentation>
        </xs:annotation>
      </xs:attribute>
    </xs:complexType>
  </xs:element>
</xs:schema>

2.

Describe a set of tennis matches according to the following XSD:

a)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="matches">
    <xs:annotation>
      <xs:documentation>This element describes tennis matches</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence>
        <xs:element name="match" minOccurs="3" maxOccurs="5">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="referee" type="xs:string"/>
              <xs:element name="player" type="xs:string" minOccurs="2" maxOccurs="2"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

b)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="matches">
    <xs:annotation>
      <xs:documentation>This element describes tennis matches</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence minOccurs="3" maxOccurs="10">
        <xs:element name="referee" type="xs:string"/>
        <xs:element name="player" type="xs:string" minOccurs="2" maxOccurs="2"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

c)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="matches">
    <xs:annotation>
      <xs:documentation>This element describes tennis matches</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence>
        <xs:element name="match" minOccurs="3" maxOccurs="5">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="referee" type="xs:string"/>
              <xs:element name="player" type="xs:string" minOccurs="2" maxOccurs="2"/>
            </xs:sequence>
            <xs:attribute name="date" type="xs:date" use="required"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

d)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="matches">
    <xs:annotation>
      <xs:documentation>This element describes tennis matches</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence>
        <xs:element name="match" minOccurs="3" maxOccurs="5">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="referee">
                <xs:complexType>
                  <xs:attribute name="name" use="required"/>
                </xs:complexType>
              </xs:element>
              <xs:element name="player" minOccurs="2" maxOccurs="2">
                <xs:complexType>
                  <xs:attribute name="name" use="required"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="date" type="xs:date" use="required"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

e)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="matches">
    <xs:annotation>
      <xs:documentation>This element describes tennis matches</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence>
        <xs:element name="match" minOccurs="3" maxOccurs="5">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="referee" type="nameType"/>
              <xs:element name="player" type="nameType" minOccurs="2" maxOccurs="2"/>
            </xs:sequence>
            <xs:attribute name="date" type="xs:date" use="required"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="nameType">
    <xs:annotation>
      <xs:documentation>Defines the complex type name. This type is very simple and contains only an attribute.</xs:documentation>
    </xs:annotation>
    <xs:attribute name="name" use="required"/>
    <!-- This is a global type -->
  </xs:complexType>
</xs:schema>

3.

Create an XML file valid according to the following XSD (the root should be destinations):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="departure" type="xs:date"/>
  <xs:element name="destinations">
    <xs:complexType>
      <xs:sequence minOccurs="2" maxOccurs="20">
        <xs:element name="destination" type="place"/>
        <xs:element ref="departure" minOccurs="2" maxOccurs="5"/>
        <xs:element name="price" type="price"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="place">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="continent" type="continent" use="required"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:simpleType name="continent">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Africa"/>
      <xs:enumeration value="America"/>
      <xs:enumeration value="Asia"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:complexType name="price">
    <xs:attribute name="single" use="required"/>
    <xs:attribute name="double" use="required"/>
  </xs:complexType>
</xs:schema>

4.

Assume that a valid XML document, according to the following XSD, can be used to request photo prints in a photo store:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="sizeType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="20x30"/>
      <xs:enumeration value="10x15"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="quantity">
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="1"/>
      <xs:maxInclusive value="9"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:complexType name="photo">
    <xs:simpleContent>
      <xs:extension base="quantity">
        <xs:attribute name="size" type="sizeType" use="required"/>
        <xs:attribute name="src" type="xs:string" use="required"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:element name="photos">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="photo" maxOccurs="unbounded" type="photo"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Describe the following request:

Photo Size Quantity
http://site.pt/foto1.jpg 20x30 5
http://site.pt/foto2.jpg 10x15 3

5.

Create a valid XML document according to the following XSD (the root should be trips):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:complexType name="trip">
    <xs:sequence>
      <xs:element name="place" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="description" type="xs:string"/>
            <xs:element name="href" type="xs:string" minOccurs="0"/>
            <xs:element name="date" type="xs:date" minOccurs="0"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="name" type="xs:string" use="required"/>
    <xs:attribute name="type" use="required">
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:enumeration value="professional"/>
          <xs:enumeration value="leisure"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="nr_places" use="required">
      <xs:simpleType>
        <xs:restriction base="xs:byte">
          <xs:maxExclusive value="5"/>
          <xs:minInclusive value="2"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="date" type="xs:date"/>
  </xs:complexType>
  <xs:element name="trips">
    <xs:complexType>
      <xs:choice maxOccurs="unbounded">
        <xs:element name="train" type="trip"/>
        <xs:element name="plane" type="trip"/>
        <xs:element name="car" type="trip"/>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

6.

Create a valid XML document according to the following XSD (the root should be realestate):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="realestate">
    <xs:complexType>
      <xs:choice minOccurs="3" maxOccurs="3">
        <xs:element ref="apartment"/>
        <xs:element ref="house"/>
      </xs:choice>
    </xs:complexType>
  </xs:element>
  <xs:element name="apartment" type="realty"/>
  <xs:element name="house" type="realty"/>
  <xs:complexType name="realty">
    <xs:sequence>
      <xs:element name="salesman" type="xs:string"/>
      <xs:element name="price">
        <xs:simpleType>
          <xs:restriction base="xs:positiveInteger">
            <xs:minInclusive value="100"/>
            <xs:maxInclusive value="500"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="interested" minOccurs="3" maxOccurs="3" type="buyer"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="buyer">
    <xs:annotation>
      <xs:documentation>Use the content of this element for a report about the buyer.</xs:documentation>
    </xs:annotation>
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="name" type="xs:string" use="required"/>
        <xs:attribute name="contact" type="xs:string"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:schema>

Extra: change the XSD so it isn’t possible to add a report about the buyer inside the buyer element.

7.

Create a valid XML document according to the following XSD (the root should be birthdays):

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="birthdays">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="family" type="family"/>
        <xs:element name="friends" type="friends"/>
        <xs:element name="colleagues" type="colleagues"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="friends">
    <xs:sequence maxOccurs="unbounded">
      <xs:element name="friend" type="birthday"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="colleagues">
    <xs:sequence maxOccurs="unbounded">
      <xs:element name="colleague" type="birthday"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="family">
    <xs:sequence>
      <xs:element name="mother" type="birthday"/>
      <xs:element name="father" type="birthday"/>
      <xs:choice>
        <xs:element name="husband" type="birthday"/>
        <xs:element name="wife" type="birthday"/>
      </xs:choice>
      <xs:choice maxOccurs="unbounded">
        <xs:element name="sister" type="birthday"/>
        <xs:element name="brother" type="birthday"/>
        <xs:element name="son" type="birthday"/>
      </xs:choice>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="birthday">
    <xs:sequence maxOccurs="unbounded">
      <xs:element name="date" type="xs:date"/>
      <xs:element ref="phone"/>
    </xs:sequence>
    <xs:attribute name="name" type="xs:string" use="required"/>
  </xs:complexType>
  <xs:element name="phone">
    <xs:complexType>
      <xs:attribute name="number" type="xs:integer"/>
      <xs:attribute name="network" use="required">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:enumeration value="MEO"/>
            <xs:enumeration value="ZON"/>
            <xs:enumeration value="Vodafone"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:attribute>
    </xs:complexType>
  </xs:element>
</xs:schema>