CSPP51038 Lecture 2

50
CSPP51038 CSPP51038 Lecture Lecture 2 2 XML XML Simple XML Schema Simple XML Schema

description

CSPP51038 Lecture 2. XML Simple XML Schema. XML in messaging. Most modern languages have method of representing structured data. Typical flow of events in application. Read data (file, db, socket). Marshal objects. Manipulate in program. Unmarshal (file, db, socket). - PowerPoint PPT Presentation

Transcript of CSPP51038 Lecture 2

Page 1: CSPP51038 Lecture  2

CSPP51038CSPP51038

LectureLecture 2 2XMLXML

Simple XML SchemaSimple XML Schema

Page 2: CSPP51038 Lecture  2

XML in messagingXML in messaging

Most modern languages have method of Most modern languages have method of representing structured data.representing structured data.

Typical flow of events in applicationTypical flow of events in application

Read data(file, db, socket)

Marshalobjects

Manipulate inprogram

Unmarshal (file, db, socket)

•Many language-specific technologies to reduce these steps: RMI, object serialization in any language, CORBA (actually somewhat language neutral), MPI, etc.

•XML provides a very appealing alternative that hits the sweet spot for many applications

Page 3: CSPP51038 Lecture  2

User-defined types in programming User-defined types in programming languageslanguages

One view of XML is as a text-based, programming-One view of XML is as a text-based, programming-language-neutral way of representing structured language-neutral way of representing structured

information.information. Compare:Compare:

struct Student{ char* name; char* ssn; int age; float gpa;}

class Student{ public String name; public String ssn; public int age; public float gpa;}

C Java Fortrantype Student character(len=*) :: name character(len=*) :: ssn integer :: age real :: gpaend type Student

Page 4: CSPP51038 Lecture  2

Sample XML SchemaSample XML Schema

• In XML, (a common) datatype description is called an XML schema.• DTD and Relax NG are other common alternatives• Below uses schema just for illustration purposes• Note that schema itself is written in XML

<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="student"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="ssn" type="xs:string"/> <xs:element name="age" type="xs:integer"/> <xs:element name="gpa" type="xs:decimal"/> </xs:sequence> </xs:complexType> </xs:element></xs:schema>

Ignore thisFor now

Page 5: CSPP51038 Lecture  2

Alternative schemaAlternative schema

•In this example studentType is defined separately rather than anonymously

<xs:schema> <xs:element name="student" type="studentType“/> <xs:complexType name="studentType"> <xs:sequence>

<xs:element name="name" type="xs:string"/><xs:element name="ssn" type="xs:string"/><xs:element name="age" type="xs:integer"/><xs:element name="gpa" type="xs:decimal"/>

</xs:sequence> </xs:complexType></xs:schema>

new type defined separately

Page 6: CSPP51038 Lecture  2

Alternative: DTDAlternative: DTD• Can also use a DTD (Document Type Descriptor), but this is much simpler than a schema but also much less powerful (notice the lack of types)

