56
XML Schema – Part 1 1.Introduction to XML- Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

  • View
    237

  • Download
    1

Embed Size (px)

Citation preview

Page 1: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

XML Schema – Part 1

1. Introduction to XML-Schema

2. Schema basics

3. Mechanisms (strategies) for Designing Schema

4. Creating your own Datatypes

Page 2: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Simple XML?<xml version="1.0>?"

•<purchaseOrder orderDate="1999-10-20">• <shipTo country="US">• <name>Alice Smith</name>• <street>123 Maple Street</street>• <city>Mill Valley</city>• <state>CA</state>• <zip>90952</zip>• </shipTo>

<items>• <item partNum="872-AA">• <productName>Lawnmower</productName>• <quantity>1</quantity>• <USPrice>148.95</USPrice>• <comment>Confirm this is electric</comment>• </item>• <item partNum="926-AA">• <productName>Baby Monitor</productName>• <quantity>1</quantity>• <USPrice>39.98</USPrice>• <shipDate>1999-05-21</shipDate>• </item>• </items>•</purchaseOrder>

Page 3: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

XML1. XML is for structuring data

•spreadsheets, address books, configuration parameters, financial transactions, and technical drawings.

•XML is text format for representing structured data. XML makes it easy for a computer to generate data, read data, and ensure that the data structure is unambiguous. XML is extensible, platform-independent, and it supports internationalization and localization.

2. XML looks a bit like HTML

• Like HTML, XML makes use of tags (words bracketed by '<' and '>') and attributes (of the form name="value"). While HTML specifies what each tag and attribute means, and often how the text between them will look in a browser, XML uses the tags only to delimit pieces of data, and leaves the interpretation of the data completely to the application that reads it. if you see "<p>" in an XML file, do not assume it is a paragraph. Depending on the context, it may be a price, a parameter, a person, a p... (and who says it has to be a word with a "p"?).

• XML can keep data separated from your HTML

Page 4: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

3. XML is a group of technologies

•XML 1.0 is the specification that defines what "tags" and "attributes" are. Beyond XML 1.0, "the XML family" is a growing set of modules that offer useful services to accomplish important and frequently demanded tasks. XPointer and XFragments are syntaxes in development for pointing to parts of an XML document. An XPointer is a bit like a URL, but instead of pointing to documents on the Web, it points to pieces of data inside an XML file. XSLT, a transformation language used for rearranging, adding and deleting tags and attributes. The DOM is a standard set of function calls for manipulating XML (and HTML) files from a programming language. XML Schemas help developers to precisely define the structures of their own XML-based formats. There are several more modules and tools available or under development. Keep an eye on W3C's technical reports page.

Page 5: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Purpose of XML Schemas (and DTDs)

• Specify:– the structure of instance documents

• "this element contains these elements, which contains these other elements, etc"

– the datatype of each element/attribute• "this element shall hold an integer with the range 0

to 12,000" (DTDs don't do too well with specifying datatypes like this)

Page 6: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

What is a Schema?• A piece of information marked up by presence of tags is called element.

• Elements may further be enriched by attaching name-value pairs called attributes.

• Like Data Type Definitions (DTDs)

• Define the document's structure.

•Elements and attributes definition•Empty or text content elements. •Default values for attributes and elements.

• More powerful and flexible than DTDs.• XML syntax.

• Agreed upon Schema• Exchanging XML data. • Verify the received data against schema. • Valid and well-formed.

Page 7: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

?>xml version="1.0" encoding="utf-8 <?">xs:schema xmlns:xs

="http://www.w3.org/2001/XMLSchema<"

>xs:element name="book <"> xs:complexType <

<xs:sequence> <xs:element name="title“ type="xs:string"/> <xs:element name="author“ type="xs:string"/> <xs:element name="character“ maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="name"type="xs:string"/> <xs:element name=“dob" type="xs:date“/> </xs:sequence>

/> xs:complexType < </xs:element> </xs:sequence> <xs:attribute name="isbn" type="xs:string"/>

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

/>xs:schema> Example 1

Page 8: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Structure of the Data

BOOK

titleauthor character

name dob

isbn

We want to define this structure in the schema

>book isbn="0836217462"> </book<

>title> Pets </title< >author>M.Cat</author<

>name>Snoopy</name< >dob>1966</dob<

>character></character<

Page 9: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

<?xml version="1.0“ encoding="UTF-8"?>

<book isbn="0836217462"> <title> Pets </title>

