XML Schema and Namespaces

XML Namespaces

Creating a Namespace

XML Document without namespaces
   <?xml version="1.0"?>
   <book>
      <title>Programming XML in Java</title>
   </book>

Creating a Namespace (2)

XML Document with one namespace

   <?xml version="1.0"?>
   <!-- both namespace prefixes are available throughout -->
   <bk:book xmlns:bk='http://www.books.org/books'>
      <bk:title>Programing XML in Java</bk:title>
   </bk:book>

Creating a Namespace (3)

XML Document with namespaces
   <?xml version="1.0"?>
   <!-- both namespace prefixes are available throughout -->
   <bk:book xmlns:bk='http://www.books.org/books'
            xmlns:isbn='urn:ISBN:0-395-36341-6'>
      <bk:title>Programing XML in Java</bk:title>
      <isbn:number>1568491379</isbn:number>
   </bk:book>

Creating Local Namespaces

     <?xml version="1.0"?>
     <!-- both namespace prefixes are available throughout -->
     <bk:book xmlns:bk='http://www.books.org/books'>
        <bk:title>Programing XML in Java</bk:title>
        <isbn:number xmlns:isbn='urn:ISBN:0-395-36341-6'>
           1568491379
        </isbn:number>
     </bk:book>

Default Namespaces

     <?xml version="1.0"?>
     <!-- both namespace prefixes are available throughout -->
     <book xmlns='http://www.books.org/books'>
        <title>Programing XML in Java<title>
        <isbn:number xmlns:isbn='urn:ISBN:0-395-36341-6'>
           1568491379
        </isbn:number>
     </book>

Combining XHTML and MathML using namespaces

   <?xml version="1.0" encoding="UTF-8"?>
   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
             "DTD/xhtml1-strict.dtd">
   <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   <head>
      <title>A Math Example</title>
   </head>
   <body>
   <p>The following is MathML markup:</p>
      <math xmlns="http://www.w3.org/1998/Math/MathML">
         <apply> 
         <log/>
         <logbase><cn>3</cn></logbase> 
         <ci> x </ci>
      </apply>
      </math>
   </body>
   </html>

Uniqueness of Attributes

No tag may contain two attributes which:

Uniqueness of Attributes (2)

Legal Example :
   <?xml version="1.0"?>
   <RESERVATION xmlns="http://www.aeroline.com/reservations"
                xmlns:html="http://www.w3.org/1999/xhtml">
      <NAME html:class="largeSansSerif">Layman, A</NAME>
      <SEAT class="Y" html:class="largeMonotype">33B</SEAT>
      <html:a href='/cgi-bin/ResStatus'>Check Status</html:a>
      <DEPARTURE>1997-05-24T07:55:00+1</DEPARTURE>
   </RESERVATION>

Uniqueness of Attributes (3)

Illegal Example :
   <!-- http://www.w3.org is bound to n1 and n2 -->
   <x xmlns:n1="http://www.w3.org" 
      xmlns:n2="http://www.w3.org" >
      <bad a="1"     a="2" />
      <bad n1:a="1"  n2:a="2" />
   </x>

XML Schema

A Simple Example

DTD :
   <!ELEMENT text (#PCDATA | emph | name)*>
   <!ATTLIST text 
             timestamp NMTOKEN #REQUIRED>
XML Schema:
   <xsd:element name="text">
      <xsd:complexType mixed="true">
         <xsd:sequence>
            <xsd:element ref="emph"/>
            <xsd:element ref="name"/>
         </xsd:sequence>
         <xsd:attribute name="timestamp" type="xsd:date" use="required"/>
      </xsd:complexType>
   </xsd:element>
XML Document:
   <?xml version="1.0"?>
   <text timestamp="08:45:00.000">
      The deadline of <name>homework 1</name> is 
      <emph>March 9th 2001</emph>.
   </text>

Complete Example

Document:
   <?xml version="1.0"?>
   <addressBook>
      <owner>
         <cname>John Punin</cname>
         <email>puninj@cs.rpi.edu</email>
      </owner>
      <person>
         <cname>Harrison Ford</cname>
         <email>hford@famous.org</email>
      </person>
      <person>
         <cname>Julia Roberts</cname>
         <email>jr@pw.com</email>
      </person>
   </addressBook>
DTD:
   <!ENTITY % record (cname, email)>
   <!ELEMENT addressBook (owner,person*)>
   <!ELEMENT owner %record;>
   <!ELEMENT person %record;>
XML Schema:
   <?xml version="1.0"?>
   <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

   <xsd:complexType name="record">
      <xsd:sequence>
         <xsd:element name="cname" type="xsd:string"/>
         <xsd:element name="email" type="xsd:string/>
      </xsd:sequence>
   </xsd:complexType>

   <xsd:element name="addressBook">
      <xsd:complexType>
         <xsd:sequence>
         <xsd:element name="owner" type="record"/>
         <xsd:element name="person" type="record" 
                        minOccurs="0" maxOccurs="unbounded"/>
         </xsd:sequence>		
      </xsd:complexType>
   </xsd:element> 

   </xsd:schema>

Parts of XML Schema Document

     <?xml version="1.0"?>
     <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     ...
     </xsd:schema>

Parts of XML Schema Document (2)

Named Types: