XML Schema and Namespaces
XML Namespaces
- Solve the problem: Tag name conflicts
- XHTML - HTML 4.0 written in XML
- MathML - Display mathematics equations
- XHTML and MathML overlap: <var>, <select>
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
- Namespace is defined by xmlns:prefix
- prefix is used for the namespace
- The xmlns:prefix attribute is assigned to a URI
- Every tag is prefaced with the prefix name
<?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
- The xmlns attribute can be used in any element
<?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
- xmlns attribute alone, without prefix
- all the enclosed elements are assumed to belong to that namespace
<?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:
- have identical names, or
- have qualified names with the same local part and with prefixes
which have been bound to namespace names that are identical.
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
- To define a "class" of XML Documents
- "instance document" - XML document that conforms to a particular schema
- W3C XML Shema Specification
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
- Root element : schema element
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
...
</xsd:schema>
Parts of XML Schema Document (2)
Named Types:
- Simple Type - only values
<cname>John Punin</cname>
<xsd:element name="cname" type="xsd:string"/>
Complex Type - elements and/or attributes
<owner>
<cname>John Punin</cname>
<email>puninj@cs.rpi.edu</email>
</owner>
<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="owner" type="record"/>
Simple Types
- string - "Programming XML in Java"
- integer - 32, -45
- decimal - 12.4, -2.3
- float - 12.4, 3.6E-5, INF, NaN
- boolean - true, false
- time - 15:23:00.000-04:00
- date - 2001-02-18
- ID - same as in DTD
- NMTOKEN - same as in DTD
Simple Type Examples
<xsd:simpleType name='bodytemp'>
<xsd:restriction base='xsd:decimal'>
<xsd:precision value='4'/>
<xsd:scale value='1'/>
<xsd:minInclusive value='97.0'/>
<xsd:maxInclusive value='105.0'/>
</xsd:restriction>
</xsd:simpleType>
Simple Type Examples (2)
Derived type
<xsd:simpleType name='healthyBodytemp'>
<xsd:restriction base='bodytemp'>
<xsd:maxInclusive value='99.5'/>
</xsd:restriction>
</xsd:simpleType>
Using the enumeration facet
<xsd:simpleType name="myWeek">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="sunday"/>
<xsd:enumeration value="monday"/>
.....
</xsd:restriction>
</xsd:simpleType>
Complex Types
(title?,firstname*,lastname)
<xsd:complexType name="name">
<xsd:sequence>
<xsd:element name="title" minOccurs="0"/>
<xsd:element name="firstname" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="lastname"/>
</xsd:sequence>
</xsd:complexType>
Complex Types (2)
Derived Type by Extension
(title?, firstname*, lastname, genMark?)
<xsd:complexType name="fullName">
<xsd:complexContent>
<xsd:extension base="name">
<xsd:sequence>
<xsd:element name="genMark" minOccurs="0"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
Complex Types (3)
Derived Type by Restriction
(firstname+, lastname)
<xsd:complexType name="simpleName">
<xsd:complexContent>
<xsd:restriction base="name">
<xsd:sequence>
<xsd:element name="title" maxOccurs="0"/>
<xsd:element name="firstname" minOccurs="1"/>
<xsd:element name="lastname"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
Instances
<name>
<title>Mr</title>
<lastname>Punin</lastname>
</name>
<name xsi:type="simpleName">
<firstname>John</firstname>
<firstname>R</firstname>
<lastname>Punin</lastname>
</name>
<name xsi:type="fullName">
<firstname>John</firstname>
<lastname>Kennedy<lastname>
<genMark>Jr</genMark>
</name>
Compositors
(title?,firstname*,lastname)
<xsd:complexType name="name">
<xsd:sequence>
<xsd:element name="title" minOccurs="0"/>
<xsd:element name="firstname" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="lastname"/>
</xsd:sequence>
</xsd:complexType>
Compositors (2)
(PRODUCT, NUMBER, (PRICE | CHARGEACCT | SAMPLE))
<xsd:complexType name="itemType">
<xsd:sequence>
<xsd:element ref="PRODUCT"/>
<xsd:element ref="NUMBER"/>
<xsd:choice>
<xsd:element ref="PRICE"/>
<xsd:element ref="CHARGEACCT"/>
<xsd:element ref="SAMPLE"/>
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
Groups
(PRODUCT, NUMBER, (PRICE | CHARGEACCT | SAMPLE))
<xsd:group name="chargeGroup">
<xsd:choice>
<xsd:element ref="PRICE"/>
<xsd:element ref="CHARGEACCT"/>
<xsd:element ref="SAMPLE"/>
</xsd:choice>
</xsd:group>
<xsd:complexType name="itemType">
<xsd:sequence>
<xsd:element ref="PRODUCT"/>
<xsd:element ref="NUMBER"/>
<xsd:group ref="chargeGroup"/>
</xsd:sequence>
</xsd:complexType>
Elements
Elements with Simple types:
<xsd:element name="cname" type="xsd:string"/>
Elements with Anonymous Complex types:
<xsd:element name="owner>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="cname" type="xsd:string"/>
<xsd:element name="email" type="xsd:string/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Elements with Named Complex types:
<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="owner" type="record"/>
References to Elements:
DTD:
<!ELEMENT BOOK (P*)>
<!ELEMENT P (#PCDATA)>
XML Schema:
<xsd:element name="P" type="xsd:string"/>
<xsd:element name="BOOK">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="P" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
Attributes and Complex Types from Simple Types
DTD:
<!ELEMENT greeting (#PCDATA)>
<!ATTLIST greeting
language CDATA "English">
XML Schema:
<xsd:element name="greeting">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="language" type="xsd:string"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
XML Instance Document:
<greeting language="Spanish">
Hola!
</greeting>
Mixed Content
DTD:
<!ELEMENT p (#PCDATA | b)*>
<!ELEMENT b (#PCDATA)>
XML Schema:
<xsd:complexType name="boldText" mixed="true">
<xsd:sequence>
<xsd:element name="b" type="xsd:string"
minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="p" type="boldText"/>
XML Instance Document:
<p>This is <b>bold</b> text</p>
Empty Content
<img src="xmlj.gif" alt="Programming XML in Java icon"/>
<xsd:element name="img">
<xsd:complexType>
<xsd:attribute name="src" type="xsd:NMTOKEN"/>
<xsd:attribute name="alt" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
Attribute Groups
DTD:
<!ELEMENT img EMPTY>
<!ATTLIST img
alt CDATA #REQUIRED
src CDATA #REQUIRED
width CDATA #IMPLIED
height CDATA #IMPLIED>
XML Schema:
<xsd:attributeGroup name="imgAttributes">
<xsd:attribute name="alt" type="xsd:string" use="required"/>
<xsd:attribute name="src" type="xsd:NMTOKEN" use="required"/>
<xsd:attribute name="width" type="xsd:integer"/>
<xsd:attribute name="height" type="xsd:integer"/>
</xsd:attributeGroup>
<xsd:element name="img">
<xsd:complexType>
<xsd:attributeGroup ref="imgAttributes"/>
<xsd:complexType>
</xsd:element>
Namespaces
XML Schema:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.cs.rpi.edu/addressBook"
targetNamespace="http://www.cs.rpi.edu/addressBook"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<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>
Namespaces (2)
XML Instance Document:
<?xml version="1.0"?>
<addressBook xmlns="http://www.cs.rpi.edu/addressBook"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.cs.rpi.edu/addressBook
http://www.cs.rpi.edu/~puninj/addressBook.xsd"
>
<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>