XML Schema Part I Introduction XML Schema n

  • Slides: 76
Download presentation
XML Schema Part I Introduction

XML Schema Part I Introduction

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

XML Schema n XML itself does not restrict what elements existing in a document. n In a given application, you want to fix a vocabulary -what elements make sense, what their types are, etc. n Use a Schema to define an XML dialect n Music. XML, Chem. XML, Voice. XML, ADXML, etc. n Restrict documents to those tags. n Schema can be used to validate a document -- ie to see if it obeys the rules of the dialect.

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

Schema determine … n What sort of elements can appear in the document. n What elements MUST appear n Which elements can appear as part of another element n What attributes can appear or must appear n What kind of values can/must be in an attribute.

<? xml version="1. 0" encoding="UTF-8"? > • We start with sample <library> XML document

<? xml version="1. 0" encoding="UTF-8"? > • We start with sample <library> XML document and <book id="b 0836217462" available="true"> <isbn> 0836217462 </isbn> reverse engineer a <title lang="en"> Being a Dog is a Full-Time Job </title> schema as a simple <author id="CMS"> example <name> Charles Schulz </name> <born> 1922 -11 -26 </born> First identify the elements: <dead> 2000 -02 -12 </dead> author, book, born, character, </author> dead, isbn, library, name, <character id="PP"> qualification, title <name> Peppermint Patty </name> <born> 1966 -08 -22 </born> Next categorize by content <qualification> bold, brash, and tomboyish </qualification> model </character> Empty: contains nothing <character id="Snoopy"> Simple: only text nodes <name> Snoopy</name> Complex: only sub-elements <born>1950 -10 -04</born> Mixed: text nodes + sub-elements <qualification>extroverted beagle</qualification> </character> Note: content model independent <character id="Schroeder"> of comments, attributes, or <name>Schroeder</name> processing instructions! <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>

Content models n Simple content model: name, born, title, dead, isbn, qualification n Complex

Content models n Simple content model: name, born, title, dead, isbn, qualification n Complex content model: libarary, character, book, author

Content Types n We further distinguish between complex and simple content Types: n n

Content Types n We further distinguish between complex and simple content Types: n n n 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

Content Types n Simple content type: name, born, dead, isbn, qualification n Complex content

Content Types n Simple content type: name, born, dead, isbn, qualification n Complex content type: library, character, book, author, title

Building the schema n n n Schema are XML documents They must contain a

Building the schema n n n Schema are XML documents They must contain a schema root element as such <? xml version="1. 0"? > <xs: schema xmlns: xs="http: //www. w 3. org/2001/XMLSchema" target. Namespace="http: //www. w 3 schools. com" xmlns="http: //www. w 3 schools. com" element. Form. Default="qualified">. . . </xs: schema> We will discuss details in a bit -- note for now that yellow part can be excluded for now.

Flat schema for library Start by defining all of the simple types (including attributes):

Flat schema for library Start by defining all of the simple types (including attributes): <xs: schema xmlns: xs=http: //www. w 3. 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>

Complex types with simple content Now to complex types: <title lang=“en”> Being a Dog

Complex types with simple content Now to complex types: <title lang=“en”> Being a Dog is … </title> <xs: element name=“title”> <xs: complex. Type> <xs: simple. Content> <xs: extension base=“xs: string”> <xs: attribute ref=“lang”/> </xs: extension> </xs: simple. Content> </xs: complex. Type> </xs: element> “the element named title has a complex type which is a simple content obtained by extending the predefined datatype xs: string by adding the attribute defined in this schema and having the name lang. ”

Complex Types All other types are complex types with complex content. For example: <xs:

Complex Types All other types are complex types with complex content. For example: <xs: element name=“library”> <xs: complex. Type> <xs: sequence> <xs: element ref=“book” max. Occurs=“unbounded”/> </xs: sequence> </xs: complex. Type> </xs: element> <xs: element name=“author”> <xs: complex. Type> <xs: sequence> <xs: element ref=“name”/> <xs: element ref=“born”/> <xs: element ref=“dead” min. Occurs=0/> </xs: sequence> <xs: attribute ref=“id”/> </xs: complex. Type> </xs: element>

<? xml version="1. 0" encoding="UTF-8"? > <xs: schema xmlns: xs="http: //www. w 3. org/2001/XMLSchema">

