ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index | >> Example 4 / 4 << | Prev | Next |
Contents > elementFormDefault - elements > elementFormDefault qualified for not-top-level elements, namespace non-null

elementFormDefault qualified for not-top-level elements, namespace non-null

  1. XML Schema
  2. Relax NG
XML Schema keys: elementFormDefault
Relax NG keys: ns

1. XML Schema

Because the attribute "elementFormDefault" is set to "qualified", the child must be from the namespace "http://foo" (from the "targetNamespace").

Valid document


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

Invalid document
The following document is not valid.


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

Correct XML Schema (correct_0.xsd)


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

  <xsd:element name="root">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="e1" type="xsd:string"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

2. Relax NG

We can set the namespace for all elements using the "ns" attribute. The value is inherited and now all elements must be from the "http://foo" namespace.

Valid document


<root xmlns="http://foo" >
  <e1>Element 1</e1>
</root>

Correct Relax NG schema (correctRelax_0.rng)


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

  <start>
    <element>
      <name>root</name>
      <element>
        <name>e1</name>
        <text/>
      </element>
    </element>
  </start>
</grammar>