spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / authoring / languages / xml / schemas / chap1 / 3 To page 1To page 2To page 3To page 4current page
[previous]

Professional XML Schemas

Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans

Validating an Instance Document

Now let's try validating an instance document against our simple schema:

<?xml version = "1.0" encoding = "UTF-8"?>
<Name xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="name.xsd">
   <firstName>John</firstName>
   <middleInitial>J</middleInitial>
   <lastName>Johnson</lastName>
</Name>

Here, we have used the xsi:noNamespaceSchemaLocation attribute to indicate the location of the schema document to which the XML instance document conforms. In this case, it is in the same directory. Note that if you're validating this with the online version of XSV, both the XML file and the schema file need to be accessible over the web. Here's what the results look like for this file, name.xml:

XSV Instance Validation Example

The things to look out for here are the statement that there are no schema-validity problems in the target, and that the "Validation was strict": this means that the instance document has correctly validated against the schema. If you see the validation described as "lax", then you'll know that your document has not been validated, though it may be well formed. Note also the line at the bottom of the output, "Attempt to import a schema document from http://apache.wrox.co.uk/name.xsd for no namespace succeeded". This means that XSV has successfully found and loaded the correct schema document.

Let's take a look at an instance document with some problems, so you can see how XSV reports errors in document validation. Here, we've simply slipped in an extra title element that is not declared in our schema:

<Name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="name.xsd">
   <title>Dr</title>
   <firstName>John</firstName>
   <middleInitial></middleInitial>
   <lastName>Johnson</lastName>
</Name>

Let's try this one with our local version of XSV. In this case, we don't use the -i flag, as we are validating an instance document, not a schema, so we use the command:

> xsv -o xsv-out.xml -s xsv.msxsl name_2.xml

And this is what the output looks like:

XSV Instance Validation Errors Example

In the first part of the output, we see the line, "2 schema-validity problems were found in the target". If you look at the section below, where the problems are listed in detail, you can clearly see that there is a title element that is not allowed according to the schema, and XSV was expecting the firstName element to appear in its place. The first number after the file name (in this case 4) indicates the line number on which the error occurred. Again, this information can be very useful when debugging schemas.

The output prefixes all of the element names with {None} to indicate that these elements are not part of a namespace. We'll be seeing how to create schemas with a target namespace in Chapter 6.

In the final part of this chapter, we'll be tying together the ideas that we have met so far in a slightly more complex example. [Continued in part 4 - Ed.]


home / authoring / languages / xml / schemas / chap1 / 3 To page 1To page 2To page 3To page 4current page
[previous]


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger

Created: October 25, 2001
Revised: October 25, 2001


URL: http://webreference.com/authoring/languages/xml/schemas/chap1/3/5.html