TIP:
It is important to remember that many XML 1.0 documents are not
associated with namespaces at all. To validate these documents, it is
necessary to use a schema that doesn't have a
targetNamespace attribute. When developing schemas
that are not associated with a target namespace, you should
explicitly qualify schema elements (like
xs:element) to keep them from being confused with
global declarations for your application.
However, making that
simple change impacts numerous other parts of the example
application. Trying to validate the addressdoc.xml
document as it stands (with the
xsi:noNamespaceSchemaLocation attribute) causes
the Xerces schema processor to report this validity error:
General Schema Error: Schema in address-schema.xsd has a different target
namespace from the one specified in the instance document :.
To rectify this, it is necessary to change the instance document to
reference the new, namespace-enabled schema properly. This is done
using the xsi:schemaLocation attribute, like so:
<fullName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://namespaces.oreilly.com/xmlnut/address
address-schema.xsd"
language="en">Scott Means</fullName>
Unfortunately, there are still problems. If this document is
validated, the validator will report errors like these two:
Element type "fullName" must be declared.
Attribute "language" must be declared for element type "fullName".
This is because, even though a schema location has been declared, the
element still doesn't actually belong to a
namespace. Either a default namespace must be declared or a namespace
prefix that matches the target namespace of the schema must be used.
The following document uses a default namespace:
<fullName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://namespaces.oreilly.com/xmlnut/address
address-schema.xsd"
xmlns="http://namespaces.oreilly.com/xmlnut/address"
language="en">Scott Means</fullName>
But before this document can be successfully validated, it is
necessary to fix one other problem that was introduced when a target
namespace was added to the schema. Within the element declaration for
the fullName element, there is a reference to the
nationality attribute group. By associating the
schema with a target namespace, every global declaration has been
implicitly associated with that namespace. This means that the
ref attribute of the attribute group element in
the element declaration must be updated to point to an attribute
group that belongs to the new target namespace.
The clearest way to do this is to declare a new namespace prefix in
the schema that maps to the target namespace and use it to prefix any
references to global declarations:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://namespaces.oreilly.com/xmlnut/address"
xmlns:addr="http://namespaces.oreilly.com/xmlnut/address">
. . .
<xs:attributeGroup ref="addr:nationality"/>
. . .
Now, having made these three simple changes, the document will once
again validate cleanly against the schema.