WebReference.com - Part 3 of Chapter 1: Professional XML Schemas, from Wrox Press Ltd (4/5)
[previous] [next] |
Professional XML Schemas
Validating with XSV
At the time of writing, XML Schema has only recently become a full W3C recommendation, and there are limited tools available for validating instance documents using the final recommendation. A proliferation of compliant tools is expected to follow, but many are in still in beta version. Check out Appendix D for full discussion of XML Schema tools and XML Schema-compliant parsers. For now, however, we are just going to focus on one, XSV.
XSV (XML Schema Validator) is an ongoing open source project, developed at the University of Edinburgh in the UK by Henry Thompson and Richard Tobin (Henry Thompson is also co-author of the XML Schema Recommendation, Part 1). Written in Python, it is available for download either as source, or as a Win32 executable. Alternatively, you can use it as an online utility. XSV is available from:
- http://www.ltg.ed.ac.uk/~ht/xsv-status.html (for download)
- http://www.w3.org/2001/03/webdata/xsv (to use online)
The easiest way to use XSV is via the online web form. You can validate schemas on their own by simply uploading the file from your own machine, but if you want to validate instance documents against your schema, then you need to be able to make them available online. If this is difficult for you  if you are behind a firewall for example  then you may prefer to download XSV and install it on your own machine.
Since this is ongoing work, there are frequent updates to the tool, and full details concerning which parts of the XML Schema recommendation are implemented is available from the first URL above. At the time of writing, this tool appears to be the one most fully conformant with the W3C recommendation.
Warning: One of the main limitations of XSV at the time of writing is its lack of support for validating simple types. The only checks that XSV makes on simples types are on length and enumerations.
The download comes in the form of a self-installing executable for Win32. If you're working on a Unix platform, however, you'll need to download and compile the source files. Alternatively, you could check out some of the tools discussed in Appendix D, such as Turbo XML from TIBCO Extensibility Solutions.
Validating a Schema
Let's start by validating a simple schema, name.xsd:
<?xml version = "1.0" encoding = "UTF-8"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema">
<xs:element name = "Name">
<xs:complexType>
<xs:sequence>
<xs:element name = "firstName" type = "xs:string" />
<xs:element name = "middleInitial" type = "xs:string" />
<xs:element name = "lastName" type = "xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
If you try validating this schema online, then you should see something like this:

Using the downloaded version of XSV, you can check that this is a valid schema by
simply running it from the command line with a -i flag:

(Note that you'll need to have the folder in which XSV is installed included in your
PATH variable). The output here isn't immediately obvious, so let's take a quick look at
it (see the screenshot below). You can see that we are looking at a schema file here rather than an
XML instance document since it says instanceAccessed='false', and that the target is
[standalone schema assessment]. Note that no schema errors are listed.
If you are running IE5 or above, you get a more user-friendly version of this and you can redirect the XML output to another file, including a stylesheet for display, with the command:
> xsv -o xsv-out.xml -s xsv.msxsl -i name.xsd
If you have MSXML 3 installed, you should replace xsv.msxsl with the XSLT
1.0 compliant version of the stylesheet, xsv.xsl. You can then view the result in your
browser:

Note that you can use
xsv -?for information on all the possible flags.
So that covers the basic ways of using XSV. Now let's take a look at some of the error messages that occur if our schema isn't error free. Suppose, for example, we make a simple typographical mistake, such as spelling the name attribute wrongly, or forgetting to close one of the elements:
<?xml version = "1.0" encoding = "UTF-8"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema">
<xs:element name = "Name">
<xs:complexType>
<xs:sequence>
<xs:element nsme = "firstName" type = "xs:string" />
<xs:element name = "middleInitial" type = "xs:string" />
<xs:element name = "lastName" type = "xs:string" />
</xs:sequence>
<xs:complexType>
</xs:element>
</xs:schema>
In this case, XSV warns us that we have an undeclared attribute nsme, on our
element element, and that we have a complexType declaration out of place:

The reason the second error message takes this form is that XSV thinks that because we
have forgotten to add a / in our closing tag, we are trying to nest a second
complexType element inside the first, which is not allowed. Note that XSV also gives us
the line number of each error. While the mistakes may be quite obvious in our simple schema, this
information becomes very helpful when working with more complex examples.
[previous] [next] |
Created: October 25, 2001
Revised: October 25, 2001
URL: http://webreference.com/authoring/languages/xml/schemas/chap1/3/4.html