<author>M. Cat</author> <character> <name>Snoopy</name> <dob>1950</dob> </character> <character> <name>Patty</name> <dob>1966</dob> </character> </book>

?>xml version="1.0" encoding="utf-8 <?">xs:schema xmlns:xs

="http://www.w3.org/2001/XMLSchema<"

>xs:element name="book <"> xs:complexType <

<xs:sequence> <xs:element name="title“type="xs:string"/> <xs:element name="author“type="xs:string"/> <xs:element name="character"

maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="name"type="xs:string"/> <xs:element name=“dob" type="xs:date"/> </xs:sequence>

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

</xs:sequence> > xs:attribute name="isbn"

type="xs:string </"/> xs:complexType <

/>xs:element</>xs:schema <

XML Schema

Example 1

Page 10: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Explanations:• Type (xsd:string) is prefixed by the namespace prefix

associated with XML Schema, indicating a predefined XML Schema datatype:

<xsd:element name="name" type="xsd:string"/>

• Specify both minOccurs and maxOccurs . • unbounded value , default value (one).• Only in local definition• Facets

<xsd:element name="character" minOccurs="0" maxOccurs="unbounded”> …/…

</xsd:element>

• Attributes after element declarations.• Compositors:

• Sequence (ordered sequence)• All (no order but all)• Choice

Page 11: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Simple/Complex Data type• SimpleType is reserved for data types holding only

values and no attribute or element sub-nodes.

>xsd:element name="title" type="xsd:string"/< >xsd:element name="author" type="xsd:string"/<

• ComplexType - data types which aren’t simple(Book element has attributes and children elements)

<xs:element name="character" maxOccurs="unbounded">

<xs:complexType> <xsd:sequence>

<xsd:element name="name" type="xsd:string"/>

<xsd:element name=“dob" type="xsd:date“/> </xsd:sequence>

<xsd:attribute name=“…" type=“..” /> </xsd:complexType>

</xsd:element>

Page 12: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Compositors: Using <sequence> and <choice<<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.binary.org" xmlns="http://www.binary.org" elementFormDefault="qualified"> <xsd:element name="life"> <xsd:complexType> <xsd:sequence minOccurs="0" maxOccurs="unbounded"> <xsd:sequence minOccurs="0" maxOccurs="unbounded"> <xsd:element name="work" type="xsd:string"/> <xsd:element name="eat" type="xsd:string"/> </xsd: sequence> <xsd:choice> <xsd:element name="work" type="xsd:string"/> <xsd:element name="play" type="xsd:string"/> </xsd:choice> <xsd:element name="sleep" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema>

Page 13: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Compositors: Expressing Any Order<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:all> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:all> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> …….

Problem: create an element, Book, which contains Author, Title, Date, ISBN, and Publisher, in any order (Note: this is very difficult and ugly with DTDs).

Page 14: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Namespaces• A namespace is a collection of names used as element or attribute names in an XML document.

• to qualify element names to make them unique• to avoid conflicts between elements with the same name.

• xmlns - keyword for a namespace declaration..• The idea is that when you are dealing with XML documents from ten different (external) sources, name collisions can occur. If you use namespaces, you are distinguishing one element from another based on the namespace prefix. myNameSpace:Employee is not the same as yourNameSpace:Employee. When you declare a namespace you also give it an unique URI ( Universal Resource). •explicit or default declaration.

Page 15: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Explicit and Default namespace declaration• Explicit declaration (bk – qualifier)<BOOKS> <bk:BOOK xmlns:bk="http://www.books.org"

   xmlns:money="http://www.money.org">  <bk:TITLE>Tourist guide</bk:TITLE>     <bk:PRICE money:currency="US Dollar"> 22.95 </bk:PRICE> </bk:BOOK></BOOKS>• Default<BOOKS> <BOOK xmlns="http://www.books.org">   <TITLE> Tourist guide </TITLE>   <PRICE currency="US Dollar">22.95</PRIC>

/> BOOK</>BOOKS<

•Identified by a Universal Resource Identifier (URI) or by Uniform Resource Locator (URL) •It doesn't matter what the URI points to. URIs are used because they are globally unique across the Internet.

Page 16: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

?>xml version="1.0" encoding="utf-8 <?">xs:schema xmlns:xs ="http://www.w3.org/2001/XMLSchema<"

<xs:element name="book"> > xs:complexType <

<xs:sequence> <xs:element name="title“ type="xs:string"/> <xs:element name="author“ type="xs:string"/> <xs:element name="character“ maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="name"type="xs:string"/> <xs:element name="since" type="xs:date"/> </xs:sequence>

