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

Attributes from namespace other then target namespace (which is null)

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

1. XML Schema

The root element named "root" can have arbitrary number of any attributes from namespace other than the target namespace or null namespace. In this case, the "namespace" attribute will be set to value "##other". It will allow all attributes 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" />

Valid document


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

Invalid document
The attribute "x" is from null namespace. That is not allowed.


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

Correct XML Schema (correct_0.xsd)


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

  <xsd:element name="root">
    <xsd:complexType>
      <xsd:anyAttribute namespace="##other" processContents="skip"/>
    </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 empty namespace using the "except" and "nsName" elements.

Valid document


<root xmlns=""/>

Valid document


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

Invalid document
The attribute "x" is from null namespace. That is not allowed.


<root a="2" baz:x="2" xmlns="" xmlns:baz="http://baz" />

Correct Relax NG schema (correctRelax_0.rng)


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

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