XML technologies / XML Schema / Controlling namespaces / Allowing arbitrary elements or attributes from a particular namespace
(Excerpt from “XML Schema” by Eric van der Vlist)
Our marketing extension illustrates how to accommodate arbitrary elements or attributes from other namespaces, using namespace "http://dyomedea.com/ns/library/mkt":
<?xml version="1.0"?>
<!-- Namensraum: http://dyomedea.com/ns/library -->
<library xmlns="http://dyomedea.com/ns/library" xmlns:mkt="http://dyomedea.com/ns/library/mkt">
<book id="b3810518883">
<title>
Auf den Hund gekommen
</title>
<authors>
<person id="CMS">
<name>
Charles M. Schulz
</name>
</person>
</authors>
<mkt:cover>
Paperback
</mkt:cover>
<mkt:pages>
128
</mkt:pages>
</book>
</library>To allow arbitrary elements from namespace "http://dyomedea.com/ns/library/mkt" after element author, use an xs:any element:
<xs:any namespace="http://dyomedea.com/ns/library/mkt" processContents="skip"minOccurs="0" maxOccurs="unbounded"/>xs:anyAttribute should be used to allow attributes:
<xs:anyAttribute namespace="http://dyomedea.com/ns/library/mkt" processContents="skip"/>The two new attributes shown are namespace and processContents.
namespace specifies the namespaces of accepted elements or attributes. The value is a list of URI-identified namespaces with some wildcards allowed. Wildcards within the list are ##local, an unqualified element, and ##targetNamespace , the target namespace. Two wildcards can replace the list: ##any , any namespace, and ##other, any namespace except the target.
One cannot specify all namespaces undefined in the schema, or all except a listed set. This seriously limits multinamespace vocabularies wanting to exclude imported namespaces while remaining open to undefined ones.
processContents sets validator behavior for elements and attributes from these namespaces. Values are “skip”, no validation attempted; “strict”, schemas must exist and are used to validate; or “lax”, validators try to find a schema, validate if found and silently skip validation otherwise. Example:
<?xml version="1.0"?>
<xs:schema targetNamespace="http://dyomedea.com/ns/library" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://dyomedea.com/ns/library">
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element ref="book"/>
</xs:sequence>
<xs:anyAttribute namespace="http://dyomedea.com/ns/library/mkt" processContents="skip"/>
</xs:complexType>
</xs:element>
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="authors">
<xs:complexType>
<xs:sequence>
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:any namespace="http://dyomedea.com/ns/library/mkt" processContents="skip"minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="id" use="required">
<xs:simpleType>
<xs:restriction base="xs:hexBinary"/>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>This schema accepts arbitrary elements from one namespace. It can be opened further to accept them from any namespace except the target:
<xs:anyAttribute namespace="##other" processContents="skip"/>This is flexible enough for most applications, except for the missing support for undefined namespaces noted above. But wildcards are particles and cannot replace global element definitions. Document elements therefore cannot be covered by wildcards: a schema must provide a closed list of possible document elements.
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
back to top