XML technologies / XML Schema / Controlling namespaces / Namespace declarations

(Excerpt from “XML Schema” by Eric van der Vlist)

So far our documents had no namespace declarations and belonged to no namespace. Their matching schemas also had no namespace declarations except the one identifying the namespace of W3C XML Schema itself.

To match namespaced elements and attributes, associate their namespace with the schema through attribute targetNamespace of element xs:schema.

We modify the library to use one namespace:

<library xmlns="http://dyomedea.com/ns/library">
    <book id="b3810518883" available="yes">
        <isbn>
            3810518883
        </isbn>
        <title>
            Auf den Hund gekommen
        </title>
        .../...
    </book>
</library>

Declare this namespace in the schema and set it as target:

<xs:schema targetNamespace="http://dyomedea.com/ns/library" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns:lib="http://dyomedea.com/ns/library" xmlns:xs="http://www.w3.org/2001/XMLSchema">
 .../...
</xs:schema>

Namespace definitions matter especially because W3C XML Schema uses them for two purposes.

As in any namespace-compliant XML, declarations first associate a namespace URI reference with a prefix abbreviating that identifier.

Our example has two declarations: xmlns:xs="http://www.w3.org/ 2001/XMLSchema" and xmlns:lib="http://dyomedea.com/ns/library".

The first associates the namespace of W3C XML Schema with prefix xs. Any prefix would work, or it could be the default namespace; choosing xs is merely customary.

The second declares the namespace used in our instance: xmlns:lib="http://dyomedea.com/ns/library". We chose prefix lib, although no schema element or attribute uses this namespace. Any prefix or the default namespace could also be chosen.

This declaration serves the second use of namespace prefixes. W3C XML Schema uses namespace prefixes to resolve all component references, data types, elements, attributes and groups, and in the XPath expressions used in declarations of xs:unique, xs:key and xs:keyref.

We have not specified which namespace the schema describes. Attribute targetNamespace gives the URI reference identifying the target namespace.

The processor now knows the target namespace and, from the declarations, the prefixes for it and for W3C XML Schema. This suffices to write our schema.

Namespace-prefix usage shared by W3C XML Schema and XSLT is controversial because it makes W3C XML Schema, as an application, dependent on chosen prefixes. This breaks XML’s layered specifications: markup and content depend on each other and cannot change independently.

Like communication protocols, XML specifications are nested envelopes: XML 1.0 is outermost, containing namespaces. Applications should be independent of them, but W3C XML Schema binds schemas to their envelope by using prefixes in its attributes. This is dangerous and should be discouraged in other vocabularies defining their own prefixes.

One consequence forced “Canonical XML” to drop namespace-prefix rewriting from its requirements. Our four namespace-equivalent libraries therefore have four different canonical values and digital signatures.

   

 

 

 

back to top