ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index | >> Example 1 / 3 << | Prev | Next |
Contents > attributeFormDefault > Target namespace is not null, attributeFormDefault is qualified

Target namespace is not null, attributeFormDefault is qualified

  1. qualified
  2. Relax NG - attribute from namespace http://foo
XML Schema keys: attributeFormDefault, attribute
Relax NG keys: attribute, ns

1. qualified

The target namespace is not null here. If the attribute "attributeFormDefault" is set to "qualified", the attribute "xx" must be from the target namespace.

Valid document


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

Invalid document
The attribute "xx" must be from the "http://foo" namespace.


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

Correct XML Schema (correct_0.xsd)


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

  <xsd:element name="root">
    <xsd:complexType>
      <xsd:attribute name="xx" type="xsd:string" use="required"/>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

2. Relax NG - attribute from namespace http://foo

If we want the attribute to be from some particular non-null namespace, we must specify this explicitly, using the "name" element with "ns" attribute, or element "attribute" with both "name" and "ns" attributes.

Valid document


<f:root f:xx="1" xmlns:f="http://foo" />

Invalid document
The attribute "xx" must be from the "http://foo" namespace.


<f:root xx="1" xmlns:f="http://foo" />

Correct Relax NG schema (correctRelax_0.rng)
Element "name" with "ns" attribute.


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

  <start>
    <element name="root">
      <attribute>
        <name ns="http://foo">xx</name>
        <text/>
      </attribute>
      <empty/>
    </element>
  </start>
</grammar>

Correct Relax NG schema (correctRelax_1.rng)
Second possibility: element "attribute" with both "name" and "ns" attributes.


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

  <start>
    <element name="root">
      <attribute name="xx" ns="http://foo">
        <text/>
      </attribute>
      <empty/>
    </element>
  </start>
</grammar>