Declaring Namespaces in Schemas

Namespace declarations in a RELAX NG schema follow the same principles as namespace declarations in an instance document, with some small differences in the syntax. RELAX NG supports the use of both the default namespace and prefixes.

Using the Default Namespace

The namespace on which a schema expects to operate in the instance document can be defined through the ns attribute. Like the datatypeLibrary attribute seen earlier, ns is an inherited attribute. Being inherited means that you can define it in the document element of the schema (and never again) if it remains the same throughout the schema. For instance, to write a schema for the first example in this chapter, in which the entire library is using the same namespace, I can write:

<?xml version="1.0" encoding="utf-8"?>
<element xmlns="http://relaxng.org/ns/structure/1.0" name="library"
         ns="http://eric.van-der-vlist.com/ns/library">
 <oneOrMore>
  <element name="book">
   <attribute name="id"/>
   ...
  </element>
 </oneOrMore>
</element>

The compact syntax uses a slightly different declaration, default namespace, at the top of the schema:

default namespace = "http://eric.van-der-vlist.com/ns/library"

element library
 {
   element book
   {
      attribute id { text },
       ...
      }*
   }+
}

Tip

The definition of the default namespace in a RELAX NG schema doesn’t apply to attributes. This works precisely as expected, because the default namespace doesn’t apply to attributes in instance documents and should cause a minimum of surprises. ...

Get RELAX NG now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.