A product catalog

exercise No. 9

Product catalog schema Create comment in forum

Q:

Consider the following product catalog example:

<catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="catalog.xsd">
  <title>Outdoor products</title>
  <introduction>
    <para>We offer a great variety of basic stuff for mountaineering
          such as ropes, harnesses and tents.</para>
    <para>Our shop is proud for its large number of available
      sleeping bags.</para>
  </introduction>
  <product id="x-223">
    <title>Multi freezing bag  Nightmare camper</title>
    <description>
      <para>You will feel comfortable till  minus 20 degrees - At
            least if you are a penguin or a polar bear.</para>
    </description>
  </product>
  <product id="r-334">
    <title>Rope 40m</title>
    <description>
      <para>Excellent for indoor climbing.</para>
    </description>
  </product>
</catalog>

As you may have inferred the following rules shall apply for arbitrary catalog documents:

  • Each <catalog> shall have exactly one <title> and <introduction> element.

  • <introduction> and <description> shall have at least one <para> child.

  • Each <catalog> shall have at least one <product>.

  • Each <product> shall have exactly one <title> and at least one <para> child element.

  • The required id attribute shall not contain whitespace and be unique with respect to all <product> elements.

  • The attribute price shall represent money amounts and be optional.

Provide a suitable catalog.xsd schema.

A:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified"
   vc:minVersion="1.0" vc:maxVersion="1.1">

   <xs:simpleType name="money">
      <xs:restriction base="xs:decimal">
         <xs:fractionDigits value="2"/>
      </xs:restriction>
   </xs:simpleType>

   <xs:element name="title" type="xs:string"/>
   <xs:element name="para" type="xs:string"/>

   <xs:element name="description" type="paraSequence"/>
   <xs:element name="introduction" type="paraSequence"/>

   <xs:complexType name="paraSequence">
      <xs:sequence>
         <xs:element ref="para" minOccurs="1" maxOccurs="unbounded"/>
      </xs:sequence>
   </xs:complexType>

   <xs:element name="product">
      <xs:complexType>
         <xs:sequence>
            <xs:element ref="title"/>
            <xs:element ref="description"/>
         </xs:sequence>
         <xs:attribute name="id" type="xs:NMTOKEN" use="required"/>
         <xs:attribute name="price" type="money" use="optional"/>
      </xs:complexType>
   </xs:element>

   <xs:element name="catalog">
      <xs:complexType>
         <xs:sequence>
            <xs:element ref="title"/>
            <xs:element ref="introduction"/>
            <xs:element ref="product" minOccurs="1" maxOccurs="unbounded"/>
         </xs:sequence>
      </xs:complexType>

      <xs:key name="uniqueProductId">
         <xs:selector xpath="product"></xs:selector>
         <xs:field xpath="@id"/>
      </xs:key>
   </xs:element>

</xs:schema>