Namespaces

With the help of namespaces, elements and, where appropriate, attributes can be uniquely identified in XML. In this way, duplications can be avoided when naming elements. If there are several elements with the same name but another meaning in the context, they can be distinguished by the assigned namespace. The prefix of an element (e.g. xsl: of <xsl:value-of>) serves as an abbreviation for the assigned namespace URI which is usually indicated in the root element. The prefix is also required in XPath expressions in order to be able to have access to the elements from the correct namespace.


The <ns> element is required for the example since the namespace "http://www.schematron.info/ark" has been used in the ark.xml instance. If elements belonging to this namespace are addressed via XPath in a Schematron schema, a <ns> element has to be added to define a prefix for this namespace.

<schema xmlns="http://purl.oclc.org/dsdl/schematron">
  <ns uri="http://www.schematron.info/ark" prefix="ark"/>       (1)
  <pattern>
    <rule context="ark:animal[@carnivore='yes']">               (2)
      <report test="parent::*/ark:animal[@carnivore='no']">     (2)
      There are carnivores and herbivores in one accommodation. 
      The animals are not a food source!
      </report>
    </rule>
  </pattern>
</schema>
(1) The <ns> element assigns a prefix to the namespace indicated in the uri attribute.
(2) Regarding the <ns> element, the prefix can be used for the XPath expressions. If the <ns> element is missing, no prefix is defined.

Note to XPath /XML experts:
Theoretically, the <ns> element can always be omitted. This leads, however, to a complicated avoiding of the application of prefixes when addressing outside the NULL namenspace. For example, this can be done with a query for the local name with the local-name() function.

The *[local-name()='animal'] expression matches all <animal> elements, regardless of the namespace of the element.
It should be noted that in contrast to XSLT, Schematron offers no possibility to define a default  namespace. This means that all elements which are part of a namespace in the instance receive a prefix during the addressing. This prefix has to be defined per <ns> element for the appropriate namespace. If there is more than one namespace in the instance, the Schematron schema has to contain an appropriate <ns> element for each namespace.

<< back next >>

 

 

 


Copyright © dpunkt.verlag GmbH 2011
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "Schematron - Effiziente Business Rules für XML-Dokumente" is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.

dpunkt.verlag GmbH, Ringstraße 19B, 69115 Heidelberg, fon 06221-14830, fax 06221-148399, hallo(at)dpunkt.de