/> xs:complexType < </xs:element> </xs:sequence> <xs:attribute name="isbn" type="xs:string"/>

/> xs:complexType </> xs:element</>xs:schema <

The elements anddatatypes thatare used to constructschemas - schema - element - complexType - sequence - stringcome from the http://…/XMLSchemanamespace

Example 1Book.xsd

Page 17: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

enc

?>xml version="1.0" encoding="utf-8<?" >xs:schema xmlns:xs ="http://www.w3.org/2001/XMLSchema<"

targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified">

>xs:element name="book <"> xs:complexType <

<xs:sequence> <xs:element name="title“ type="xs:string"/> <xs:element name="author“ type="xs:string"/> <xs:element name="character“ maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="name"type="xs:string"/> <xs:element name="since" type="xs:date"/> </xs:sequence>

/> xs:complexType < </xs:element> </xs:sequence> <xs:attribute name="isbn" type="xs:string"/>

/> xs:complexType </> xs:element</>xs:schema<

The default namespace ishttp://www.books.org which is the targetNamespace!

Example 1

“qualified“ This is a directive to any instance documents which conform to this schema: Any elements used by the instance document which were declared in this schema must be namespace qualified.

The Book in what namespace? Since thereis no namespace qualifier it is referencing the Book element in the default namespace, which is the targetNamespace! Thus, this is a reference to the Book element declaration in this schema.

Page 18: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

XML Schema Namespace

element

complexType

schema

sequence

http://www.w3.org/2001/XMLSchema

string

integer

