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.

Homework:

Read the Schema Intro through Simple Types Tutorials from W3schools

Schema is an XML based and granular version of DTD!

  • Schemas are extensible to future additions
  • Schemas are richer and more useful than DTDs
  • Schemas are written in XML
  • Schemas support data types
  • Schemas support namespaces

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:complexType>
    <xs:sequence>
      <xs:element name="ChildElement" type="xs:string"/>

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

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

    </xs:sequence>
  </xs:complexType>
</xs:element>

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="http://www.w3.org/1999/XSL/Transform">
is the namespace for Style Sheets

and we will use a namespace for Schema

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
is the namespace for schema.

 

Example:

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="http://e-commerce.pvc.maricopa.edu/jon">

example: xmlns:html="http://www.w3.org/TR/html4/" for html namespace

<html:a href="http://www.asu.edu"> 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="http://www.paradisevalley.edu/~storslee/xml/xml" >

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="http://www.w3.org/2001/XMLSchema">

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

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

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

    </xs:sequence>
  </xs:complexType>
</xs:element>

</xs:schema>

(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="http://www.w3.org/2001/XMLSchema-instance">

XML Elements & Attributes

</RootElement>

 

A Sample Schema located on W3schools

 

Practice:

Create a DTD and Schema for this XML document:

Terms:

Schema, DataTypes, Simple Elements, Complex Elements,

DataTypes: integer, string, decimal, date, time

 

 

Dr. Jon Storslee
Phone: 602-787-6734

HTML Valid


Paradise Valley Community CollegeĀ 
18401 N. 32nd Street Phoenix, AZ 85032