ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index | >> Example 10 / 10 << | Prev | Next |
Contents > Wildcard patterns > Elements from namespace other than target namespace (which is null)

Elements from namespace other than target namespace (which is null)

  1. XML Schema
  2. Relax NG

Please, send all comments, bug-reports, and contributions to Jiri.Jirat@systinet.com. Thank you very much.

XML Schema keys: any, maxOccurs
Relax NG keys: anyName, except, nsName

1. XML Schema

The root element named "root" can have an arbitrary number of any elements from namespace other than the target namespace. The target namespace is null here. The "namespace" attribute will be set to value "##other". It will allow all elements which are from namespace other than null namespace.

Valid document


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

Invalid document
Element is from null namespace (the "target namespace") - that's not allowed.


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

Correct XML Schema (correct_0.xsd)


<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="##other" minOccurs="0" maxOccurs="unbounded" processContents="skip"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

2. Relax NG

The element "anyName" says that the attribute can have any name from any namespace. We will exclude all attributes from the null namespace ("target namespace") using the "except" and "nsName" elements.

Valid document


<root xmlns="">
  <a:a xmlns:a="http://bar" />
</root>

Invalid document
Element is from null namespace (the "target namespace") - that's not allowed.


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

Correct Relax NG schema (correctRelax_0.rng)


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

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