Having only been really introduced to XML a week ago, I’m proud to announce my first go at authoring an XML Schema. I can’t say it was really fun, but it was very interesting to be sure. I think XML in general is neat, but creating a schema really gave new insights into how XML files can and should be designed.
Please, if you can propose improvements – leave me a comment!
The sample XML file
<?xml version="1.0" encoding="UTF-8"?>
<inventory
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="inventory.xsd">
<widget>
<material>Aluminium</material>
<length units="ft">1</length>
<width units="in">2</width>
<thickness units="in">.25</thickness>
<price units="USD">5.00</price>
<stockQty>25</stockQty>
</widget>
<widget>
<material>Aluminium</material>
<length units="ft">1</length>
<width units="in">2</width>
<thickness units="in">.5</thickness>
<price units="USD">10.00</price>
<stockQty>15</stockQty>
</widget>
<widget>
<material>Aluminium</material>
<length units="ft">1</length>
<width units="in">4</width>
<thickness units="in">.25</thickness>
<price units="USD">12.50</price>
<stockQty>16</stockQty>
</widget>
<widget>
<material>Aluminium</material>
<length units="ft">1</length>
<width units="in">4</width>
<thickness units="in">.5</thickness>
<price units="USD">15.00</price>
<stockQty>8</stockQty>
</widget>
<widget>
<material>Aluminium</material>
<length units="ft">10</length>
<width units="in">2</width>
<thickness units="in">.25</thickness>
<price units="USD">15.00</price>
<stockQty>25</stockQty>
</widget>
<widget>
<material>Aluminium</material>
<length units="ft">10</length>
<width units="in">2</width>
<thickness units="in">.5</thickness>
<price units="USD">30.00</price>
<stockQty>13</stockQty>
</widget>
<widget>
<material>Aluminium</material>
<length units="ft">10</length>
<width units="in">4</width>
<thickness units="in">.25</thickness>
<price units="USD">35.00</price>
<stockQty>11</stockQty>
</widget>
<widget>
<material>Aluminium</material>
<length units="ft">10</length>
<width units="in">4</width>
<thickness units="in">.5</thickness>
<price units="USD">40.00</price>
<stockQty>4</stockQty>
</widget>
<widget>
<material>Copper</material>
<length units="ft">1</length>
<width units="in">2</width>
<thickness units="in">.25</thickness>
<price units="USD">5.50</price>
<stockQty>19</stockQty>
</widget>
<widget>
<material>Copper</material>
<length units="ft">1</length>
<width units="in">2</width>
<thickness units="in">.5</thickness>
<price units="USD">11.00</price>
<stockQty>17</stockQty>
</widget>
<widget>
<material>Copper</material>
<length units="ft">1</length>
<width units="in">4</width>
<thickness units="in">.25</thickness>
<price units="USD">15.00</price>
<stockQty>3</stockQty>
</widget>
<widget>
<material>Copper</material>
<length units="ft">1</length>
<width units="in">4</width>
<thickness units="in">.5</thickness>
<price units="USD">16.50</price>
<stockQty>8</stockQty>
</widget>
<widget>
<material>Copper</material>
<length units="ft">10</length>
<width units="in">2</width>
<thickness units="in">.25</thickness>
<price units="USD">16.00</price>
<stockQty>21</stockQty>
</widget>
<widget>
<material>Copper</material>
<length units="ft">10</length>
<width units="in">2</width>
<thickness units="in">.5</thickness>
<price units="USD">30.00</price>
<stockQty>13</stockQty>
</widget>
<widget>
<material>Copper</material>
<length units="ft">10</length>
<width units="in">4</width>
<thickness units="in">.25</thickness>
<price units="USD">35.00</price>
<stockQty>18</stockQty>
</widget>
<widget>
<material>Copper</material>
<length units="ft">10</length>
<width units="in">4</width>
<thickness units="in">.5</thickness>
<price units="USD">44.00</price>
<stockQty>2</stockQty>
</widget>
</inventory>
The Schema
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:annotation>
<xsd:documentation xml:lang="en">
Widget Schema for Henry's Widgets.
Copyright 2008 Henry's Widgets Inc.
</xsd:documentation>
</xsd:annotation>
<xsd:element name="inventory" type="inventoryType"/>
<xsd:complexType name="inventoryType">
<xsd:annotation>
<xsd:documentation>
The inventory contains a number of widget elements of
which are defined by the complexType 'widgetType'
</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="widget" type="widgetType" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="materialType">
<xsd:annotation>
<xsd:documentation>
This type defines the possible valid materials for widgets.
This could be easily extended, but currently only includes
Aluminium and Copper.
</xsd:documentation>
</xsd:annotation>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Aluminium"/>
<xsd:enumeration value="Copper"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="widgetType">
<xsd:annotation>
<xsd:documentation>
This type defines widgets. Some of the elements have 'unit' attributes that have fixed values. These could be extended.
</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="material" type="materialType"/>
<xsd:element name="length">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:decimal">
<xsd:attribute name="units" type="xsd:string" fixed="ft" use="required"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="width">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:decimal">
<xsd:attribute name="units" type="xsd:string" fixed="in" use="required"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="thickness">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:decimal">
<xsd:attribute name="units" type="xsd:string" fixed="in" use="required"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="price">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:decimal">
<xsd:attribute name="units" type="xsd:string" fixed="USD" use="required"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="stockQty" type="xsd:nonNegativeInteger"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Again, this is just my first try at this. I hope to improve my skills. Next, XSLT!
Zipped source: inventory.zip