boolean

)schema-for-schemas(

Page 19: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Book Namespace(target namespace)

ISBN

Book

Character

Title

Author

name

http://www.books.org

dob

Page 20: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

<?xml version="1.0"?><Book xmlns ="http://www.books.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.books.org Book.xsd">

… <Title>My Life and Times</Title>

<Author>Paul McCartney</Author> ...

</Book>1. First, using a default namespace declaration, tell the schema-validator that all of the elementsused in this instance document come from the http://www.books.org namespace.2. Second, with schemaLocation tell the schema-validator that the http://www.books.org namespace is defined by Book.xsd (i.e., schemaLocation contains a pair of values).3. Third, tell the schema-validator that the schemaLocation attribute we are using is the one in the XML Schema-instance namespace.

1

2

3

XMLSchema and XML instance document

Page 21: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Referencing a schema in an XML instance document

Book.xml Book.xsd

targetNamespace="http://www.books.org"

schemaLocation="http://www.books.org Book.xsd"

- defines elements in namespace http://www.books.org

- uses elements from namespace http://www.books.org

A schema defines a new vocabulary. Instance documents use that new vocabulary.

Page 22: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Note multiple levels of checking

Book.xml Book.xsd XMLSchema.xsd(schema-for-schemas)

Validate that the xml documentconforms to the rules describedin Book.xsd

Validate that Book.xsd is a validschema document, i.e., it conformsto the rules described in theschema-for-schemas

Page 23: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Russian Doll Design?>xml version="1.0 <? “

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

>xs:element name="book <"> xs:complexType <

<xs:sequence> <xs:element name="title“ type="xs:string"/> <xs:element name="author“ type="xs:string"/> <xs:element name="character“ maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="name"type="xs:string"/> <xs:element name=“dob" type="xs:date”/> </xs:sequence>

/> xs:complexType < </xs:element> </xs:sequence> <xs:attribute name="isbn" type="xs:string"/>

/> xs:complexType </> xs:element</>xs:schema<

Example 1

Page 24: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Flat Catalog<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name=“author" type="xs:string"/> <xs:element name="title" type="xs:string"/>

<xs:element name="name" type="xs:string"/> <xs:element name=“dob" type="xs:date"/>

<xs:attribute name="isbn" type="xs:string"/> <!-- definition of complex type elements --> <xs:element name="character">

<xs:complexType> <xs:sequence>

<xs:element ref="name"/> <xs:element ref=“dob"/>

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

</xs:element> <xs:element name="book">

<xs:complexType> <xs:sequence>

<xs:element ref="title"/> <xs:element ref="character"

minOccurs="0” maxOccurs="unbounded</" </xs:sequence> <xs:attribute ref="isbn"/>

</xs:complexType></xs:element> </xs:schema>

Example 2

First definition of the simple types

These are global definitions

Next definition of attributes

Next, definition of complex types

The definition of the cardinality is done when the elements are referenced

Page 25: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Summary Mechanisms of definitions

• Russian Doll Design:• Tight structure• Multiple occurrences of a same element name with different definitions.• Depth in the embedded definitions• Hardly readable and difficult to maintain when documents are complex.

• Flat Catalog:• Catalog of all the elements. • references to element and attribute definitions that need to be within the scope of the referencer.• Using a reference to an element or an attribute is somewhat comparable to cloning an object. The element or attribute is defined first, and it can be duplicated at another place in the document structure by the reference mechanism, in the same way an object can be cloned. The two elements (or attributes) are then two instances of the same class.

Page 26: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Anonymous types (no name)

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema>

Russian Doll Design

Example 3

Page 27: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Named Types<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" type="BookPublication" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:complexType name="BookPublication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> …/….. <xsd:element name=“Magazine" type="BookPublication" maxOccurs="unbounded"/>…/..</xsd:schema>

Named type

The advantage ofsplitting out Book'selement declarationsand wrapping themin a named type isthat now this typecan be reused byother elements.

Example 4

Page 28: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Please note that:

<xsd:element name="A"> <xsd:complexType> <xsd:sequence> <xsd:element name="B" …/> <xsd:element name="C" …/> </xsd:sequence> </xsd:complexType></xsd:element>

is equivalent to:

<xsd:element name="A" type="foo"/>

<xsd:complexType name="foo"> <xsd:sequence> <xsd:element name="B" …/> <xsd:element name="C" …/> </xsd:sequence></xsd:complexType>

Element A instantiates complexType foo.

Element A has the complexType definitioninlined in the elementdeclaration.

Page 29: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Summary Definition Mechanisms• Russian Doll Design:

• Tightly follows the structure • Define each element and attribute within its context and to allow multiple occurrences of a same element name to carry different definitions.• Hardly readable and difficult to maintain when documents are complex.

• Flat Catalog:• Catalog of all the elements available in the instance document and, for each of them, lists of child elements and attributes. • Use references to element and attribute definitions that need to be within the scope of the referencer.• Somewhat comparable to cloning an object. The element or attribute is defined first, and it can be duplicated at another place in the document structure by the reference mechanism, in the same way an object can be cloned. The two elements (or attributes) are then two instances of the same class.

• Named Types• Give a name to the simpleType and complexType elements.• Comparable to defining a class and using it to create an object

Page 30: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

• Capture the semantics in the XML Schema

• The <annotation> element is used for documenting the schema, both for humans and for programs.

– Use <documentation> for providing a comment to humans

– Use <appinfo> for providing a comment to programs

• The content is any well-formed XML

• Note that annotations have no effect on schema validation

Annotating Schemas

Page 31: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Annotating Schemas<xsd:annotation> <xsd:documentation> The following constraint is not expressible with XML Schema: The value of element A should be greater than the value of element B. So, we need to use a separate tool (e.g., Schematron) to check this constraint. We will express this constraint in the appinfo section (below).</xsd:documentation> <xsd:appinfo> <assert test="A &gt; B">A should be greater than

B</assert> </xsd:appinfo><xsd:/annotation>

Page 32: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Code to check the structure and content(datatype) of the data

Code to actuallydo the work

"In a typical program, up to 60% of the code is spent checking the data!"

Save time and money using XML Schemas

Continued -->

Page 33: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Code to check the structure and content

of the data

Code to actuallydo the work

If your data is structured asXML, and there is a schema,then you can hand the data-checking task off to a schema validator.

Thus, your code is reducedby up to 60%!!!

Big $$ savings!

Save time and money using XML Schemas (cont.)

Page 34: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Classic use of XML Schemas(Trading Partners )

Supplier Consumer

XML data

SchemaValidator

XMLSchema

Softwareto Process

D.

“D. isokay" D

(Schema at third-party, neutral web site)

Page 35: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

XML Schema --> GUI

SchemaGUI

Builder HTMLSupplier

WebServer

Page 36: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

XML Schema --> Smart Editor

Schema Smart Editor (e.g., XML Spy)

Helps you build yourinstance documents.For example, it popsup a menu showing you what is valid next.It knows this by lookingat the XML Schema!

Page 37: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Element Substitution• We can define a group of substitutable elements (called a

substitutionGroup) by declaring an element (called the head) and then declaring other elements which state that they are substitutable for the head element.

<xsd:element name="subway" type="xsd:string"/><xsd:element name="T" substitutionGroup="subway"

type="xsd:string"/>

subway is the head elementT is substitutable for subway

Page 38: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

<xsd:element name="subway" type="xsd:string"/> <xsd:element name="T" substitutionGroup="subway" type="xsd:string"/> <xsd:element name="transportation"> <xsd:complexType> <xsd:sequence> <xsd:element ref="subway"/> </xsd:sequence> </xsd:complexType> </xsd:element>

<transportation> <subway>Red Line</subway> </transportation>

Instance doc:

<transportation> <T>Red Line</T> </transportation>

Alternative instance doc(substitute T for subway):

This example shows the <subway> element being substituted with the <T> element.

Schema

Page 39: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

<xsd:element name="subway" type="xsd:string"/><xsd:element name="metro" substitutionGroup="subway" type="xsd:string"/> <xsd:complexType name="transport"> <xsd:sequence> <xsd:element ref="subway"/> </xsd:sequence> </xsd:complexType><xsd:element name="transportation" type="transport"/><xsd:element name="transporte" substitutionGroup="transportation"/>

<transportation> <subway>Red Line</subway> </transportation>

Schema

Instance doc:

<transporte> <metro>Linea Roja</metro> </transporte>

Alternative instance doc(customized for Spanish clients):

Substitution Groups (Example)

Remarks : Transitive A, B, C Not Symmetric

Blocking substitution: <xsd:element name="…" type="…" block="substitution"/>

Page 40: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Creating your own Datatypes

• Simple Types Derivationfor example, you want to create your “special” string which will be at length 10 and etc.

• Complex Types Derivation

Page 41: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

• xs:restriction elements. •The different kind of restrictions that can be applied on a datatype are called facets.

• Union of datatypes.

• White space separated lists.

Creating your own Datatypes(Simple datatypes)

Page 42: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

<xsd:simpleType name="TelephoneNumber“ type=“xsd:string”/><xsd:simpleType name="TelephoneNumber"> <xsd:restriction base="xsd:string"> <xsd:length value=“10"/> <xsd:pattern value="\d{2}-\d{7}"/> </xsd:restriction></xsd:simpleType>

1. This creates a new datatype called 'TelephoneNumber'. 2. Elements of this type can hold string values, 3. But the string length must be exactly 10 characters long and 4. The string must follow the pattern: dd-ddddddd, where 'd' represents a 'digit'. (Obviously, in this example the regular expression makes the length facet redundant.) patterns are specified using Regular Expressions

5. In general we use: restriction, facet, value.6. Restriction with Complex Type.

1

2

3

4

Creating your own Datatypes

Page 43: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

An element declared to be of type TelephoneNumbermust be a string of length=10 and the string must follow the pattern: 2 digits, dash, 7 digits.

<xsd:simpleType name="shape"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="circle"/> <xsd:enumeration value="triangle"/> <xsd:enumeration value="square"/> </xsd:restriction></xsd:simpleType>

<xsd:simpleType name="TelephoneNumber"> <xsd:restriction base="xsd:string"> <xsd:length value=“10"/> <xsd:pattern value="\d{2}-\d{7}"/> </xsd:restriction></xsd:simpleType>

An element declared to be of type shape must be a string with a value of either circle, or triangle, or square.

Multiple Facets - "and" them together, or "or" them together?

Page 44: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

General Form of Creating a Simple Datatype by Specifying Facet Values

<xsd:simpleType name= "name"> <xsd:restriction base= "xsd:source"> <xsd:facet value= "value"/> <xsd:facet value= "value"/> … </xsd:restriction></xsd:simpleType>

Facets: - length - minlength - maxlength - pattern - enumeration - minInclusive - maxInclusive - minExclusive - maxExclusive ...

Sources: - string - boolean - number - float - double - duration - dateTime - time ...

The different kind of restrictions that can be applied on a datatype are called facets

Page 45: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Creating your own Datatypes

• A new datatype can be defined from an existing datatype (called the "base" type) by specifying values for one or more of the optional facets for the base type.

• Example. The string primitive datatype has six optional facets:– length

– minLength

– maxLength

– pattern

– enumeration

– whitespace (legal values: preserve, replace, collapse)

Page 46: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Creating a simpleType from another simpleType

• Thus far we have created a simpleType using one of the built-in datatypes as our base type.

• However, we can create a simpleType that uses another simpleType as the base.

Page 47: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Example: creating simpleTypesfrom another simpleTypes

1. simpleType that uses a built-in base type:

<xsd:simpleType name= “VeryBigInteger"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="-1290"/> <xsd:maxInclusive value="29035"/> </xsd:restriction></xsd:simpleType>

2. simpleType that uses another simpleType as the base type:

<xsd:simpleType name= " MyInteger "> <xsd:restriction base=“VeryBigInteger "> <xsd:minInclusive value="0"/> <xsd:maxInclusive value="120"/> </xsd:restriction></xsd:simpleType>

Page 48: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Creating Simple Datatypes(list)

>xs:simpleType name="TelephoneBook<" <xs:list itemType=“TelephoneNumber"/>

</xs:simpleType>

< TelephoneBook >02-1234567 03-9876543</ TelephoneBook >

• xs:list defines a whitespace-separated list of values.• “TelephoneNumber“ – from the previous example• You cannot create list types from existing list types, nor from complex types• Facets can be applied to list types ,such as: length, minLength, maxLength• It allows <TelephoneBook> to contain an arbitrarily long list of TelephoneNumbers

Page 49: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Creating simpleType - Union <xsd:simpleType name="name"> <xsd:union memberTypes="space delimited simpleTypes"/></xsd:simpleType> <xsd:simpleType name="TomsFamily"> .../… </xsd:simpleType> <xsd:simpleType name="RogersFamily">

../… </xsd:simpleType> <xsd:simpleType name="CostelloFamily"> <xsd:union memberTypes="TomsFamily RogersFamily "/> </xsd:simpleType>

Page 50: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Creating simpleType - UnionAlternatively,

<xsd:simpleType name="name"> <xsd:union> <xsd:simpleType> … </xsd:simpleType> <xsd:simpleType> … </xsd:simpleType> … </xsd:union> </xsd:simpleType>

Page 51: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Creating Simple Datatypes (union)>xs:simpleType name="isbnType<"

<xs:union> <xs:simpleType>

<xs:restriction base="xs:string"> <xs:pattern value="[0-9]{10}"/>

</xs:restriction> </xs:simpleType>

<xs:simpleType> <xs:restriction base="xs:NMTOKEN">

<xs:enumeration value="TBD"/> <xs:enumeration value="NA"/>

</xs:restriction> </xs:simpleType>

/>xs:union< />xs:simpleType<

NMTOKEN simple type (like string and etc.)used to define only attributes US, BrésilNow isbnType may receive the union of simple types NMTOKEN or string

Page 52: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Complex Datatype Derivation• We can do a form of subclassing Complex

Type definitions: "derived types“.– derive by restriction: create a type which is a

subset of the base type. There are two ways to subset the elements:

• redefine a base type element to have a restricted range of values, or

• redefine a base type element to have a more restricted number of occurrences.

– derive by extension: extend the parent complexType with more elements

Page 53: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

<xsd:complexType name="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author"type="xsd:string"maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence></xsd:complexType ><xsd:complexType name="BookPublication"> <xsd:complexContent> <xsd:extension base="Publication"> <xsd:sequence> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent></xsd:complexType >

<xsd:element name="Book" type="BookPublication" maxOccurs="unbounded"/>

Page 54: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Title

AuthorDatePublication

ISBN

Publisher BookPublication

Page 55: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Derive by Restriction<xsd:complexType name="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence></xsd:complexType><xsd:complexType name= "SingleAuthorPublication"> <xsd:complexContent> <xsd:restriction base="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:gYear"/> </xsd:sequence> </xsd:restriction> </xsd:complexContent></xsd:complexType>

Elements of type SingleAuthorPublication will have 3 child elements - Title, Author, and Date.However, there must be exactly one Author element.Note that in the restriction type you must repeat all the declarations from the base type (except whenthe base type has an element with minOccurs="0" and the subtype wishes to delete it. ).

Page 56: XML Schema – Part 1 1.Introduction to XML-Schema 2.Schema basics 3.Mechanisms (strategies) for Designing Schema 4.Creating your own Datatypes

Prohibiting Derivations

• Sometimes we may want to create a type and disallow all derivations of it, or just disallow extension derivations, or disallow restriction derivations.– Rationale: "For example, I may create a complexType and make it

publicly available for others to use. However, I don't want them to extend it with their proprietary extensions or subset it to remove, say, copyright information."

<xsd:complexType name="Publication" final="#all" …> Publication cannot be extended nor restricted

<xsd:complexType name="Publication" final="restriction" …> Publication cannot be restricted

<xsd:complexType name="Publication" final="extension" …> Publication cannot be extended