WebReference.com - Part 3 of Chapter 1: Professional XML Schemas, from Wrox Press Ltd (2/5)
[previous] [next] |
Professional XML Schemas
Validating an Instance Document
Having understood some of the basics for writing XML Schemas, we should look at how we validate document instances. You may have noticed that none of the sample XML documents in this chapter have indicated a link to the XML Schema they are supposed to correspond to. They have not included an equivalent of the Document Type Declaration (whether it refers to inline definitions or an external DTD). This is because there is no direct link of any kind between an instance document and its XML Schema.
A document author can indicate where a copy of the schema they used to write the document
can be found using the xsi:schemaLocation
attribute, whose value is a URL, but there is no
requirement for the processor to use the indicated schema. For example we could use the following to
indicate where the Customer.xsd
file can be found:
<?xml version = "1.0" ?>
<Customer xmlns:xsi = "https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "https://www.wrox.com/ProXMLSchemas/Customer.xsd">
...
</Customer>
Note that we have had to declare the XML Schema for Instance Documents namespace and its
prefix xsi:
in order to use the schemaLocation
attribute (as the
schemaLocation
attribute is defined in that namespace).
Parsers can ignore or override the suggestion in the
schemaLocation
attribute; they may decide to use a different schema or use a cached copy of the suggested schema.
Sometimes it is helpful to be able to validate a document against a different schema than that which it was authored against. Therefore we can leave it up to the program that hands the XML document to the parser to say which schema to use to validate it.
Note also that we have not so far been indicating the intended namespace to which our
schema belongs. This means that the markup we have been creating does not belong to a namespace. In
this case we need to use the xsi:noNamespaceSchemaLocation
attribute on the root element,
like this:
<?xml version = "1.0" ?>
<Customer xmlns:xsi = "https://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation = "Customer.xsd">
...
</Customer>
This indicates to the parser where it can find a copy of the schema that doesn't belong to a namespace.
[previous] [next] |
Created: October 25, 2001
Revised: October 25, 2001
URL: https://webreference.com/authoring/languages/xml/schemas/chap1/3/2.html