start page | rating of books | rating of authors | reviews | copyrights

Book HomeXML SchemaSearch this book

10.5. Namespaces and XPath Expressions

It's time to remember that default namespaces do not apply to XPath expressions, and that a prefix needs to be defined for each namespace used in the XPath expressions of the xs:unique, xs:key, and xs:keyref declarations!

A prefix declaration is therefore required to write expressions that reference qualified elements and attributes, even if the target namespace is defined as the default namespace. It's also important to notice that the xs:keyref refer attribute is a QName that needs to be prefixed, and a xs:key/xs:keyref on the authors in (in our previous example) with a namespace could become:

<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>


Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.