XML technologies / XML Schema / Controlling namespaces / Schemas for XML, XML Base and XLink
(Excerpt from “XML Schema” by Eric van der Vlist)
XML Base, XLink and XML 1.0 each define attribute-only vocabularies. Attributes such as xml:base, xlink:href or xml:lang work with elements of any vocabulary, conveying meaning to those elements and sometimes their descendants.
XML attributes
We will allow XML attributes in our document, using xml:lang to permit descriptions in several languages and explicitly identify each language:
<?xml version="1.0"?>
<library xmlns="http://dyomedea.com/ns/library">
<book id="b3810518883">
<title>
Auf den Hund gekommen
</title>
<description xml:lang="en">
Its title says it all !
</description>
<description xml:lang="de">
Der Titel sagt alles!!
</description>
</book>
</library>Following the recommendation “Namespaces in XML 1.0”, W3C XML Schema considers xml:lang, xml:space, from XML 1.0, and xml:base, from the XML Base specification, to belong to XML 1.0’s namespace identified by URI reference "http://dyomedea.com/ns/library".
The XML 1.0 namespace is exceptional: instance documents need not declare it. Its prefix “xml” is reserved and meaningful even to parsers unaware of namespaces. Another prefix could theoretically be assigned, but this would confuse and should be discouraged.
Declare the XML namespace, import a schema and reference its attributes, as with elements in our people namespace:
<?xml version="1.0"?>
<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:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="xml.xsd"/>
<xs:element name="description">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="xs:string">
<xs:attribute ref="xml:lang"/>
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element ref="lib:book"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element ref="lib:description" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>Here we import a local schema (xml.xsd) for XML. The W3C defines its own schema for attributes xml:lang, xml:space and xml:base.
<?xml version='1.0'?>
<!DOCTYPE xs:schema PUBLIC "-//W3C//DTD XMLSCHEMA 200102//EN" "XMLSchema.dtd">
<xs:schema targetNamespace="http://www.w3.org/XML/1998/namespace" xmlns:xs="http://www.w3.org/2001/XMLSchema" xml:lang="en">
<xs:annotation>
<xs:documentation>See http://www.w3.org/XML/1998/namespace.html and http://www.w3.org/TR/REC-xml for information about this namespace.</xs:documentation>
</xs:annotation>
<xs:annotation>
<xs:documentation>This schema defines attributes and an attribute group
suitable for use by
schemas wishing to allow xml:base, xml:lang or xml:space attributes
on elements they define.
To enable this, such a schema must import this schema
for the XML namespace, e.g. as follows:
<schema . . .>
. . .
<import namespace="http://www.w3.org/XML/1998/namespace"
schemaLocation="http://www.w3.org/2001/03/xml.xsd"/>
Subsequently, qualified reference to any of the attributes
or the group defined below will have the desired effect, e.g.
<type . . .>
. . .
<attributeGroup ref="xml:specialAttrs"/>
will define a type which will schema-validate an instance
element with any of those attributes</xs:documentation>
</xs:annotation>
<xs:annotation>
<xs:documentation>In keeping with the XML Schema WG's standard versioning
policy, this schema document will persist at
http://www.w3.org/2001/03/xml.xsd.
At the date of issue it can also be found at
http://www.w3.org/2001/xml.xsd.
The schema document at that URI may however change in the future,
in order to remain compatible with the latest version of XML Schema
itself. In other words, if the XML Schema namespace changes, the version
of this document at
http://www.w3.org/2001/xml.xsd will change
accordingly; the version at
http://www.w3.org/2001/03/xml.xsd will not change.
</xs:documentation>
</xs:annotation>
<xs:attribute name="lang" type="xs:language">
<xs:annotation>
<xs:documentation>In due course, we should install the relevant ISO 2- and 3-letter codes as the enumerated possible values . . .</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="space" default="preserve">
<xs:simpleType>
<xs:restriction base="xs:NCName">
<xs:enumeration value="default"/>
<xs:enumeration value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="base" type="xs:anyURI">
<xs:annotation>
<xs:documentation>See http://www.w3.org/TR/xmlbase/ for information about this attribute.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attributeGroup name="specialAttrs">
<xs:attribute ref="xml:base"/>
<xs:attribute ref="xml:lang"/>
<xs:attribute ref="xml:space"/>
</xs:attributeGroup>
</xs:schema> Choosing an original remote schema, on the W3C website, or a local copy is controversial. Remote access guarantees the latest version but may desynchronize schemas and applications. Host unavailability can prevent processing and remote access may expose applications to malicious schema substitution.
XLink attributes
To illustrate XLink attributes, replace the author definition beneath element authors with a reference to an author described as person in another XML document:
<?xml version="1.0"?>
<library xmlns="http://dyomedea.com/ns/library" xmlns:xlink="http://www.w3.org/1999/xlink">
<book id="b3810518883">
<title>
Auf den Hund gekommen
</title>
<authors>
<person xlink:href="authors.xml#CMS"/>
</authors>
</book>
</library> Again, declare the XLink namespace and import a schema before using its attributes:
<?xml version="1.0"?>
<xs:schema targetNamespace="http://dyomedea.com/ns/library" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:lib="http://dyomedea.com/ns/library">
<xs:import namespace="http://www.w3.org/1999/xlink" schemaLocation="xlink.xsd"/>
<xs:element name="person">
<xs:complexType>
<xs:attribute ref="xlink:href" use="required"/>
<xs:attribute ref="xlink:type" fixed="simple"/>
<xs:attribute ref="xlink:show" fixed="embed"/>
<xs:attribute ref="xlink:actuate" fixed="onLoad"/>
</xs:complexType>
</xs:element>
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element ref="lib:book"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element ref="lib:authors"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="authors">
<xs:complexType>
<xs:sequence>
<xs:element ref="lib:person"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>Following the XLink specification, we fix additional attributes (type, show and actuate). This is valid, but values are available only to applications using a post-schema-validation infoset (PSVI) as defined by W3C XML Schema.
XLink supplies no schema, but the required parts are easy to create:
<?xml version="1.0"?>
<xs:schema targetNamespace="http://www.w3.org/1999/xlink" elementFormDefault="qualified" attributeFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xlink="http://www.w3.org/1999/xlink">
<xs:attribute name="type">
<xs:simpleType>
<xs:restriction base="xs:NMTOKEN">
<xs:enumeration value="simple"/>
<xs:enumeration value="extended"/>
<xs:enumeration value="locator"/>
<xs:enumeration value="arc"/>
<xs:enumeration value="resource"/>
<xs:enumeration value="title"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="href" type="xs:anyURI"/>
<xs:attribute name="role" type="xs:anyURI"/>
<xs:attribute name="arcrole" type="xs:anyURI"/>
<xs:attribute name="title" type="xs:string"/>
<xs:attribute name="show">
<xs:simpleType>
<xs:restriction base="xs:NMTOKEN">
<xs:enumeration value="new"/>
<xs:enumeration value="replace"/>
<xs:enumeration value="embed"/>
<xs:enumeration value="other"/>
<xs:enumeration value="none"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="label" type="xs:NMTOKEN"/>
<xs:attribute name="actuate">
<xs:simpleType>
<xs:restriction base="xs:NMTOKEN">
<xs:enumeration value="onLoad"/>
<xs:enumeration value="onRequest"/>
<xs:enumeration value="other"/>
<xs:enumeration value="none"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="from" type="xs:NMTOKEN"/>
<xs:attribute name="to" type="xs:NMTOKEN"/>
</xs:schema>
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XML Schema” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de