|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 6
The answer to these deficiencies: XML Schema. XML Schemas are in XML format; they allow data typing, user-defined types, and range value constraints. XML Schema's popularity and software support is growing because it is now a final W3C Recommendation. Here you'll find an example of constraining the same document type using XML Schema:
Listing 3. socks.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="socks">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="sock" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="image" type="imageType"/>
<xsd:element name="color" type="colorType"/>
<xsd:element name="price" type="money"/>
<xsd:element name="smell" type="smellType"/>
</xsd:sequence>
<xsd:attribute name="number" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="imageType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="(.)+\.(gif|jpg|jpeg|bmp)"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="colorType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="black"/>
<xsd:enumeration value="white"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="money">
<xsd:restriction base="xsd:decimal">
<xsd:fractionDigits value="2"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="smellType">
<xsd:annotation>
<xsd:documentation>0=clean and 10=smells terrible</xsd:documentation>
</xsd:annotation>
<xsd:restriction base="xsd:nonNegativeInteger">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="10"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
That looks a little more complicated, but believe me, the complexity is worth it. In addition to the constraints we specified in the DTD, XML Schema lets us add the following:
<image> tag must end with an image type extension (like .gif).
<price> must be a number with two fractional digits (like 5.34).
<smell> must be an int between 0 and 10. There is also a documentation comment stating 0=clean and 10=smells terrible.
For more on XML Schemas, see the schema tutorial in Resources.
To associate socks.xml with our XML Schema, we'll change only some attributes in the root (socks) tag:
Listing 4. socks.xml for use with the XML Schema
<?xml version="1.0"?>
<socks xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="socks.xsd">
<sock number="1">
<name>black socks</name>
...
How do we check to see if an XML document is valid against a DTD or XML Schema? We use an XML parser like Apache Xerces to
verify that an XML document conforms to the socks.dtd or socks.xsd constraints. I've provided a Windows batch file in my sample code called validate.bat to do that:
validate.bat <path_to_socks>\socks.xml
This invokes the Xerces parser in validation mode, which asks it to please check socks.xml against its stated document type. Xerces now supports both DTD and XML Schema validation.