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

Attributes from namespace other than target namespace (which is not 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: 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. The target namespace is not null here. The "namespace" attribute will be set to value "##other". It will allow all attributes which are from namespace other than targetNamespace or null.

Valid document


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

Invalid document
Attribute is from null namespace (that's not allowed).


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

Invalid document
Attribute is from the targetNamespace (that's not allowed).


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

Correct XML Schema (correct_0.xsd)


<xsd:schema targetNamespace="http://foo" 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 and the "target namespace" using the "except" and "nsName" elements.

Valid document


<root xmlns="http://foo" />

Valid document


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

Invalid document
Attribute is from null namespace (that's not allowed).


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

Invalid document
Attribute is from the namespace "http://foo" (that's not allowed).


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

Correct Relax NG schema (correctRelax_0.rng)


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

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