23
XML Schemas J. Pontes November 15, 2004

XML Schemas J. Pontes November 15, 2004. Schemas Defines what a set of one or more document can look like. What elements it contains, order, content,

Embed Size (px)

Citation preview

Page 1: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

XML Schemas

J. Pontes

November 15, 2004

Page 2: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

Schemas

Defines what a set of one or more document can look like.

What elements it contains, order, content, and attributes.

These documents are written in XML Schema language

Page 3: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

DTD Disadvantages compared to XML

Cannot be parsed by an XML parser All declarations in DTD are global

(impossible to define two elements with same name.)

DTD cannot control what kind of information a given element or attribute can contain.

Page 4: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

What XML Schema Buys You

One can define both global elements (used same way all along document) and local elements (particular meaning in a particular context).

XML Schemas contain a system of data types.

Gives more control over XML document

Page 5: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

Schema: types of content

Simple Type – text only Complex Types – contain other elements or

attributes.

Page 6: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

Simple Type

Date Integer String Customer built types

– In DTD we used #OCDATA to capture a name, a date or practically anything.

Page 7: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

Complex Type

Describe the structure of a document, rather than content.

Complex Types– Elements that contain elements– Elements that contain element and text– Elements that contain only text– Elements that are empty

• Each may contain attributes

Page 8: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

Examples of Built-in Simple Types

<xsd:element name=“weight” type=“xsd:string”/>

<xsd:element name=“population” type=“xsd:integer” />

Page 9: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

Custom Simple Type

<xsd:simpleType name=“zipcodeType”><xsd:restriction base=“xsd:string”><xsd:pattern value=“\d{5}(-\d{4}?” /></xsd:restriction>

<xsd:simpleType>

This type limits the content of elements (zipcodeType) to a string with 5 digits, an optional hyphen and 4 extra digits.

Page 10: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

Complex Type Definition<xsd:complexType name=“endType”>

<xsd:sequence><xsd:element name=“animal”type=“animalType” minOccurs=“1”maxOccurs=“unbounded”/>

</xsd:sequence><xsd:complexType>

Defines endType. It contains another element animal defined with another complex type animalType to define a particular element

Page 11: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

Local end Global declarations Global

– Declared at top level of a schema, just immediately below xsd:schema element. They are available to be used throughout the the rest of the schema.

– Global element declaration do not determine where an element can appear in the XML document – they only determine what the element will look like.

– A global element declaration must be explicitly referenced in order to have it appear in a corresponding XML document.

Simple– Locally declared elements. Limited to the complex type definition in

which they are declared and may not not be used elsewhere in the schema.

– Such locally declared elements are automatically referenced.

Page 12: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

Example of Local and Global Elements

<?xml version=“1.0”?><xsd:schema smlns:xsd=“http://www.w3.org/2000/10/XMLSchema><xsd:element name=“endanged_species” type=“endType”/><xsd:element name=“name” type=“xsd:string”/><xsd:complexType name=“endType”>

<xsd:sequence><xsd:element name=“animal”><xsd:complexType>

<xsd:sequence><xsd:element ref=“name”minOccurs=“2”/><xsd:element name=“source” type=“sourceType”/>

…</xsd:complexType>

automatically referenced.manually referenced.

Same name different definitions.

Page 13: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

Cont’d <xsd:complexType name=“habitatType”>

<xsd:sequence><xsd:element name=“river”><xsd:complexType>

<xsd:sequence><xsd:element ref=“name”minOccurs=“1”maxOccurs=“unbounded”/>

<xsd:element name=“source” type=“xsd:string”/>…</xsd:complexType>

Same name different definitions.

Page 14: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

Beginning a Simple Schema<?xml version=“1.0”?> //Declaring XML

<xsd:schema xmlns:xsd=“ http://www.w3.org/2000/10/XMLSchema ”

Any element or type that is prefixed with xsd: will be recognized as coming from namespace*

Schema rules go in this space.

</xsd:schema> Save you schema as a text only with the .xsd extension.

Page 15: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

Indicating a Location<?xml version=“1.0”?>

<endanged_species

xmlns:xsd=“ http://www.w3.org/2000/10/XMLSchema-instance ”

xsi:noNamespaceSchemaLocation=

“ http://www.cookwook.com/ns/end_species/end_species.xsd ”> *

* file.xsd previously created.

Page 16: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

Making Schemas Annotations

<?xml version=“1.0”?>

<xsd:schema xmlns:xsd=“ http://www.w3.org/2000/10/XMLSchema ”

<xsd:annotation>

<xsd:documentation>

This schema will be used to validate the set of XML documents for the Endangered Species Project.

</xsd:documentation>

</xsd:annotation>

Page 17: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

Defining Simple Types When declaring an element, you choose its name and what kind of content it

should contain.– xsd:string – string of characters– xsd:decimal – decimal number– xsd:boolean – true of false– xsd:date – CCYY-MM-DD– xsd:time - – xsd:uriReference - element will contain an URL– xsd:language – two letter language listed in ISO639– Custom – name of a custom simple type.

– Code.xsd<xsd:element name="weight" type="xsd:string" /> <xsd:element name="population" type="xsd:integer" />

– Code.xml<weight>500 pounds</weight><population>28</population>

When a value is an integer or a string?

Page 18: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

More XML Data Types

Click Here for More Data Types

Page 19: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

Date and Time Data Types Code.xsd

<xsd:element name=“gestation”

type=“xsd:timeDuration”/>

(represent a certain amount of time)

Code.xml

<gestation>P3M15D</gestation>

(PnYnMnDTnHnMnS)

Period

n - non negative

T begins optional time section

Page 20: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

Code.xsdxsd:element name=“bedtime” type=“xsd.time:/>

Code.xml

<bedtime>20:15:05-05:00>/bedtime>

Date and Type Data Types

Page 21: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

Additional xsd:time

xsd:time (hh:mm:ss.sss) xsd:timeInstant (CCYY-MM-DDTh:mm:ss.sss)

xsd:date CCYY-MM-DD xsd:month (CCYY-MM xsd:year (CCYY) xsd:century (CC) xsd:recurringDate xsd:recurringDay

Page 22: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

Number Type

xsd:decimal xsd:integer xsd:positiveinteger xsd:negativeinteger xsd:float xsd:double

Page 23: XML Schemas J. Pontes November 15, 2004. Schemas  Defines what a set of one or more document can look like.  What elements it contains, order, content,

XML Validator

STG XML Validation