ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index | >> Example 8 / 8 << | Prev | Next |
Contents > Restrictions > Element from any

Element from any

  1. Correct XML Schema
  2. Incorrect XML Schema - wrong occurrence range
XML Schema keys: restriction, element, any

1. Correct XML Schema

Type "AAA" allows one arbitrary element from any namespace. We declare type "BBB", which is a restriction of the type "AAA" and this restriction will allow only element "x" from the target namespace. The target namespace is null here. Schema Component Constraint: Particle Derivation OK (Elt:Any - NSCompat) .

Valid document


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

Invalid document
The type "BBB" allows only one "x" element from null namespace.


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

Invalid document
The type "BBB" allows only one "x" element from null namespace.


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

Correct XML Schema (correct_0.xsd)


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

  <xsd:element name="root" type="BBB"/>

  <xsd:complexType name="AAA">
    <xsd:sequence>
      <xsd:any namespace="##any" minOccurs="1" maxOccurs="1" processContents="skip"/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="BBB">
    <xsd:complexContent>
      <xsd:restriction base="AAA">
        <xsd:sequence>
          <xsd:element name="x" minOccurs="1" maxOccurs="1"/>
        </xsd:sequence>
      </xsd:restriction>
    </xsd:complexContent>
  </xsd:complexType>
</xsd:schema>

2. Incorrect XML Schema - wrong occurrence range

The restriction is not valid, because the occurrence range of element "x" (in the derived type) is higher than of the wildcard (represented by "xsd:any") in the original type. Schema Component Constraint: Particle Derivation OK (Elt:Any - NSCompat), 2 .

Incorrect XML Schema (incorrect_0.xsd)


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

  <xsd:element name="root" type="BBB"/>

  <xsd:complexType name="AAA">
    <xsd:sequence>
      <xsd:any namespace="##any" minOccurs="1" maxOccurs="1" processContents="skip"/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="BBB">
    <xsd:complexContent>
      <xsd:restriction base="AAA">
        <xsd:sequence>
          <xsd:element name="x" minOccurs="1" maxOccurs="2"/>
        </xsd:sequence>
      </xsd:restriction>
    </xsd:complexContent>
  </xsd:complexType>
</xsd:schema>