title: Defining and referencing XSD types in namespaces
body: <p>I know <code>xmlns</code> defines a namespace, but I am a bit confused about its use in an XSD file (this is codesynthesis provided example).</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  
  &lt;!--
  
  file      : examples/cxx/parser/library/library.xsd
  copyright : not copyrighted - public domain
  
  --&gt;
  
  &lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
              xmlns:lib="http://www.codesynthesis.com/library"
              targetNamespace="http://www.codesynthesis.com/library"&gt;
  
    &lt;xsd:simpleType name="isbn"&gt;
      &lt;xsd:restriction base="xsd:unsignedInt"/&gt;
    &lt;/xsd:simpleType&gt;
  
  
    &lt;xsd:complexType name="title"&gt;
      &lt;xsd:simpleContent&gt;
        &lt;xsd:extension base="xsd:string"&gt;
          &lt;xsd:attribute name="lang" type="xsd:string"/&gt;
        &lt;/xsd:extension&gt;
      &lt;/xsd:simpleContent&gt;
    &lt;/xsd:complexType&gt;
  
  
    &lt;xsd:simpleType name="genre"&gt;
      &lt;xsd:restriction base="xsd:string"&gt;
        &lt;xsd:enumeration value="romance"/&gt;
        &lt;xsd:enumeration value="fiction"/&gt;
        &lt;xsd:enumeration value="horror"/&gt;
        &lt;xsd:enumeration value="history"/&gt;
        &lt;xsd:enumeration value="philosophy"/&gt;
      &lt;/xsd:restriction&gt;
    &lt;/xsd:simpleType&gt;
  
  
    &lt;xsd:complexType name="person"&gt;
      &lt;xsd:sequence&gt;
        &lt;xsd:element name="name" type="xsd:string"/&gt;
        &lt;xsd:element name="born" type="xsd:string"/&gt;
        &lt;xsd:element name="died" type="xsd:string" minOccurs="0"/&gt;
      &lt;/xsd:sequence&gt;
    &lt;/xsd:complexType&gt;
  
  
    &lt;xsd:complexType name="author"&gt;
      &lt;xsd:complexContent&gt;
        &lt;xsd:extension base="lib:person"&gt;
      &lt;xsd:attribute name="recommends" type="xsd:IDREF"/&gt; &lt;!-- Book --&gt;
        &lt;/xsd:extension&gt;
      &lt;/xsd:complexContent&gt;
    &lt;/xsd:complexType&gt;
  
  
    &lt;xsd:complexType name="book"&gt;
      &lt;xsd:sequence&gt;
        &lt;xsd:element name="isbn" type="lib:isbn"/&gt;
        &lt;xsd:element name="title" type="lib:title"/&gt;
        &lt;xsd:element name="genre" type="lib:genre"/&gt;
        &lt;xsd:element name="author" type="lib:author" maxOccurs="unbounded"/&gt;
      &lt;/xsd:sequence&gt;
      &lt;xsd:attribute name="available" type="xsd:boolean" use="required"/&gt;
      &lt;xsd:attribute name="id" type="xsd:ID" use="required"/&gt;
    &lt;/xsd:complexType&gt;
  
  
    &lt;xsd:complexType name="catalog"&gt;
      &lt;xsd:sequence&gt;
        &lt;xsd:element name="book" type="lib:book" maxOccurs="unbounded"/&gt;
      &lt;/xsd:sequence&gt;
    &lt;/xsd:complexType&gt;
  
  
    &lt;xsd:element name="catalog" type="lib:catalog"/&gt;
  
  &lt;/xsd:schema&gt;
  </code></pre>
  
  <p>Why are the new types which are defined like <code>person</code>, <code>author</code>referenced with <code>lib</code> namespace prefix but not with <code>xsd</code>prefix, where both are defined in document? What makes them belong to <code>lib</code> but not to <code>xsd</code>? </p>
  
  <p>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?</p>
  
  <p>For example, <code>author</code> doesn't have lib prefix when it is defined but it uses <code>lib:person</code> with namespace prefix (and likewise when <code>author</code> is used later, it belongs to <code>lib</code>!). This adds to the confusion!</p>
  
  <pre><code>  &lt;xsd:complexType name="author"&gt;
      &lt;xsd:complexContent&gt;
        &lt;xsd:extension base="lib:person"&gt;
      &lt;xsd:attribute name="recommends" type="xsd:IDREF"/&gt; &lt;!-- Book --&gt;
        &lt;/xsd:extension&gt;
      &lt;/xsd:complexContent&gt;
    &lt;/xsd:complexType&gt;
  </code></pre>