ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index | >> Example 2 / 3 << | Prev | Next |
Contents > The namespace of document root element > The document root element is from some particular namespace

The document root element is from some particular namespace

  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: targetNamespace
Relax NG keys: ns

1. XML Schema

If we use the attribute "schemaLocation", we say that the schema is for element from some particular namespace. In schema, you need to use the "targetNamespace" attribute, which defines the namespace, from which the element must be.

Valid document


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

Correct XML Schema (correct_0.xsd)


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

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

2. Relax NG

See, how easily this can be accomplished using Relax NG. The pattern <name ns="http://foo">root</name> says, that the root element must be named "root" and that it must be from the namespace "http://foo".

Valid document


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

Correct Relax NG schema (correctRelax_0.rng)


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

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

Correct Relax NG schema (correctRelax_1.rng)
This short version is also valid.


<element xmlns="http://relaxng.org/ns/structure/1.0" >
  <name ns="http://foo">root</name>
  <text/>
</element>