Book HomeXML SchemaSearch this book

10.11. Allowing Any Elements or Attributes from a Particular Namespace

We are going to see how to accommodate any element or attribute from other namespaces using our marketing extension, the http://dyomedea.com/ns/library/mkt namespace, as an example:

<?xml version="1.0"?>
<!-- Namespace: http://dyomedea.com/ns/library --> 
<library xmlns="http://dyomedea.com/ns/library"
  xmlns:mkt="http://dyomedea.com/ns/library/mkt">
  <book id="b0836217462">
    <title>
      Being a Dog Is a Full-Time Job
    </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 any elements from the http://dyomedea.com/ns/library/mkt namespace after the author element, we use a 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 above are namespace and processContents.

namespace specifies the namespaces of the elements or attributes that will be accepted. The value should be a list of namespaces that URIs allow a number of wildcards. The wildcards permitted within the list are ##local (a nonqualified element) and ##targetNamespace (the target namespace). Two wildcards can also be used instead of the list: ##any (any namespace) and ##other (any namespace other than the target namespace).

TIP: It is not possible to specify that the possible namespaces are all the namespaces not defined in a schema, or even all the namespaces except those in a list. This is a serious limitation for multi-namespace vocabularies that would like to restrict some of the imported namespaces while remaining open to undefined namespaces.

processContents specifies the behavior of the validator regarding the elements or attributes from the specified namespaces. The possible values are "skip" (no validation is attempted on these elements or attributes), "strict" (schemas for the namespaces that will be included need to be available, and validators will validate the elements and attributes against these schemas), or "lax" (validators will do their best to find a schema for the included elements and attributes, validate them when they have found one, and silently skip the validation when they haven't). For 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 has been opened to accept any element from a single namespace and can be further opened to accept any element from any namespace other than the target namespace:

<xs:anyAttribute namespace="##other" processContents="skip"/>

This mechanism is flexible enough (with the exception of the lack of support for any undefined namespaces already mentioned) to accommodate a large majority of applications, but we must note that these wildcards are considered particles and can't replace global element definitions. The unfortunate consequence is that document elements cannot be wildcarded because a schema needs to provide a closed list of possible document elements.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.