<? xml version="1. 0" encoding="UTF-8"? > <xs: schema xmlns: xs="http: //www. w 3. 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="book"> <xs: element name="isbn" type="xs: string"> </xs: element> <xs: complex. Type> <xs: attribute name="id" type="xs: ID"> </xs: attribute> <xs: sequence> <xs: attribute name="available" type="xs: boolean"> </xs: attribute> <xs: element ref="isbn"> </xs: element> <xs: attribute name="lang" type="xs: language"> </xs: attribute> <xs: element ref="title"> </xs: element> <xs: element name="title"> <xs: element ref="author" min. Occurs="0" max. Occurs="unbound <xs: complex. Type> <xs: element ref="character" min. Occurs="0" max. Occurs="unbou <xs: simple. Content> </xs: sequence> <xs: extension base="xs: string"> <xs: attribute ref="available"> </xs: attribute> <xs: attribute ref="lang"> </xs: attribute> <xs: attribute ref="id"> </xs: attribute> </xs: extension> </xs: complex. Type> </xs: simple. Content> </xs: element> </xs: complex. Type> <xs: element name="character"> </xs: element> <xs: complex. Type> <xs: element name="library"> <xs: sequence> <xs: complex. Type> <xs: element ref="name"/> <xs: sequence> <xs: element ref="born"/> <xs: element max. Occurs="unbounded" ref="book"> </xs: element> <xs: element ref="qualification"/> </xs: sequence> </xs: complex. Type> <xs: attribute ref="id"> </xs: attribute> </xs: element> </xs: complex. Type> <xs: element name="author"> </xs: element> <xs: complex. Type> <xs: sequence> </xs: schema> <xs: element ref="name"> </xs: element> <xs: element ref="born"> </xs: element> <xs: element ref="dead" min. Occurs="0"> </xs: element> </xs: sequence> <xs: attribute ref="id"> </xs: attribute> </xs: complex. Type> </xs: element>

<? xml version="1. 0" encoding="UTF-8"? > <xs: schema xmlns: xs="http: //www. w 3. org/2001/XMLSchema">

<? xml version="1. 0" encoding="UTF-8"? > <xs: schema xmlns: xs="http: //www. w 3. org/2001/XMLSchema"> <xs: element name="library"> <xs: complex. Type> <xs: sequence> <xs: element name="book" max. Occurs="unbounded"> <xs: complex. Type> <xs: sequence> <xs: element name="isbn" type="xs: integer"> </xs: element> <xs: element name="title"> <xs: complex. Type> <xs: simple. Content> <xs: extension base="xs: string"> <xs: attribute name="lang" type="xs: language" > </xs: attribute> </xs: extension> </xs: simple. Content> </xs: complex. Type> </xs: element> <xs: element name="author" min. Occurs="0" max. Occurs="unbounded"> <xs: complex. Type> <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: complex. Type> </xs: element> <xs: element name="character" min. Occurs="0" max. Occurs="unbounded"> <xs: complex. Type> <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: complex. Type> </xs: element> </xs: sequence> <xs: attribute type="xs: ID" name="id"> </xs: attribute> <xs: attribute name="available" type="xs: boolean"> </xs: attribute> </xs: complex. Type> </xs: element> </xs: sequence> </xs: complex. Type> </xs: element> </xs: schema> Same schema but with everything defined locally!

Dissecting Schema

Dissecting Schema

