ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index | >> Example 7 / 10 << | Prev | Next |
Contents > Wildcard patterns > Arbitrary number of elements from any namespace

Arbitrary number of elements from any namespace

  1. XML Schema
  2. Relax NG
XML Schema keys: any
Relax NG keys: anyName, except, nsName

1. XML Schema

The root element named "root" can have an arbitrary number of any elements from any namespace.

Valid document


<root xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />

Valid document


<root xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <a/>
</root>

Valid document


<root xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <a/>
  <x:b xmlns:x="http://foo" />
</root>

Correct XML Schema (correct_0.xsd)
We must set the "minOccurs" and "maxOccurs" attributes to 0 and "unbounded", respectively - to override default values.


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >

  <xsd:element name="root">
    <xsd:complexType>
      <xsd:sequence minOccurs="1" maxOccurs="1">
        <xsd:any namespace="##any" minOccurs="0" maxOccurs="unbounded" processContents="skip"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

2. Relax NG

The element "anyName" says that the elements can have any name from any namespace. If we want to allow an arbitrary number of such attributes, we need to use the "zeroOrMore" element

Valid document


<root xmlns=""/>

Valid document


<root xmlns="">
  <a/>
</root>

Valid document


<root xmlns="">
  <a/>
  <x:b xmlns:x="http://foo" />
</root>

Correct Relax NG schema (correctRelax_0.rng)


<grammar xmlns="http://relaxng.org/ns/structure/1.0" >

  <start>
    <element>
      <name ns="">root</name>
      <zeroOrMore>
        <element>
          <anyName/>
          <text/>
        </element>
      </zeroOrMore>
    </element>
  </start>
</grammar>