XML technologies / XML Schema / Controlling namespaces / Namespaces and XPath expressions

(Excerpt from “XML Schema” by Eric van der Vlist)

Recall that default namespaces do not apply to XPath expressions and that a prefix must be defined for every namespace used in XPath expressions in declarations of xs:unique, xs:key and xs:keyref.

A prefix declaration is therefore necessary for expressions referencing qualified elements and attributes, even if the target namespace is defined as the default namespace. Also note that attribute refer of xs:keyref is a QName that must carry a prefix. An xs:key/xs:keyref for the authors in the previous example could look like this:

<xs:schema targetNamespace="http://dyomedea.com/ns/library" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns:lib="http://dyomedea.com/ns/library" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="library">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="book" type="lib:bookType" maxOccurs="unbounded"/>
        <xs:element name="author" type="lib:authorType" maxOccurs="unbounded"/>
        <xs:element name="characters" type="lib:characterType" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
    <xs:key name="author">
      <xs:selector xpath="lib:author"/>
      <xs:field xpath="@id"/>
    </xs:key>
    <xs:keyref name="keyref" refer="lib:author">
      <xs:selector xpath="lib:book/lib:author-ref"/>
      <xs:field xpath="@ref"/>
    </xs:keyref>
  </xs:element>
</xs:schema>

   

 

 

 

back to top