What’s in a Schema? n A Schema is an XML document (a DTD is

What’s in a Schema? n A Schema is an XML document (a DTD is not) n Because it is an XML document, it must have a root element n n The root element is <schema> Within the root element, there can be n Any number and combination of n n n Inclusions Imports Re-definitions Annotations Followed by any number and combinations of n n Simple and complex data type definitions Element and attribute definitions Model group definitions Annotations

Structure of a Schema <schema> <!– any number of the following --> <include. .

Structure of a Schema <schema> <!– any number of the following --> <include. . . /> <import>. . . </import> <redefine>. . . </redefine> <annotation>. . . </annotation> <!– any number of following definitions --> <simple. Type>. . . </simple. Type> <complex. Type>. . . </complex. Type> <element>. . . </element> <attribute/> <attribute. Group>. . . </attribute. Group> <group>. . . </group> <annotation>. . . </annotation> </schema>

Simple Types

Simple Types

Elements n What is an element with simple type? n n A simple element

Elements n What is an element with simple type? n n A simple element is an XML element that can contain only text. It cannot contain any other elements or attributes. Can also add restrictions (facets) to a data type in order to limit its content, and you can require the data to match a defined pattern.

Example Simple Element n The syntax for defining a simple element is: n <xs:

Example Simple Element n The syntax for defining a simple element is: n <xs: element name="xxx" type="yyy"/> where xxx is the name of the element and yyy is the data type of the element. Here are some XML elements: n n n <lastname>Refsnes</lastname> <age>37</age> <dateborn>1968 -03 -27</dateborn> And here are the corresponding simple element definitions: n n n <xs: element name="lastname" type="xs: string"/> <xs: element name="age" type="xs: integer"/> <xs: element name="dateborn" type="xs: date"/>

Common XML Schema Data Types n XML Schema has a lot of built-in data

Common XML Schema Data Types n XML Schema has a lot of built-in data types. Here is a list of the most common types: n n n xs: string xs: decimal xs: integer xs: boolean xs: date xs: time

Declare Default and Fixed Values for Simple Elements n n Simple elements can have

Declare Default and Fixed Values for Simple Elements n n Simple elements can have a default value OR a fixed value set. A default value is automatically assigned to the element when no other value is specified. In the following example the default value is "red": n <xs: element name="color" type="xs: string" default="red"/> A fixed value is also automatically assigned to the element. You cannot specify another value. In the following example the fixed value is "red": n <xs: element name="color" type="xs: string" fixed="red"/>

Attributes (Another simple type) n n All attributes are declared as simple types. Only

Attributes (Another simple type) n n All attributes are declared as simple types. Only complex elements can have attributes!

What is an Attribute? n Simple elements cannot have attributes. If an element has

What is an Attribute? n Simple elements cannot have attributes. If an element has attributes, it is considered to be of complex type. n But the attribute itself is always declared as a simple type. n This means that an element with attributes always has a complex type definition. n

How to Define an Attribute n The syntax for defining an attribute is: n

How to Define an Attribute n The syntax for defining an attribute is: n n n <xs: attribute name="xxx" type="yyy"/> where xxx is the name of the attribute and yyy is the data type of the attribute. Here an XML element with an attribute: <lastname lang="EN">Smith</lastname> And here a corresponding simple attribute definition: <xs: attribute name="lang" type="xs: string"/>

Declare Default and Fixed Values for Attributes n n n Attributes can have a

Declare Default and Fixed Values for Attributes n n n Attributes can have a default value OR a fixed value specified. A default value is automatically assigned to the attribute when no other value is specified. In the following example the default value is "EN": <xs: attribute name="lang" type="xs: string" default="EN"/> A fixed value is also automatically assigned to the attribute. You cannot specify another value. In the following example the fixed value is "EN": n <xs: attribute name="lang" type="xs: string" fixed="EN"/>

Creating Optional and Required Attributes n All attributes are optional by default. To explicitly

Creating Optional and Required Attributes n All attributes are optional by default. To explicitly specify that the attribute is optional, use the "use" attribute: n <xs: attribute name="lang" type="xs: string" use="optional"/> To make an attribute required: n <xs: attribute name="lang" type="xs: string" use="required"/>

Restrictions n n As we will see later, simple types can have ranges put

Restrictions n n As we will see later, simple types can have ranges put on their values These are known as restrictions

Complex Types

Complex Types

Complex Elements n n A complex element is an XML element that contains other

Complex Elements n n A complex element is an XML element that contains other elements and/or attributes. There are four kinds of complex elements: n n n empty elements that contain only other elements that contain only text elements that contain both other elements and text Note: Each of these elements may (or must) contain attributes as well!

Examples of Complex XML Elements n A complex XML element, "product", which is empty:

Examples of Complex XML Elements n A complex XML element, "product", which is empty: n n A complex XML element, "employee", which contains only other elements: n n <employee> <firstname>John</firstname> <lastname>Smith</lastname> </employee> A complex XML element, "food", which contains only text: n n <product pid="1345"/> <food type="dessert">Ice cream</food> A complex XML element, "description", which contains both elements and text: n <description> It happened on <date lang="norwegian">03. 99</date>. . </description >

An Example XML Schema <? xml version="1. 0" encoding="UTF-8"? > <xs: schema xmlns: xs="http:

An Example XML Schema <? xml version="1. 0" encoding="UTF-8"? > <xs: schema xmlns: xs="http: //www. w 3. org/2001/XMLSchema" element. Form. Default="qualified" attribute. Form. Default="unqualified"> <xs: element name="rooms"> <xs: complex. Type> <xs: sequence> <xs: element name="room" min. Occurs="0" max. Occurs="unbounded"> <xs: complex. Type> <xs: sequence> <xs: element name="capacity" type="xs: decimal"/> <xs: element name="equiptment. List"/> <xs: element name="features" min. Occurs="0"> <xs: complex. Type> <xs: sequence> <xs: element name="feature" type="xs: string" max. Occurs="unbounded"/> </xs: sequence> </xs: complex. Type> </xs: element> </xs: sequence> <xs: attribute name="name" type="xs: string" use="required"/> </xs: complex. Type> </xs: element> </xs: sequence> </xs: complex. Type> </xs: element> </xs: schema>

Referencing XML Schema in XML documents

Referencing XML Schema in XML documents

Sample Schema header n The <schema> element may contain some attributes. A schema declaration

Sample Schema header n The <schema> element may contain some attributes. A schema declaration often looks something like this: n <? xml version="1. 0"? > <xs: schema xmlns: xs="http: //www. w 3. org/2001/XMLSchema" target. Namespace="http: //www. w 3 schools. com" xmlns="http: //www. w 3 schools. com" element. Form. Default="qualified">. . . </xs: schema>

Schema headers, cont. n The following fragment: xmlns: xs=http: //www. w 3. org/2001/XMLSchema indicates

Schema headers, cont. n The following fragment: xmlns: xs=http: //www. w 3. org/2001/XMLSchema indicates that the elements and data types used in the schema (schema, element, complex. Type, sequence, string, boolean, etc. ) come from the "http: //www. w 3. org/2001/XMLSchema" namespace. It also specifies that the elements and data types that come from the "http: //www. w 3. org/2001/XMLSchema" namespace should be prefixed with xs: !!

Schema header, cont. n This fragment: n n target. Namespace=http: //www. w 3 schools.

Schema header, cont. n This fragment: n n target. Namespace=http: //www. w 3 schools. com indicates that the elements defined by this schema (note, to, from, heading, body. ) come from the "http: //www. w 3 schools. com" namespace. xmlns=http: //www. w 3 schools. com indicates that the default namespace is "http: //www. w 3 schools. com". This fragment: n element. Form. Default="qualified“ indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified.

Referencing schema in XML n This XML document has a reference to an XML

Referencing schema in XML n This XML document has a reference to an XML Schema: n <? xml version="1. 0"? > <note xmlns="http: //www. w 3 schools. com" xmlns: xsi="http: //www. w 3. org/2001/XMLSchema-instance" xsi: schema. Location="http: //www. w 3 schools. com note. xsd"> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>

Referencing schema in xml, cont. n The following fragment: n n xmlns=http: //www. w

Referencing schema in xml, cont. n The following fragment: n n xmlns=http: //www. w 3 schools. com xmlns= specifies the default namespace declaration. This declaration tells the schema-validator that all the elements used in this XML document are declared in the "http: //www. w 3 schools. com" namespace.

… n Once you have the XML Schema Instance namespace available: n xmlns: xsi=http:

… n Once you have the XML Schema Instance namespace available: n xmlns: xsi=http: //www. w 3. org/2001/XMLSchema-instance you can use the schema. Location attribute. This attribute has two values. The first value is the namespace to use. The second value is the location of the XML schema to use for that namespace: n xsi: schema. Location="http: //www. w 3 schools. com note. xsd"

Using References

Using References

Using References n You don't have to have the content of an element defined

Using References n You don't have to have the content of an element defined in the nested fashion as just shown <xs: element name="rooms"> <xs: complex. Type> <xs: sequence> <xs: element name="room"> <xs: complex. Type> <xs: sequence> <xs: element name="capacity" type="xs: decimal"/> n You can define the element globally and use a reference to it instead <xs: element name="rooms"> <xs: complex. Type> <xs: sequence> <xs: element ref="room"/> </xs: sequence></xs: complex. Type></xs: element> <xs: element name="room"> … </xs: element>

Rooms Schema using References <? xml version="1. 0" encoding="UTF-8"? > <xs: schema xmlns: xs="http:

Rooms Schema using References <? xml version="1. 0" encoding="UTF-8"? > <xs: schema xmlns: xs="http: //www. w 3. org/2001/XMLSchema" element. Form. Default="qualified" attribute. Form. Default="unqualified"> <xs: element name="rooms"> <xs: complex. Type> <xs: sequence> <xs: element ref="room" max. Occurs="unbounded"/> </xs: sequence> </xs: complex. Type> </xs: element> <xs: element name="room"> <xs: complex. Type> <xs: sequence> <xs: element name="capacity" type="xs: decimal"/> <xs: element name="equiptment. List"/> <xs: element ref="features" min. Occurs="0" max. Occurs="1"/> </xs: sequence> <xs: attribute name="name" type="xs: string" use="required"/> </xs: complex. Type> </xs: element> <xs: element name="features"> <xs: complex. Type> <xs: sequence> <xs: element name="feature" type="xs: string" max. Occurs="unbounded"/> </xs: sequence> </xs: complex. Type> </xs: element>

Types Both elements and attributes have types, which are defined in the Schema. One

Types Both elements and attributes have types, which are defined in the Schema. One can reuse types by giving them names. OR <xsd: element name="Robot"> <xsd: complex. Type> <xsd: sequence> <xsd: element ref="Sensor_List" min. Occurs="0"/> <xsd: element ref="Specification_List" min. Occurs="0"/> <xsd: element ref="Note" min. Occurs="0"/> </xsd: sequence> </xsd: complex. Type> </xsd: element> <xsd: element name="Robot” type=“Robo. Type”> <xsd: complex. Type name="Robo. Type” > <xsd: sequence> <xsd: element ref="Sensor_List" min. Occurs="0"/> <xsd: element ref="Specification_List" min. Occurs="0"/> <xsd: element ref="Note" min. Occurs="0"/> </xsd: sequence> </xsd: complex. Type> </xsd: element>

Other XML Schema Features n n n Foreign key facility (uses Xpath) Rich datatype

Other XML Schema Features n n n Foreign key facility (uses Xpath) Rich datatype facility n Build up datatypes by inheritance n Don’t need to list all of the attributes (can say "these attributes plus others"). n Restrict strings using regular expressions Namespace aware. n Can restrict location of an element based on a namespaces

Restrictions

Restrictions

Datatype Restrictions n A DTD can only say that price can be any non-markup

Datatype Restrictions n A DTD can only say that price can be any non-markup text. Like this translated to Schemas <xsd: element name="zip" type="xsd: string"/> n But in Schema you can do better: <xsd: element name="zip" type="xsd: decimal"/> n Or even, make your own restrictions <xsd: simple. Type name="Zip. Plus 4"> <xsd: restriction base="xsd: string"> <xsd: length value="10"/> <xsd: pattern value="d{5}-d{4}"/> </xsd: restriction> </xsd: simple. Type> <xsd: element name="zip" type="Zip. Plus 4">

Restriction Ranges n The restrictions must be "derived" from a base type, so it's

Restriction Ranges n The restrictions must be "derived" from a base type, so it's object based <xs: element name="Life. Universe. And. Everything"> <xs: simple. Type> <xs: restriction base="xs: integer"> <xs: min. Inclusive value="42"/> <xs: max. Inclusive value="42"/> </xs: restriction> </xs: simple. Type> </xs: element> n n Preceding "derived" from "integer" Has 2 restrictions (called "facets") n n The first says that it must be greater than 41 The second says that it must be less than 43 XML file is "42" <Life. Universe. And. Everything xmlns: xsi="http: //www. w 3. org/2001/XMLSchema -instance" xsi: no. Namespace. Schema. Location="Life. Universe. Everything. xsd">42</Life Universe. And. Everything> n

Facet enumeration fraction. Digits length max. Exclusive max. Inclusive max. Length min. Exclusive min.

Facet enumeration fraction. Digits length max. Exclusive max. Inclusive max. Length min. Exclusive min. Inclusive min. Length pattern total. Digits white. Space Description Defines a list of acceptable values The maximum number of decimal places allowed. >=0 The exact number of characters or list items allowed. >=0 The upper bounds for numeric values (the value must be less than the value specified) The upper bounds for numeric values (the value must be less than or equal to the value specified) The maximum number of characters or list items allowed. >=0 The lower bounds for numeric values (the value must be greater than the value specified) The lower bounds for numeric values (the value must be greater than or equal to the value specified) The minimum number of characters or list items allowed >=0 The sequence of acceptable characters based on a regular expression The exact number of digits allowed. >0 Specifies how white space (line feeds, tabs, spaces, and

Enumeration Facet <xs: element name="Favorite. Color"> <xs: simple. Type> <xs: restriction base="xs: string"> <xs:

Enumeration Facet <xs: element name="Favorite. Color"> <xs: simple. Type> <xs: restriction base="xs: string"> <xs: enumeration value="red"/> <xs: enumeration value="no blue"/> <xs: enumeration value="aarrrrggghh!!"/> </xs: restriction> </xs: simple. Type> </xs: element>

Patterns (Regular Expressions) n One interesting facet is the pattern, which allows restrictions based

Patterns (Regular Expressions) n One interesting facet is the pattern, which allows restrictions based on a regular expression n This regular expression specifies a normal word of one or more characters: <xs: element name="Word"> <xs: simple. Type name="Word. Type"> <xs: restriction base="xs: string"> <xs: pattern value="[a-z. A-Z]+"/> </xs: restriction> </xs: simple. Type> </xs: element>

Patterns (Regular Expressions) n Individual characters may be repeated a specific number of times

Patterns (Regular Expressions) n Individual characters may be repeated a specific number of times in the regular expression. n The following regular expression restricts the string to exactly 8 alpha-numeric characters: <xs: element name="password"> <xs: simple. Type> <xs: restriction base="xs: string"> <xs: pattern value="[a-z. A-Z 0 -9]{8}"/> </xs: restriction> </xs: simple. Type> </xs: element>

Whitespace facet n n The "whitespace" facet controls how white space in the element

Whitespace facet n n The "whitespace" facet controls how white space in the element will be processed There are three possible values to the whitespace facet n n n "preserve" causes the processor to keep all whitespace as-is "replace" causes the processor to replace all whitespace characters (tabs, carriage returns, line feeds, spaces) with space characters "collapse" causes the processor to replace all strings of whitespace characters (tabs, carriage returns, line feeds, spaces) with a single space character <xs: simple. Type> <xs: restriction base="xs: string"> <xs: whitespace value="replace"/> </xs: restriction> </xs: simple. Type>

Types Both elements and attributes have types, which are defined in the Schema. One

Types Both elements and attributes have types, which are defined in the Schema. One can reuse types by giving them names. Addr. xsd: <xsd: element name="Address"> <xsd: complex. Type> <xsd: sequence> <xsd: element name="Street" type="xsd: string"/> <xsd: element name="Apartment" type="xsd: string"/> <xsd: element name="Zip" type="xsd: string"/> </xsd: sequence> </xsd: complex. Type> </xsd: element> OR <xsd: complex. Type name="Addr. Type"> <xsd: sequence> <xsd: element name="Street" type="xsd: string"/> <xsd: element name="Apartment" type="xsd: string"/> <xsd: element name="Zip" type="xsd: string"/> </xsd: sequence> </xsd: complex. Type> <xsd: element name=“Ship. Address" type="Addr. Type"/> <xsd: element name=“Bill. Address" type="Addr. Type"/>

Types n The usage in the XML file is identical: <? xml version="1. 0"

Types n The usage in the XML file is identical: <? xml version="1. 0" encoding="UTF-8"? > <Purchase. Order xmlns: xsi="http: //www. w 3. org/2001/XMLSchema-instance" xsi: no. Namespace. Schema. Location="Address-With. Type. Name. xsd"> <Bill. Address> <Street>1108 E. 58 th St. </Street> <Apartment>Ryerson 155</Apartment> <Zip>60637</Zip> </Bill. Address> <Ship. Address> <Street>1108 E. 58 th St. </Street> <Apartment>Ryerson 155</Apartment> <Zip>60637</Zip> </Ship. Address> </Purchase. Order>

Type Extensions n A third way of creating a complex type is to extend

Type Extensions n A third way of creating a complex type is to extend another complex type (like OO inheritance) <xs: element name="Employee" type="Person. Info. Type"/> <xs: complex. Type name="Person. Name. Type"> <xs: sequence> <xs: element name="First. Name" type="xs: string"/> <xs: element name="Last. Name" type="xs: string"/> </xs: sequence> </xs: complex. Type> <xs: complex. Type name="Person. Info. Type"> <xs: complex. Content> <xs: extension base="Person. Name. Type"> <xs: sequence> <xs: element name="Address" type="xs: string"/> <xs: element name="City" type="xs: string"/> <xs: element name="Country" type="xs: string"/> </xs: sequence> </xs: extension> </xs: complex. Content> </xs: complex. Type>

Type Extensions (use) n To use a type that is an extension of another,

Type Extensions (use) n To use a type that is an extension of another, it is as though it were all defined in a single type <Employee xmlns: xsi="http: //www. w 3. org/2001/XMLSchema-instance" xsi: no. Namespace. Schema. Location="Type. Extension. xsd"> <First. Name>King</First. Name> <Last. Name>Arthur</Last. Name> <Address>Round Table</Address> <City>Camelot</City> <Country>England</Country> </Employee>

Simple Content in Complex Type n n If a type contains only simple content

Simple Content in Complex Type n n If a type contains only simple content (text and attributes), a <simple. Content> element can be put inside the <complex. Type> <simple. Content> must have either a <extension> or a <restriction> This example is from the (Bridge of Death) Episode Dialog: <xs: element name="dialog"> <xs: complex. Type> <xs: simple. Content> <xs: extension base="xs: string"> <xs: attribute name="speaker" type="xs: string" use="required"/> </xs: extension> </xs: simple. Content> </xs: complex. Type> </xs: element> n

Model Groups n Model Groups are used to define an element that has n

Model Groups n Model Groups are used to define an element that has n n n mixed content (elements and text mixed) element content Model Groups can be n all n n choice n n the elements specified must all be there, but in any order any of the elements specified may or may not be there sequence n all of the elements specified must appear in the specified order

"All" Model Group The following schema specifies 3 elements and mixed content <xs: element

"All" Model Group The following schema specifies 3 elements and mixed content <xs: element name="Book. Cover"> <xs: complex. Type mixed="true"> <xs: all min. Occurs="0" max. Occurs="1"> <xs: element name="Book. Title" type="xs: string"/> <xs: element name="Author" type="xs: string"/> <xs: element name="Publisher" type="xs: string"/> </xs: all> </xs: complex. Type> </xs: element> n The following XML file is valid in the above schema <Book. Cover xmlns: xsi="http: //www. w 3. org/2001/XMLSchema-instance" xsi: no. Namespace. Schema. Location="All. Model. Group. xsd"> n Title: <Book. Title>The Holy Grail</Book. Title> Published: <Publisher>Moose</Publisher> Author: <Author>Monty Python</Author> </Book. Cover>

 Attributes The attribute declaration is part of the type of the element. <xs:

Attributes The attribute declaration is part of the type of the element. <xs: element name="dialog"> <xs: complex. Type> <xs: simple. Content> <xs: extension base="xs: string"> <xs: attribute name="speaker" type="xs: string" use="required"/> </xs: extension> </xs: simple. Content> </xs: complex. Type> </xs: element> …

Attributes <xsd: element name="cartoon"> <xsd: complex. Type> <xsd: sequence> <xsd: element ref="character" min. Occurs="0"

Attributes <xsd: element name="cartoon"> <xsd: complex. Type> <xsd: sequence> <xsd: element ref="character" min. Occurs="0" max. Occurs="unbounded"/> </xsd: sequence> <xsd: attribute name="name" type="xsd: string" use="required"/> <xsd: attribute name="genre" type="xsd: string" use="required"/> <xsd: attribute name="syndicated" use="required"> <xsd: simple. Type> <xsd: restriction base="xsd: NMTOKEN"> <xsd: enumeration value="yes"/> <xsd: enumeration value="no"/> </xsd: restriction> </xsd: simple. Type> </xsd: attribute> </xsd: complex. Type> </xsd: element> If an attribute type is more complicated than a basic type, then we spell out the type in a type declaration.

Optional and Required Attributes n All attributes are optional by default. To explicitly specify

Optional and Required Attributes n All attributes are optional by default. To explicitly specify that the attribute is optional, use the "use" attribute: <xs: attribute name="speaker" type="xs: string" use="optional"/> n To make an attribute required: <xs: attribute name="speaker" type="xs: string" use="required"/>

Other XML Schema Features n n n Foreign key facility (uses Xpath) Rich datatype

Other XML Schema Features n n n Foreign key facility (uses Xpath) Rich datatype facility n Build up datatypes by inheritance n Don’t need to list all of the attributes (can say “these attributes plus others). n Restrict strings using regular expressions Namespace aware. n Can restrict location of an element based on a namespaces

XML Schema Status n Became a W 3 C recommendation Spring 2001 n n

XML Schema Status n Became a W 3 C recommendation Spring 2001 n n World domination expected imminently. Supported in Xalan. Supported in XML spy and other editor/validators. On the other hand: n n More complex than DTDs. Ultra verbose.

Validating a Schema n By using Xeena or XMLspy or XML Notepad. n n

Validating a Schema n By using Xeena or XMLspy or XML Notepad. n n When publishing hand-written XML docs, this is the way to go. By using a Java program that performs validation. n When validating on-the-fly, must do it this way

Some guidelines for Schema design

Some guidelines for Schema design

Designing a Schema n n Analogous to database schema design --- look for intuitive

Designing a Schema n n Analogous to database schema design --- look for intuitive names Can start with an E-R diagram, and then convert n n Attributes to Attributes Subobjects to Subelements Relationships to IDREFS Normalization? Still makes sense to avoid repetition whenever possible– n n n If you have an Enrolment document, only list Ids of students, not their names. Store names in a separate document Leave it to tools to connect them

Designing a Schema (cont. ) n Difficulties: n n Many more degrees of freedom

Designing a Schema (cont. ) n Difficulties: n n Many more degrees of freedom than with database schemas: e. g. one can associate information with something by including it as an attribute or a subelement. <ADDRESS NAME=“Martin Sheen”, Street=“ 1222 Alameda Drive” , City=“Carmel”, State=“CA”, ZIP=“ 40145”> <ADDRESS> <NAME> Martin Sheen </NAME> … <ZIP> 4145 </ZIP> </ADDRESS> n n ELEMENTS are more extensible – use when there is a possibility that more substructure will be added. ATTRIBUTES are easier to search on.

“Rules” for Designing a Schema Never leave structure out. The following is definitely a

“Rules” for Designing a Schema Never leave structure out. The following is definitely a bad idea: n <ADDRESS> Martin Sheen 1222 Alameda Drive, Carmel, CA 40145 </ADDRESS> n Better would be: n <ADDRESS first. Name=“Martin” lastname=“Sheen” streen. Num=“ 1222” streen. Name=“Alameda Drive” city=“Carmel” state=“CA” zip=“ 40145” /> n Or: <ADDRESS> <name> n <first>Martin</first><last>Sheen</last> </name> <street> <num>1222</num><name>Alameda Drive</name> </street> <city>Carmel</city> <state>CA</state><zip>40145</zip> </ADDRESS>

More“Rules” for Designing a Schema n When to use Elements (instead of attributes) n

More“Rules” for Designing a Schema n When to use Elements (instead of attributes) n Do not put large text blocks inside an attribute (Bad Idea) <book type=“memoir” content=“Bravely bold Sir Robin rode forth from Camelot. He was not afraid to die, O brave Sir Robin. He was not at all afraid to be killed in nasty ways, Brave, brave, brave Sir Robin! n He was not in the least bit scared to be mashed into a pulp, Or to have his eyes gouged out and his elbows broken, To have his kneecaps split and his body burned away And his limbs all hacked and mangled, brave Sir Robin! His head smashed in and his heart cut out And his liver removed and his bowels unplugged…”> n Elements are more flexible, so use an Element if you think you might have to add more substructure later on.

More “Rules” for Designing Schemas n More on when to use Elements (instead of

More “Rules” for Designing Schemas n More on when to use Elements (instead of Attributes) n Use an embedded element when the information you are recording is a constituent part of the parent element n n n one's head and one's height are both inherent to a human being, you can't be a conventionally structured human being without having a head and having a height One's head is a constituent part and one's height isn't -- you can cut off my head, but not my height use embedded elements for complex structure validation (obvious) use embedded elements when you need to show order (attributes are not ordered)

More “Rules” for Designing Schemas n When to use Attributes instead of Elements n

More “Rules” for Designing Schemas n When to use Attributes instead of Elements n n use an attribute when the information is inherent to the parent but not a constituent part (height instead of head) use attributes to stress the one-to-one relationship among pieces of information n n to stress that the element represents a tuple of information dangerous rule, though n n Leads to the extreme formulation that a <chapter> element can have a TITLE= attribute And then to the conclusion that it really ought to have a CONTENT= attribute too Then you find yourself writing the entire document as an empty element with an attribute value as long as the Quest for the Holy Grail use attributes for simple datatype validation (obviously)

Bookings XML Schema <? xml version="1. 0" encoding="UTF-8"? > <xs: schema xmlns: xs="http: //www.

Bookings XML Schema <? xml version="1. 0" encoding="UTF-8"? > <xs: schema xmlns: xs="http: //www. w 3. org/2001/XMLSchema" element. Form. Default="qualified" attribute. Form. Default="unqualified"> <xs: element name="bookings"> <xs: complex. Type> <xs: sequence> <xs: element ref="last. Updated" max. Occurs="1" min. Occurs="0"/> <xs: element ref="meeting. Date" max. Occurs="unbounded"/> </xs: sequence> </xs: complex. Type> </xs: element> <xs: element name="year" type="xs: integer"/> <xs: element name="month" type="xs: string"/> <xs: element name="day" type="xs: integer"/> Note that there are four global types in this document!

Bookings, cont. <xs: element name="meeting. Date"> <xs: complex. Type> <xs: sequence> <xs: element ref="year"/>

Bookings, cont. <xs: element name="meeting. Date"> <xs: complex. Type> <xs: sequence> <xs: element ref="year"/> <xs: element ref="month"/> <xs: element ref="day"/> <xs: element ref="meeting" max. Occurs="unbounded" min. Occurs="0"/> </xs: sequence> </xs: complex. Type> </xs: element> <xs: element name="last. Updated"> <xs: complex. Type> <xs: attribute name="date" type="xs: string"/> <xs: attribute name="time" type="xs: string"/> </xs: complex. Type> </xs: element>

Bookings, cont. <xs: element name="meeting"> <xs: complex. Type> <xs: sequence> <xs: element name="meeting. Name"

Bookings, cont. <xs: element name="meeting"> <xs: complex. Type> <xs: sequence> <xs: element name="meeting. Name" max. Occurs="1" min. Occurs="1" type="xs: string"/> <xs: element name="room. Name" max. Occurs="1" min. Occurs="1" type="xs: string"/> </xs: sequence> </xs: complex. Type> </xs: element> </xs: schema>

An Example Bookings Document • Reverse engineer a reasonable schema for the following sample

An Example Bookings Document • Reverse engineer a reasonable schema for the following sample xml file <? xml version="1. 0" encoding="UTF-8"? > <bookings xmlns: xsi="http: //www. w 3. org/2001/XMLSchema-instance" xsi: no. Namespace. Schema. Location=". . /schemas/Bookings. xsd"> <meeting. Date> <year>2003</year> <month>April</month> <day>1</day> <meeting. Name>Democratic Party</meeting. Name> <room. Name>Green Room</room. Name> </meeting> <meeting. Name>Republican Party</meeting. Name> <room. Name>Red Room</room. Name> </meeting. Date> </bookings>