Schema Basics
This section will construct, step-by-step, a simple schema document representing a typical address book entry, introducing different features of the XML Schema language as needed. Example 17-1 shows a very simple, well-formed XML document.
<?xml version="1.0"?> <fullName>Scott Means</fullName>
Assuming that the fullName
element can only contain a simple string value, the schema for this
document would look like Example
17-2.
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="fullName" type="xs:string"/> </xs:schema>
It is also common to associate the sample instance document
explicitly with the schema document. Since the fullName
element is not in any namespace,
the xsi:noNamespaceSchemaLocation
attribute is used, as shown in Example 17-3.
<?xml version="1.0"?> <fullName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="address-schema.xsd">Scott Means</fullName>
Validating the simple document against its schema requires a
validating XML parser that supports schemas such as the open source
Xerces parser from the Apache XML Project (http://xml.apache.org/xerces2-j/ ). This is written in
Java and includes a command-line program called dom.Writer
that can be used to validate
addressdoc.xml, like this:
% java dom.Writer -V -S addressdoc.xml
Since the document is valid, dom.Writer
will simply ...
Get XML in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.