Namespace Patterns

Namespace Patterns

Patterns using Namespace

1. Defining the default namespace for an element
2. Adding namespaces with a prefix
3. Wildcards
4. Exceptions.
1.

Defining the default namespace for an element

DaveP

This example associates the doc element and all its descendants with the default namespace of http://www.dpawson.co.uk/ns#

 <element  name="doc"
          ns="http://www.dpawson.co.uk/ns#">
    <attribute name="id"/>
    <text/>
 </element>
     

The default may be changed similarly, by a further definition, with a different namespace

2.

Adding namespaces with a prefix

DaveP

This shows a similar element which requires the dp prefix

<element  name="doc"
          xmlns:dp="http://www.dpawson.co.uk/ns#">
    <attribute name="id"/>
     <text/>
 </element>
     

This allows for an instance such as

<dp:doc id="a23">
   document content
</dp:doc>

It also enables easier definition of elements in the given namespace, using

<element  name="dp:doc"
             xmlns:dp="http://www.dpawson.co.uk/ns#"
             xmlns:sp="http://www.dpawson.co.uk/ns/rss#">
    <attribute name="id"/>
  <element name="dp:section">
   <element name="dp:para">
    <text/>
   </element>
   <element name="sp:para">
   <text/>
   </element>
 </element>
</element>
     

Once the namespaces have been defined, its easy enough to use the prefixes to help define other elements in one of the namespaces.

3.

Wildcards

DaveP

The wildcard pattern can be used to allow namespaced elements from any other namespace. For instance, to include any element in the XHTML namespace, the following definition could be used.

  <define name="XHTML">
    <element>
      <nsName ns="http://www.w3.org/1999/xhtml"/>
      <zeroOrMore>
        <attribute>
          <anyName/>
        </attribute>
      </zeroOrMore>
      <zeroOrMore>
        <choice>
          <text/>
          <ref name="XHTML"/>
        </choice>
      </zeroOrMore>
    </element>
  </define>

     

Then at an appropriate time, this can be referenced

<element name="dp:insert">
  <ref name="XHTML"/>
</element>

so within the <dp:insert> element, any element from the XHTML namespace can be used.

Note that without the nsName element, any element in any namespace could be added.

4.

Exceptions.

DaveP

SGML old hands may remember the ability to specify the content of an element as being one from a list of elements, except for this that or the other element. This used to look something like

<!ELEMENT Footnote - O (#PCDATA|Para+) -(Footnote) -- no footnotes in footnotes -->
     

Which meant that a Footnote could contain PCDATA, Para elements, but could not contain other footnote elements.

RelaxNG has a similar idea using the following syntax.

  <define name="general">
  <element>
   <anyName>
    <except>
     <nsName ns="http://www.dpawson.co.uk/rss/ns#"/>
     <nsName/>
    </except>
   </anyName>
   <ref name="anything"/>
  </element>
 </define>
 

  <define name="anything">
    <zeroOrMore>
      <choice>
        <element>
          <anyName/>
          <ref name="anything"/>
        </element>
        <attribute>
          <anyName/>
        </attribute>
        <text/>
      </choice>
    </zeroOrMore>
  </define>


The <except> element used here has a few effects. It disallows anything from the default namespace, it disallows anything from the given namespace (.../rss/ns#) but it will allow elements from the ../rs/ns# namespace if the element has another namespace. For instance the following is valid.

<html xmlns ="http://www.xhtml">
  <head><title> xxx </title> </head>
  <body>
<h2>Header </h2>
<x xmlns="">

</x>
</body>
</html>


but if any element in the ../rss/ns# namespace is present outside of the <html> element, it will be identified as invalid