5.5 Any Element, Any AttributeAny Element, Any Attribute
In previous sections we have seen several mechanisms for
extending the content models of complex types. For example,
a mixed content model can contain arbitrary character data
in addition to elements, and for example, a content model
can contain elements whose types are imported
from external namespaces. However, these mechanisms provide
very broad and very narrow controls respectively. The
purpose of this section is to describe a flexible mechanism
that enables content models to be extended by any elements
and attributes belonging to specified namespaces.
To illustrate, consider a version of the quarterly report,
4Q99html.xml, in
which we have embedded an XHTML representation of the XML
parts data. The XHTML content appears as the content of the
element htmlExample, and the default
namespace is changed on the outermost XHTML element
(table) so that all the XHTML elements belong
to the XHTML namespace,
http://www.w3.org/1999/xhtml:
NOTE:
Quarterly Report with
XHTML, 4Q99html.xml
<purchaseReport
xmlns="http://www.example.com/Report"
period="P3M" periodEnding="1999-12-31">
<regions>
<!-- part sales listed by zip code, data from 4Q99.xml -->
</regions>
<parts>
<!-- part descriptions from 4Q99.xml -->
</parts>
<htmlExample>
<table xmlns="http://www.w3.org/1999/xhtml"
border="0" width="100%">
<tr>
<th align="left">Zip Code</th>
<th align="left">Part Number</th>
<th align="left">Quantity</th>
</tr>
<tr><td>95819</td><td> </td><td> </td></tr>
<tr><td> </td><td>872-AA</td><td>1</td></tr>
<tr><td> </td><td>926-AA</td><td>1</td></tr>
<tr><td> </td><td>833-AA</td><td>1</td></tr>
<tr><td> </td><td>455-BX</td><td>1</td></tr>
<tr><td>63143</td><td> </td><td> </td></tr>
<tr><td> </td><td>455-BX</td><td>4</td></tr>
</table>
</htmlExample>
</purchaseReport>
ref32To permit the appearance of XHTML in
the instance document we modify the report schema by
declaring a new element htmlExample whose
content is defined by the
any element. In general, an any element specifies that any
well-formed XML is permissible in a type's content model.
In the example, we require the XML to belong to the
namespace http://www.w3.org/1999/xhtml, in
other words, it should be XHTML. The example also requires
there to be at least one element present from this
namespace, as indicated by the values of minOccurs and maxOccurs:
NOTE:
Modification to purchaseReport Declaration to Allow
XHTML in Instance
<element name="purchaseReport">
<complexType>
<sequence>
<element name="regions" type="r:RegionsType"/>
<element name="parts" type="r:PartsType"/>
<element name="htmlExample">
<complexType>
<sequence>
<any namespace="http://www.w3.org/1999/xhtml"
minOccurs="1" maxOccurs="unbounded"
processContents="skip"/>
</sequence>
</complexType>
</element>
</sequence>
<attribute name="period" type="duration"/>
<attribute name="periodEnding" type="date"/>
</complexType>
</element>
ref33The modification permits some
well-formed XML belonging to the namespace
http://www.w3.org/1999/xhtml to appear inside the
htmlExample element. Therefore 4Q99html.xml is permissible
because there is one element which (with its children) is
well-formed, the element appears inside the appropriate
element (htmlExample), and the instance
document asserts that the element and its content belongs
to the required namespace. However, the XHTML may not
actually be valid because nothing in 4Q99html.xml by itself can
provide that guarantee. If such a guarantee is required,
the value of the processContents
attribute should be set to strict
(the default value). In this case, an XML processor is
obliged to obtain the schema associated with the required
namespace, and validate the XHTML appearing within the
htmlExample element.
textTypeIn another example, we define a text
type which is
similar to the text type defined in XML Schema's introductory
type library (see also [Type Libraries]), and
is suitable for internationalized human-readable text. The
text type allows an unrestricted mixture of character content
and element content from any namespace, for example Ruby annotations, along with
an optional xml:lang attribute. The lax
value of the
processContents
attribute instructs an XML processor to validate the element
content on a can-do basis: It will validate elements and attributes for
which it can obtain schema information, but it will not signal errors
for those it cannot obtain any schema information.
NOTE:
Text Type
<xsd:complexType name="text">
<xsd:complexContent mixed="true">
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:any processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute ref="xml:lang"/>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
ref34Namespaces may be used to permit and
forbid element content in various ways depending upon the
value of the
namespace attribute, as shown in Table 4:
nsTable2100%namespace attribute in any
Table 4. Namespace Attribute in Any
Value of Namespace Attribute
Allowable Element Content
| 11##any |
11Any well-formed XML from any namespace (default) |
| 11##local |
11Any well-formed XML that is not qualified, i.e. not
declared to be in a namespace |
| 11##other |
11Any well-formed XML that is from a namespace other
thannot from the target
namespace of the type being defined (unqualified
elements are not allowed) |
| 11"http://www.w3.org/1999/xhtml ##targetNamespace" |
11Any well-formed XML belonging to any namespace in the
(whitespace separated) list; ##targetNamespace is
shorthand for the target namespace of the type being
defined |
ref35In addition to the any element which enables element
content according to namespaces, there is a corresponding
anyAttribute element which enables attributes to
appear in elements. For example, we can permit any XHTML
attribute to appear as part of the htmlExample
element by adding
anyAttribute to its declaration:
NOTE:
Modification to htmlExample Declaration to Allow XHTML
Attributes
<element name="htmlExample">
<complexType>
<sequence>
<any namespace="http://www.w3.org/1999/xhtml"
minOccurs="1" maxOccurs="unbounded"
processContents="skip"/>
</sequence>
<anyAttribute namespace="http://www.w3.org/1999/xhtml"/>
</complexType>
</element>
This declaration permits an XHTML attribute, say
href, to appear in the htmlExample
element. For example:
NOTE:
An XHTML attribute in the htmlExample Element
....
<htmlExample xmlns:h="http://www.w3.org/1999/xhtml"
h:href="http://www.example.com/reports/4Q99.html">
<!-- XHTML markup here -->
</htmlExample>
....
The
namespace attribute in an anyAttribute element can
be set to any of the values listed in
Table 4 for the
any element, and anyAttribute can be
specified with a processContents
attribute. In contrast to an
any element, anyAttribute cannot
constrain the number of attributes that may appear in an
element.
|