Class 6 Notes for XML

Learning Objectives:

You will be able to create a namespace for an XML document.

You will be able to create a schema for an XML document.

.You will be able to identify the difference between DTD and Scehmas.


Read the Schema Intro through Simple Types Tutorials from W3schools

Schema is an XML based and granular version of DTD!

The book breaks down Schema into DataType & Structures. Syntax for Schemas:

A DataType specifies the type of information to be found and add constraints as to what is acceptable.

Syntax of Schema:

Schema Element Structures are Simple (no Children or attributes) or Complex (By definition, the root element must be complex!!)


Attributes: <xs:attribute name="AttributeName" type="xs:string"/>

Simple Elements: <xs:element name="ElementName" type="xs:string"/>

Complex Element:

<xs:element name="ElementName">
      <xs:element name="ChildElement" type="xs:string"/>

      <xs:element name="ChildElement1" type="xs:decimal"/>

      <xs:element name="ChildElement2" type="xs:integer "/>


Create a Schema for your  XML Resume & XML Address book

Topics Covered:

What is a namespace:

A namespace is a set of definitions or rules for naming elements that is located on the Internet.

The W3C definition: An XML namespace is a collection of names, identified by a URI reference, which are used in XML documents as element types and attribute names.

Namespaces and DTDs: A namespace allows you to create predefined names that help with element naming conventions. Namespaces eliminates naming problems (collisions) such as element names that are the same but used differently!


We have used a namespace when we applied Style Sheets

<xsl:stylesheet version="1.0" xmlns:xsl="">
is the namespace for Style Sheets

and we will use a namespace for Schema

<xs:schema xmlns:xs="">
is the namespace for schema.



If I wanted to create a namespace called Jon for my XML documents. I would have to identify a location (URL or URI) on the Web that has the naming convention I created documented.

<Jon:root-element xmlns:Jon="">

example: xmlns:html="" for html namespace

<html:a href=""> Jon </html:a>

<html:img src="logo.gif" />


You declare a name space as an attribute of your root element! xmlns:namespace-prefix="URL-of-namespace"

<jon:TV xmlns:jon="" >

Namespace Syntax is at W3schools. An example of a namespace is XS is the default namespace for Schema's




You can add several restrictions to your schema.

maxOccurs="unbounded" for an= complex element that will repeat itself several times

minOccurs="1" for an element to occur at least one. (default is 1)


Common XML Element Schema Data Types

xs:string xs:integer xs:date
xs:decimal xs:boolean xs:time

Example of Schema:

<?xml version="1.0" ?> To identify it as an XML document

<xs:schema xmlns:xs="">

<xs:element name="RootElement">
      <xs:element name="ChildElement" type="xs:string"/>

      <xs:element name="ChildElement1" type="xs:decimal"/>

      <xs:element name="ChildElement2" type="xs:integer "/>



(Save the file with the extension of xsd, example: jon.xsd)

The Syntax for attching a Schema to an XML Document for Validation

<?xml version="1.0"?>
<RootElement   xsi:noNamespaceSchemaLocation="cis234.xsd"   xmlns:xsi="">

XML Elements & Attributes



A Sample Schema located on W3schools



Create a DTD and Schema for this XML document:


Schema, DataTypes, Simple Elements, Complex Elements,

DataTypes: integer, string, decimal, date, time



Final Step:

Review in your mind everything we covered in class and remember to cover it again before you go to bed so your mind to process this information while you sleep.