| Previous | Next
Working with NamespacesSo far, namespaces have only been dealt with as they relate to the schema processor and schema language itself. But the schema specification was written with the intention that schemas could support and describe XML namespaces. In an ideal world, any XML parser with access to the Internet would be able to validate any XML document, given only that document's namespace. In fact, the Resource Directory Description Language (RDDL) standard is an attempt to build the framework that will enable this functionality and is described in detail in . Target NamespacesAssociating a schema with a particular XML namespace is extremely simple: add a <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://namespaces.anonymous.com/xmlnut/address"> 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 However, making that simple change impacts numerous other parts of the example application. Trying to validate the 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.anonymous.com/xmlnut/address address-schema.xsd" language="en">Scott Means</fullName> Notice that the 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.anonymous.com/xmlnut/address address-schema.xsd" xmlns="http://namespaces.anonymous.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 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.anonymous.com/xmlnut/address" xmlns:addr="http://namespaces.anonymous.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. TIP: The obvious lesson from this is that namespaces should be incorporated into your schema design as early as possible. If not, there will likely be a large amount of cleanup involved as various assumptions that used to be true are no longer valid. Controlling QualificationOne of the major headaches with DTDs is that they have no explicit support for namespace prefixes since they predate the Namespaces in XML recommendation. Although Namespaces in XML went to great pains to explain that prefixes were only placeholders and only the namespace URIs really matter, it was painful and awkward to design a DTD that could support arbitrary prefixes. Schemas correct this by validating against namespace URIs and local names rather than prefixed names. The <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://namespaces.anonymous.com/xmlnut/address" xmlns:addr="http://namespaces.anonymous.com/xmlnut/address" attributeFormDefault="qualified"> Now, if Attribute "language" must be declared for element type "fullName". Since the default attribute form has been set to The easiest way to fix the instance document is to declare an explicit namespace prefix and use it to qualify the element and attribute, as shown in Example 16-5. Example 16-5. addressdoc.xml with explicit namespace prefix<?xml version="1.0"?> <addr:fullName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://namespaces.anonymous.com/xmlnut/address address-schema.xsd" xmlns:addr="http://namespaces.anonymous.com/xmlnut/address" addr:language="en">Scott Means</addr:fullName> The |