Java Reference
In-Depth Information
<xsd:attributeGroup name="coords">
<xsd:attribute name="x" type="xsd:integer" use="required"/>
<xsd:attribute name="y" type="xsd:integer" use="required"/>
</xsd:attributeGroup>
</xsd:schema>
This is somewhat longer than the DTD for Sketcher, but it does provide several advantages. All the data
in the document now has types specified so the document is more precisely defined. This schema is XML,
so the documents and the schema are defined in fundamentally the same way and are equally communic-
able. There is no problem combining one schema with another because namespaces are supported, and every
schema can be easily extended. You can save this as a file Sketcher.xsd .
A Document That Uses a Schema
A document that has been defined in accordance with a particular schema is called an instance document
for that schema. An instance document has to identify the schema to which it conforms, and this is done
using attribute values within the root element of the document. Here's an XML document for a sketch that
identifies the location of the schema:
<sketch
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:/D:/Beg%20Java%20Stuff/Sketcher.xsd">
<!-- Elements defined for the sketch... -->
</sketch>
The value for the xmlns attribute identifies the namespace name http://www.w3.org/2001/XMLSchema-
instance and specifies xsi as the prefix used to represent this namespace name. In an instance document,
the value for the noNamespaceSchemaLocation attribute in the xsi namespace is a hint about the location
where the schema for the document can be found. Here the noNamespaceSchemaLocation value is a URI
for a file on the local machine, and the spaces are escaped because this is required within a URI. The value
you specify for the xsi:noNamespaceSchemaLocation attribute is always regarded as a hint, so in principle
an application or parser processing this document is not obliged to take account of this. In practice, though,
this usually is taken into account when the document is processed, unless there is good reason to ignore it.
You define a value for the noNamespaceSchemaLocation attribute because a sketch document has no
namespace; if it had a namespace, you would define a value for the schemaLocation attribute that includes
two URIs separated by whitespace within the value specification — the URI for the namespace and a URI
that is a hint for the location of the namespace. Obviously, because one or more spaces separate the two
URIs, the URIs cannot contain unescaped spaces.
PROGRAMMING WITH XML DOCUMENTS
Right at the beginning of this chapter I introduced the notion of an XML processor as a module that is used
by an application to read XML documents. An XML processor parses a document and makes the elements,
together with their attributes and content, available to the application, so it is also referred to as an XML
parser . In case you haven't met the term before, a parser is just a program module that breaks down text
Search WWH ::




Custom Search