XSD gets even more interesting
|
|
|||
|
|
First, a correction regarding last week's column on XML Schema Definition. In the part about creating derived data types, we goofed. Eagle-eyed reader Will Wagers wrote to tell us of our error: "Surely, you meant -2 to the 63 to 2 to the 63 -1?"Surely we did. The moral is never edit by cell phone while driving at, ahem, 65 mph.
This week we promised XSD would get interesting when what we meant was "even more interesting" . . . .
So by now you should have a pretty good idea of XSD's primitive (also called "basic") and derived data types. Just to make things even more gripping, that's not all! Oh no, XSD also has complex types that are derived from multiples and/or combinations of primitive and derived types.
This example is borrowed from a book we highly recommend: Architecting Web Services by William Oellermann Jr. Oellermann covers a huge amount of ground in this book in as easily understood a manner as possible considering the inherent complexity of the subject.
While we're at it, we should mention another book that we rather like that attempts to ease you into the dark recesses of XSD and, indeed, many other things XML-related - XML for the World Wide Web by Elizabeth Castro.
Castro's book is not quite as detailed (or as long) as Oellermann's but has more examples that are more detailed, making it easier to understand the principles of XML, XSD and related technologies. We recommend getting both books.
So here's a complex type defined:
<complexType name = "money" >
<simpleContent>
<extension base = "decimal">
<attribute name = "currency" type = "string" />
</extension>
</simpleContent>
</complexType>
The complex type that we're defining is called "money," and it is based on the primitive type "decimal." But we need to add to the type an attribute that defines the currency - hence the "extension" element.
This XSD specification applied to the following XML would pass validation:
<money currency="USD">123.45</money>
So far all we've done is just lightly scratch the surface of creating a schema using XSD, but you can see how definitions under XSD create templates for validating XML content.
We recommend that you check out the World Wide Web Consortium's XSD primer, but you might want to read either or both of our recommended books first - the W3C stuff is beyond exhaustive.
A helping hand
As you can imagine, creating an XSD by hand would be very time-consuming and error-prone, but, fear not, there are tools to help you create and validate XML content against XSD specifications.
One of the most highly rated XML tool suites is XML Spy from Altova (www.xmlspy.com). XML Spy is for creating and validating XML schemas, Document Type Definitions (DTD) and documents. It presents you with an integrated development environment with multiple panes and automatically generates code in the background. You can validate XML, DTD and XSD documents using the built-in incremental parser.
Most of the process of constructing something like an XML or XSD document is a matter of drag-and-drop and, where required, XML Spy can generate an Access database to support an XML document.
This is an awesome tool, but don't underestimate the steep learning curve that comes with such sophistication. Then again, XML and XSD aren't for techno-wimps anyway.
Another major player in this market is SoftQuad (recently swallowed up by Corel) with the XMetal product (http://shorterlink.com/?XZCU9Y).
XMetal was one of the earliest and most polished XML editing tools and the latest release, Version 3.0, includes XSD support. We have yet to get our hands on this product but it looks very promising (see http://shorterlink.com/?ASY1N7 for a full feature list).
Be all that as it may, next week we'll carry on with our tour through Web services technology.
So what are you doing with Web services? Spill your content to gearhead@gibbs.com.
RELATED LINKS

