WebReference.com - Part 3 of Chapter 1: Professional XML Schemas, from Wrox Press Ltd (5/5) | WebReference

WebReference.com - Part 3 of Chapter 1: Professional XML Schemas, from Wrox Press Ltd (5/5)

To page 1To page 2To page 3To page 4current page
[previous]

Professional XML Schemas

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=" https://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 https://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="https://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.]


To page 1To page 2To page 3To page 4current page
[previous]

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


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