Scheming schemas
|
|
|||
|
|
Ah, the litany of technologies continues unabated: In recent Gearhead columns we have discussed TCP/IP, HTTP, FTP, telnet, Simple Mail Transfer Protocol, Post Office Protocol, XML, Remote Procedure Call, XML-RPC, Simple Object Access Protocol and namespaces, all as they relate to Web services. Such is the rich, creamy velvety goodness that is the essence of Gearhead. Or something.
Let's move on swiftly to the next technology that is related to Web services simply by association (rather reminds us of the old movie line "Round up the usual suspects!"). The technology we have in mind is XML Schema Definition (XSD).
Easier than DTD
As we noted last week, the most common technology used to define the structure of an XML document has been Document Type Definitions (DTD). Among the deficiencies of DTDs is that they are written in their own language, which, while not as hard to learn as Urdu or Sanskrit, is nevertheless something one would rather avoid. The alternative to using a DTD is to use an XSD.
XSDs are much easier to understand than DTDs because they are written in XML. Better still, XSDs are better than DTDs at characterizing how the content of an XML file is structured.
Now to really explain the ins and outs of XSD would require this column to be approximately 428.97 feet long. We're just going to hit the highlights.
XSDs are structured like any other XML document and properly include a namespace declaration following the XML declaration:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd=http://www.w3c.org/2000/10/XMLSchema>
[[ XSD stuff ]]
</xsd:schema>
So far, so simple - it's that bit in the middle that gets complex. The "XSD stuff" is all about defining the elements that we're going to use in our XML document. These definitions are created by declaring the elements and which data types they use.
There are two data types: Primitive (or base) types and derived types. Derived types are defined by combining two or more data types (either primitive or derived types) to create compound data types.
Primitive data
There are nine primitive data types, including Boolean (true | false), decimal, uriReference and string. These types can be further refined by attributes that restrict the data type's value range or enumerate the allowable values of the data type.
For example, we can set upper and lower limits on the number of characters in a string:
<simpleType name="password">
<restriction base="string">
<minLength>5</minLength>
<maxLength>25</maxLength>
</restriction>
</simpleType>
Here's another definition, but this time we're enumerating the allowable values that an element can be assigned:
<simpleType name="accountType">
<restriction base="string">
<enumeration<user</ enumeration>
<enumeration>manager</enumeration>
<enumeration>administrator</ enumeration>
<enumeration>operator</ enumeration>
</restriction>
</simpleType>
An example of a derived data type is "integer," which you would get by restricting the base type "decimal" to zero decimal places. If you want to use a long integer, you now could derive this from "integer" by setting the restriction that the value must be no less than -(263)-1 and no greater than 263. So the following two XSD definitions define the "integer" and "long" data types:
<simpleType name="integer">
<restriction base= "decimal">
<scale>0</scale>
</restriction>
</simpleType>
<simpleType name="long">
<restriction base= "integer">
<minInclusive>-9223372036854775807</minInclusive>
<maxInclusive>9223372036854775808</maxInclusive>
</restriction>
</simpleType>
Pretty cool, huh? You wait, next week it will get interesting. . . .
Derive your comments to gearhead@gibbs.com.
RELATED LINKS
Comments and suggestions to gh@gibbs.com.
Gibbs Forum
The place to discuss Gibbs's columns.
Check out this week's edition of
Backspin for more musings from Gibbs.