<!DOCTYPE Student [ <! – Each XML file is stored in a document whose name is the same as the root node -- > <! ELEMENT Student (name,ssn,age,gpa)> <! – Student has four attributes -- > <!ELEMENT name (#PCDATA)> <! – name is parsed character data -- > <!ELEMENT ssn (#PCDATA)> <!ELEMENT age (#PCDATA)> <!ELEMENT gpa (#PCDATA)>]>

Page 7: CSPP51038 Lecture  2

Another alternative: Relax NGAnother alternative: Relax NG

Gaining in popularityGaining in popularity

Can be very simple to write and at same Can be very simple to write and at same time has many more features than DTDtime has many more features than DTD

Still much less common than SchemaStill much less common than Schema

Page 8: CSPP51038 Lecture  2

Creating instances of typesCreating instances of types

In programming languages, we instantiate objects:

struct Student s1, s2;s1.name = “Andrew”s1.ssn=“123-45-6789”;

Student s = new Student();s1.name = “Andrew”;s1.ssn=“123-45-6789”;.type(Student) :: s1s1%name = ‘Andrew’.

C

Java

Fortran

Page 9: CSPP51038 Lecture  2

Creating XML documentsCreating XML documents

XML is XML is notnot a programming language! a programming language!

In XML we make a Student “object” in an xml file In XML we make a Student “object” in an xml file (Student.xml):(Student.xml):

<Student><Student>

<name>Andrew</name><name>Andrew</name>

<ssn>123-45-6789</ssn><ssn>123-45-6789</ssn>

<age>39</age><age>39</age>

<gpa>2.0</gpa><gpa>2.0</gpa>

</Student> </Student>

Think of this as like a serialized object.Think of this as like a serialized object.

Page 10: CSPP51038 Lecture  2

XML and SchemaXML and Schema

Note that there are two parts to what we didNote that there are two parts to what we did Defining the “structure” layoutDefining the “structure” layout Defining an “instance” of the structureDefining an “instance” of the structure

The first is done with an appropriate Schema or The first is done with an appropriate Schema or DTD.DTD.The second is the XML partThe second is the XML partBoth can go in the same file, or an XML file can Both can go in the same file, or an XML file can refer to an external Schema or DTD (typical)refer to an external Schema or DTD (typical)From this point on we use only SchemaFrom this point on we use only SchemaExercise 1Exercise 1

Page 11: CSPP51038 Lecture  2

??

Question: What can we do with such a file?Question: What can we do with such a file?

Some answers:Some answers:

Write corresponding Schema to define its contentWrite corresponding Schema to define its content

Write XSL transformation to displayWrite XSL transformation to display

Parse into a programming languageParse into a programming language

Page 12: CSPP51038 Lecture  2

Exercise 1Exercise 1

Page 13: CSPP51038 Lecture  2

Exercise 1 SolutionExercise 1 Solution<?xml version="1.0" encoding="UTF-8"?><cars> <car> <make>dodge</make> <model>ram</model> <color>red</color> <year>2004</year> <mileage>22000</mileage> </car>

<car> <make>Ford</make> <model>Pinto</model> <color>white</color> <year>1980</year> <mileage>100000</mileage> </car>

</cars>

Page 14: CSPP51038 Lecture  2

Some sample XML Some sample XML documentsdocuments

Page 15: CSPP51038 Lecture  2

Order / WhitespaceOrder / Whitespace

Note that element order is important, but whitespace in element data is not. This is the same as far as the xml parser is concerned:

<Article ><Headline>Direct Marketer Offended by Term 'Junk Mail' </Headline><authors>

<author> Joe Garden</author><author> Tim Harrod</author>

</authors><abstract>Dan Spengler, CEO of the direct-mail-marketing firm Mailbox of

Savings, took umbrage Monday at the use of the term <it>junk mail</it></abstract><body type="url" > http://www.theonion.com/archive/3-11-01.html </body>

</Article>

Page 16: CSPP51038 Lecture  2

Molecule ExampleMolecule Example

XML is extremely useful for standardizing data sharing within XML is extremely useful for standardizing data sharing within specialized domains. Below is a part of the specialized domains. Below is a part of the Chemical Chemical Markup Language Markup Language describing a water molecule and its describing a water molecule and its constituentsconstituents

<?xml version "1.0" ?><?xml version "1.0" ?><CML><CML>

<MOL TITLE="Water" ><MOL TITLE="Water" ><ATOMS> <ATOMS>

<ARRAY BUILTIN="ELSYM" > H O H</ARRAY><ARRAY BUILTIN="ELSYM" > H O H</ARRAY></ATOMS></ATOMS><BONDS><BONDS>

<ARRAY BUILTIN="ATID1" >1 2</ARRAY><ARRAY BUILTIN="ATID1" >1 2</ARRAY><ARRAY BUILTIN="ATID2" >2 3</ARRAY><ARRAY BUILTIN="ATID2" >2 3</ARRAY><ARRAY BUILTIN="ORDER" >1 1</ARRAY><ARRAY BUILTIN="ORDER" >1 1</ARRAY>

</BONDS></BONDS></MOL></MOL>

</CML></CML>

Page 17: CSPP51038 Lecture  2

Rooms exampleRooms example

A typical example showing a few more XML features:A typical example showing a few more XML features:

<?xml version="1.0" ?><?xml version="1.0" ?> <rooms> <rooms>

<room name="<room name="RedRed">">  <capacity><capacity>1010</capacity> </capacity> <equipmentList><equipmentList>

<equipment><equipment>ProjectorProjector</equipment> </equipment>   

</equipmentList></equipmentList>  </room></room><room name="<room name="GreenGreen">">  

<capacity><capacity>55</capacity> </capacity>   <equipmentList /> <equipmentList /> <features><features>   <feature><feature>No RoofNo Roof</feature> </feature>    </features></features>  

</room></room>   </rooms></rooms>

Page 18: CSPP51038 Lecture  2

SuggestionSuggestion

Try building each of those documents in Try building each of those documents in an XML builder tool (XMLSpy, Oxygen, an XML builder tool (XMLSpy, Oxygen, etc.) or at least an XML-aware editor.etc.) or at least an XML-aware editor.

Note: it is not required to create a schema Note: it is not required to create a schema to do this. Just create new XML document to do this. Just create new XML document and start building.and start building.

Page 19: CSPP51038 Lecture  2

Dissecting an XML Dissecting an XML DocumentDocument

Page 20: CSPP51038 Lecture  2

Things that can appear in an XML documentThings that can appear in an XML document

ELEMENTSELEMENTS: : simplesimple, , complexcomplex, , emptyempty, or , or mixedmixed content model; content model; attributes. attributes.

The The XML declarationXML declaration

Processing instructions(PIsProcessing instructions(PIs) ) <? …?><? …?> Most common is Most common is <?xml-stylesheet …?><?xml-stylesheet …?> <?xml-stylesheet type=“text/css” <?xml-stylesheet type=“text/css” href=“mys.css”?>href=“mys.css”?>

CommentsComments <!-- <!-- comment textcomment text --> -->

Page 21: CSPP51038 Lecture  2

Begin TagsEnd Tags

Tags

Attributes

<?xml version "1.0"<?xml version "1.0" ?>?>

<<CMLCML><><MOL TITLE="Water" MOL TITLE="Water" > <> <ATOMSATOMS>> <<ARRAY BUILTIN="ELSYM" ARRAY BUILTIN="ELSYM" >> H O H H O H</</ARRAYARRAY>></</ATOMSATOMS>><<BONDSBONDS>><<ARRAY BUILTIN="ATID1" >1 2ARRAY BUILTIN="ATID1" >1 2</</ARRAYARRAY>><<ARRAY BUILTIN="ATID2" >2 3ARRAY BUILTIN="ATID2" >2 3</</ARRAYARRAY>><<ARRAY BUILTIN="ORDER" >1 1ARRAY BUILTIN="ORDER" >1 1</</ARRAYARRAY>></</BONDSBONDS>></</MOLMOL>></</CMLCML>>

Parts of an XML documentParts of an XML documentDeclaration

AttributeValues

An XML element is everything from (including) the element's start tag to (including) the element's end tag.

Page 22: CSPP51038 Lecture  2

XML and TreesXML and TreesTags give the structure of a Tags give the structure of a document. They divide the document. They divide the document up into document up into Elements, Elements, starting at the top most starting at the top most element, theelement, the root element. root element. The The stuff inside an element is its stuff inside an element is its content – content cancontent – content caninclude other elements along include other elements along with ‘character data’with ‘character data’

CML

MOL

ATOMS BONDS

ARRAY ARRAY ARRAY ARRAY

HOH 12 23 11

Root element

CDATA sections

Page 23: CSPP51038 Lecture  2

XML and XML and TreesTrees

<?xml version "1.0"<?xml version "1.0" ?>?><<CMLCML>>

<<MOL TITLE="Water" MOL TITLE="Water" >><<ATOMSATOMS>>

<<ARRAY BUILTIN="ELSYM" ARRAY BUILTIN="ELSYM" >> H O H H O H</</ARRAYARRAY>></</ATOMSATOMS>><<BONDSBONDS>>

<<ARRAY BUILTIN="ATID1" >1 2ARRAY BUILTIN="ATID1" >1 2</</ARRAYARRAY>><<ARRAY BUILTIN="ATID2" >2 3ARRAY BUILTIN="ATID2" >2 3</</ARRAYARRAY>><<ARRAY BUILTIN="ORDER" >1 1ARRAY BUILTIN="ORDER" >1 1</</ARRAYARRAY>>

</</BONDSBONDS>></</MOLMOL>>

</</CMLCML>>

CML

MOL

ATOMS BONDS

ARRAY ARRAY ARRAY ARRAY

HOH 12 23 11

Root element

Data sections

Page 24: CSPP51038 Lecture  2

XML and TreesXML and Trees

rooms

room

capacity equipmentlistequipmentlist

equipment

capacity

room

features

feature10

projector

5

No Roof

Page 25: CSPP51038 Lecture  2

More detail on elementsMore detail on elements

Page 26: CSPP51038 Lecture  2

Element relationshipsElement relationships

<book> <title>My First XML</title> <prod id="33-657" media="paper"></prod> <chapter>Introduction to XML <para>What is HTML</para> <para>What is XML</para> </chapter> <chapter>XML Syntax <para>Elements must have a closing tag</para> <para>Elements must be properly nested</para> </chapter> </book>

•Book is the root element.

•Title, prod, and chapter are child elements of book.

•Book is the parent element of title, prod, and chapter.

•Title, prod, and chapter are siblings (or sister elements) because they have the same parent.

Page 27: CSPP51038 Lecture  2

Well formed XMLWell formed XML

Page 28: CSPP51038 Lecture  2

Well-formed vs ValidWell-formed vs Valid

An XML document is said to be An XML document is said to be well-well-formedformed if it obeys basic semantic and if it obeys basic semantic and syntactic constraints.syntactic constraints.

This is different from a This is different from a validvalid XML XML document, which (as we will see in more document, which (as we will see in more depth) properly matches a schema.depth) properly matches a schema.

Page 29: CSPP51038 Lecture  2

Rules for Well-Formed XMLRules for Well-Formed XML

An XML document is considered well-formed if it obeys the An XML document is considered well-formed if it obeys the following rules:following rules:

There must be one element that contains all others (root element)There must be one element that contains all others (root element)

All tags must be balanced All tags must be balanced <BOOK>...</BOOK><BOOK>...</BOOK> <BOOK /><BOOK />

Tags must be nested properly:Tags must be nested properly: <BOOK> <LINE> This is OK </LINE> </BOOK><BOOK> <LINE> This is OK </LINE> </BOOK> <LINE> <BOOK> This is </LINE> definitely NOT </BOOK> <LINE> <BOOK> This is </LINE> definitely NOT </BOOK>

OKOK

Element text is case-sensitive soElement text is case-sensitive so <P>This is not ok, even though we do it all the time <P>This is not ok, even though we do it all the time

in HTML!</p>in HTML!</p>

Page 30: CSPP51038 Lecture  2

More Rules for Well-Formed XMLMore Rules for Well-Formed XML

The attributes in a tag must be in quotesThe attributes in a tag must be in quotes < ITEM CATEGORY=“Home and Garden” Name=“hoe-matic < ITEM CATEGORY=“Home and Garden” Name=“hoe-matic

t500”>t500”>

Comments are allowedComments are allowed <!–- They are done just as in HTML… --><!–- They are done just as in HTML… -->

Must begin withMust begin with <?xml version=‘1.0’ ?><?xml version=‘1.0’ ?>

Special characters must be escaped: the most common are Special characters must be escaped: the most common are < < " ' > &" ' > &

<formula> x &lt; y+2x </formula><formula> x &lt; y+2x </formula><cd title="&quot; mmusic"><cd title="&quot; mmusic">

Page 31: CSPP51038 Lecture  2

Naming RulesNaming Rules

Naming rules for XML elementsNaming rules for XML elements Names may contain letters, numbers, and other

characters Names must not start with a number or punctuation

character Names must not start with the letters xml (or XML or

Xml ..) Names cannot contain spaces

Any name can be used, no words are reserved, but the idea is to make names descriptive. Names with an underscore separator are typical

Examples: <first_name>, <date_of_birth>, etc.

Page 32: CSPP51038 Lecture  2

XML ToolsXML Tools

XML can be created with any text editorXML can be created with any text editor

Normally we use an XML-friendly editorNormally we use an XML-friendly editor e.g. XMLSpye.g. XMLSpy nXML emacs extensionsnXML emacs extensions MSXML on WindowsMSXML on Windows OxygenOxygen Etc etc.Etc etc.

To check and validate XML, use either these tools To check and validate XML, use either these tools and/or xmllint on Unix systems.and/or xmllint on Unix systems.

Page 33: CSPP51038 Lecture  2

Another ViewAnother View

XML-as-data is one way to introduce XMLXML-as-data is one way to introduce XML

Another is as a Another is as a markup language markup language similar to html.similar to html.

One typically says that html has a fixed tag set, whereas One typically says that html has a fixed tag set, whereas XML allows the definition of arbitrary tagsXML allows the definition of arbitrary tags

This analogy is particularly useful when the goal is to use This analogy is particularly useful when the goal is to use XML for text presentation -- that is, when most of our XML for text presentation -- that is, when most of our data fields contain textdata fields contain text

Note that mixed element/text fields are permissible in XMLNote that mixed element/text fields are permissible in XML

Page 34: CSPP51038 Lecture  2

Article exampleArticle example

<Article > <Headline>Direct Marketer Offended by Term 'Junk Mail' </Headline> <authors> <author> Joe Garden</author> <author> Tim Harrod</author> </authors> <abstract>Dan Spengler, CEO of the direct-mail-marketing firm Mailbox of Savings, took umbrage Monday at the use of the term <it>junk mail</it>. </abstract> <body type="url" > http://www.theonion.com/archive/3-11-01.html </body>

</Article>

Page 35: CSPP51038 Lecture  2

More uses of XMLMore uses of XML

There is more!There is more!

A very popular use of XML is as a base syntax A very popular use of XML is as a base syntax for programming languages (the elements for programming languages (the elements become program control structures)become program control structures) XSLT, BPEL, ant, etc. are good examplesXSLT, BPEL, ant, etc. are good examples XML is ubiqitous and must have a deep XML is ubiqitous and must have a deep

understanding to be efficient and productiveunderstanding to be efficient and productive

Many other current and potential uses -- up to Many other current and potential uses -- up to the creativity of the programmerthe creativity of the programmer

Page 36: CSPP51038 Lecture  2

XML SchemaXML Schema

There are many details to cover of schema There are many details to cover of schema specification. It is extremely rich, flexible, specification. It is extremely rich, flexible, and somewhat complexand somewhat complex

We will do this in detail next lectureWe will do this in detail next lecture

Now we begin with a brief introductionNow we begin with a brief introduction

Page 37: CSPP51038 Lecture  2

XML Schema

XML itself does not restrict what elements existing in a document.

In a given application, you want to fix a vocabulary -- what elements make sense, what their types are, etc.

Use a Schema to define an XML dialect MusicXML, ChemXML, VoiceXML, ADXML, etc.

Restrict documents to those tags.

Schema can be used to validate a document -- ie to see if it obeys the rules of the dialect.

Page 38: CSPP51038 Lecture  2

Schema determine …What sort of elements can appear in the document.

What elements MUST appear

Which elements can appear as part of another element

What attributes can appear or must appear

What kind of values can/must be in an attribute.

Page 39: CSPP51038 Lecture  2

<?xml version="1.0" encoding="UTF-8"?><library> <book id="b0836217462" available="true"> <isbn> 0836217462 </isbn> <title lang="en"> Being a Dog is a Full-Time Job </title> <author id="CMS"> <name> Charles Schulz </name> <born> 1922-11-26 </born> <dead> 2000-02-12 </dead> </author> <character id="PP"> <name> Peppermint Patty </name> <born> 1966-08-22 </born> <qualification> bold,brash, and tomboyish </qualification> </character> <character id="Snoopy"> <name> Snoopy</name> <born>1950-10-04</born> <qualification>extroverted beagle</qualification> </character> <character id="Schroeder"> <name>Schroeder</name> <born>1951-05-30</born> <qualification>brought classical music to the Peanuts Strip</qualification> </character> <character id="Lucy"> <name>Lucy</name> <born>1952-03-03</born> <qualification>bossy, crabby, and selfish</qualification> </character> </book></library>

• We start with sample XML document and reverse engineer a schema as a simple example

First identify the elements:author, book, born, character,dead, isbn, library, name,qualification, title

Next categorize by contentmodelEmpty: contains nothingSimple: only text nodesComplex: only sub-elementsMixed: text nodes + sub-elements

Note: content model independentof comments, attributes, or processing instructions!

Page 40: CSPP51038 Lecture  2

Content modelsContent models

Simple content model: name, born, title, dead, isbn, qualification

Complex content model: libarary, character, book, author

Page 41: CSPP51038 Lecture  2

Content TypesContent Types

We further distinguish between complex and simple content Types: Simple Type: An element with only text nodes

and no child elements or attributes Complex Type: All other cases

We also say (and require) that all attributes themselves have simple type

Page 42: CSPP51038 Lecture  2

Content TypesContent Types

Simple content type: name, born, dead, isbn, qualification

Complex content type: library, character, book, author, title

Page 43: CSPP51038 Lecture  2

Exercise2 answerExercise2 answer

• In the previous example <book>

• book has element content, because it contains other elements.

• Chapter has mixed content because it contains both text

and other elements.

• Para has simple content (or text content) because it contains only text.

• Prod has empty content, because it carries no

information

Page 44: CSPP51038 Lecture  2

Building the schemaBuilding the schema

Schema are XML documentsSchema are XML documents

They must contain a schema root element as suchThey must contain a schema root element as such <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"

targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified">

... ... </xs:schema>

We will discuss details in a bit -- note that yellow part can be excluded for now.

Page 45: CSPP51038 Lecture  2

Flat schema for libraryFlat schema for library

Start by defining all of the simple types (including attributes):

<xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema> <xs:element name=“name” type=“xs:string”/> <xs:element name=“qualification” type=“xs:string”/> <xs:element name=“born” type=“xs:date”/> <xs:element name=“dead” type=“xs:date”/> <xs:element name=“isbn” type=“xs:string”/> <xs:attribute name=“id” type=“xs:ID”/> <xs:attribute name=“available” type=“xs:boolean”/> <xs:attribute name=“lang” type=“xs:language/> …/…</xs:schema>

Page 46: CSPP51038 Lecture  2

Complex types with simple content

Now to complex types with simple content:

<title lang=“en”> Being a Dog is …</title>

<xs:element name=“title”> <xs:complexType> <xs:simpleContent> <xs:extension base=“xs:string”> <xs:attribute ref=“lang”/> </xs:extension> </xs:simpleContent> </xs:complexType></xs:element>

“the element named title has a complextype which is a simple content obtainedby extending the predefined datatypexs:string by adding the attribute definedin this schema and having the name lang.”

Page 47: CSPP51038 Lecture  2

Complex TypesComplex Types

All other types are complex types with complex content. For example:

<xs:element name=“library”> <xs:complexType> <xs:sequence> <xs:element ref=“book” maxOccurs=“unbounded”/> </xs:sequence> </xs:complexType></xs:element>

<xs:element name=“author”> <xs:complexType> <xs:sequence> <xs:element ref=“name”/> <xs:element ref=“born”/> <xs:element ref=“dead” minOccurs=0/> </xs:sequence> <xs:attribute ref=“id”/> </xs:complexType></xs:element>

Page 48: CSPP51038 Lecture  2

<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="name" type="xs:string"/> <xs:element name="qualification" type="xs:string"/> <xs:element name="born" type="xs:date"> </xs:element> <xs:element name="dead" type="xs:date"> </xs:element> <xs:element name="isbn" type="xs:string"> </xs:element> <xs:attribute name="id" type="xs:ID"> </xs:attribute> <xs:attribute name="available" type="xs:boolean"> </xs:attribute> <xs:attribute name="lang" type="xs:language"> </xs:attribute> <xs:element name="title"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute ref="lang"> </xs:attribute> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> <xs:element name="library"> <xs:complexType> <xs:sequence> <xs:element maxOccurs="unbounded" ref="book"> </xs:element> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="author"> <xs:complexType> <xs:sequence> <xs:element ref="name"> </xs:element> <xs:element ref="born"> </xs:element> <xs:element ref="dead" minOccurs="0"> </xs:element> </xs:sequence> <xs:attribute ref="id"> </xs:attribute> </xs:complexType> </xs:element>

<xs:element name="book"> <xs:complexType> <xs:sequence> <xs:element ref="isbn"> </xs:element> <xs:element ref="title"> </xs:element> <xs:element ref="author" minOccurs="0" maxOccurs="unbounded”/> <xs:element ref="character" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute ref="available"> </xs:attribute> <xs:attribute ref="id"> </xs:attribute> </xs:complexType> </xs:element> <xs:element name="character"> <xs:complexType> <xs:sequence> <xs:element ref="name"/> <xs:element ref="born"/> <xs:element ref="qualification"/> </xs:sequence> <xs:attribute ref="id"> </xs:attribute> </xs:complexType> </xs:element>

</xs:schema>

Page 49: CSPP51038 Lecture  2

<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="library"> <xs:complexType> <xs:sequence> <xs:element name="book" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="isbn" type="xs:integer"> </xs:element> <xs:element name="title"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="lang" type="xs:language" > </xs:attribute> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> <xs:element name="author" minOccurs="0" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"> </xs:element> <xs:element name="born" type="xs:date"> </xs:element> <xs:element name="dead" type="xs:date"> </xs:element> </xs:sequence> <xs:attribute name="id" type="xs:ID"> </xs:attribute> </xs:complexType> </xs:element> <xs:element name="character" minOccurs="0" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"> </xs:element> <xs:element name="born" type="xs:date"> </xs:element> <xs:element name="qualification" type="xs:string" > </xs:element> </xs:sequence> <xs:attribute name="id" type="xs:ID"> </xs:attribute> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute type="xs:ID" name="id"> </xs:attribute> <xs:attribute name="available" type="xs:boolean"> </xs:attribute> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element></xs:schema>

Same schema but with everythingdefined locally!

Page 50: CSPP51038 Lecture  2

Next LectureNext Lecture

Even with this simple example there are Even with this simple example there are many design issues to discussmany design issues to discuss When is a flat layout betterWhen is a flat layout better When is a nested layout betterWhen is a nested layout better What are scoping rulesWhat are scoping rules When to use ref= vs. defining new typeWhen to use ref= vs. defining new type

Schema in depth is topic of next lectureSchema in depth is topic of next lecture