xmlxsdxml-namespacescodesynthesis

Defining and referencing XSD types in namespaces


I know xmlns defines a namespace, but I am a bit confused about its use in an XSD file (this is codesynthesis provided example).

<?xml version="1.0"?>

<!--

file      : examples/cxx/parser/library/library.xsd
copyright : not copyrighted - public domain

-->

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:lib="http://www.codesynthesis.com/library"
            targetNamespace="http://www.codesynthesis.com/library">

  <xsd:simpleType name="isbn">
    <xsd:restriction base="xsd:unsignedInt"/>
  </xsd:simpleType>


  <xsd:complexType name="title">
    <xsd:simpleContent>
      <xsd:extension base="xsd:string">
        <xsd:attribute name="lang" type="xsd:string"/>
      </xsd:extension>
    </xsd:simpleContent>
  </xsd:complexType>


  <xsd:simpleType name="genre">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="romance"/>
      <xsd:enumeration value="fiction"/>
      <xsd:enumeration value="horror"/>
      <xsd:enumeration value="history"/>
      <xsd:enumeration value="philosophy"/>
    </xsd:restriction>
  </xsd:simpleType>


  <xsd:complexType name="person">
    <xsd:sequence>
      <xsd:element name="name" type="xsd:string"/>
      <xsd:element name="born" type="xsd:string"/>
      <xsd:element name="died" type="xsd:string" minOccurs="0"/>
    </xsd:sequence>
  </xsd:complexType>


  <xsd:complexType name="author">
    <xsd:complexContent>
      <xsd:extension base="lib:person">
    <xsd:attribute name="recommends" type="xsd:IDREF"/> <!-- Book -->
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>


  <xsd:complexType name="book">
    <xsd:sequence>
      <xsd:element name="isbn" type="lib:isbn"/>
      <xsd:element name="title" type="lib:title"/>
      <xsd:element name="genre" type="lib:genre"/>
      <xsd:element name="author" type="lib:author" maxOccurs="unbounded"/>
    </xsd:sequence>
    <xsd:attribute name="available" type="xsd:boolean" use="required"/>
    <xsd:attribute name="id" type="xsd:ID" use="required"/>
  </xsd:complexType>


  <xsd:complexType name="catalog">
    <xsd:sequence>
      <xsd:element name="book" type="lib:book" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>


  <xsd:element name="catalog" type="lib:catalog"/>

</xsd:schema>

Why are the new types which are defined like person, authorreferenced with lib namespace prefix but not with xsdprefix, where both are defined in document? What makes them belong to lib but not to xsd?

Secondly they are referenced standalone when they are defined but they have namespace prefix when they are being used. Shouldn't they be defined with namespace prefix as well?

For example, author doesn't have lib prefix when it is defined but it uses lib:person with namespace prefix (and likewise when author is used later, it belongs to lib!). This adds to the confusion!

  <xsd:complexType name="author">
    <xsd:complexContent>
      <xsd:extension base="lib:person">
    <xsd:attribute name="recommends" type="xsd:IDREF"/> <!-- Book -->
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

Solution

  • Why are the new types which are defined like person, author referenced with lib namespace prefix but not with xsd prefix, where both are defined in document? What makes them belong to lib but not to xsd?

    The xsd namespace prefix is defined by

    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    

    This namespace is reserved for XML Schema constructs.

    The lib namespace prefix is defined by

    xmlns:lib="http://www.codesynthesis.com/library"
    

    This namespace is user-defined (controlled by Codesynthesis, in this case, for its components).

    Secondly they are referenced standalone when they are defined but they have namespace prefix when they are being used. Shouldn't they be defined with namespace prefix as well?

    No, for a targetNamespace declaration such as:

    targetNamespace="http://www.codesynthesis.com/library"
    

    A definition of a type such as author (no namespace prefix here),

    <xsd:complexType name="author">...</xsd:complexType>
    

    is automatically in the http://www.codesynthesis.com/library namespace, but must be referenced via the corresponding namespace prefix, type="lib:author".