xml schema vinod kumar kayartaya. what is xml schema? xml schema is an xml based alternative to dtd...

45
XML Schema XML Schema Vinod Kumar Kayartaya

Upload: derrick-briggs

Post on 16-Jan-2016

270 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

XML SchemaXML Schema

Vinod Kumar Kayartaya

Page 2: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

What is XML Schema?What is XML Schema?

XML Schema is an XML based alternative to DTD

An XML schema describes the structure of an XML document

The purpose of an XML Schema is to define the legal building blocks of an XML document, just like a DTD

XML Schemas support data types, which DTDs do not

The XML Schema language is also referred to as XML Schema Definition (XSD)

XML documents can have a reference to a DTD or an XML Schema

Page 3: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

What Does XML Schema Define?What Does XML Schema Define?

An XML Schema:– defines elements that can appear in a document – defines attributes that can appear in a document – defines which elements are child elements – defines the sequence in which the child elements

can appear – defines the number of child elements – defines whether an element is empty or can

include text – defines data types for elements and attributes – defines default values for elements and

attributes

Page 4: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

XML Schema SpecificationXML Schema Specification

XML Schema specification consists of two parts:– Structures– Datatypes

http://www.w3.org/TR/xmlschema-0/ (Primer)

http://www.w3.org/TR/xmlschema-1/ (Structures)

http://www.w3.org/TR/xmlschema-2/ (Datatypes)

Page 5: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

XML TermsXML Terms

Instance Document – an XML document that is valid according to a particular schema

Schema – the XML document that has structural rules defined for an XML document

Instance Document (.xml file)

Schema (.xsd file)

Schema Validator

Page 6: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Building Blocks of SchemaBuilding Blocks of Schema

Element declarations – Complex type definitions – Simple type definitions

Attribute declarations

Page 7: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Schema ElementsSchema Elements

Simple Elements:– they do not contain other elements<xs:element name="name" type="type"/>

Complex Elements– they contain other elements<xs:element name="name">

<xs:complexType> . . element content . </xs:complexType> </xs:element>

Page 8: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Schema – Example 1Schema – Example 1<!ELEMENT note (to, from,

heading, body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)>

<!ELEMENT note (to, from, heading, body)>

<!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)>

<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"><xsd:element name="note"> <xsd:complexType> <xsd:sequence> <xsd:element name="to" type="xsd:string"/> <xsd:element name="from" type="xsd:string"/> <xsd:element name="heading" type="xsd:string"/> <xsd:element name="body" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema>

<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"><xsd:element name="note"> <xsd:complexType> <xsd:sequence> <xsd:element name="to" type="xsd:string"/> <xsd:element name="from" type="xsd:string"/> <xsd:element name="heading" type="xsd:string"/> <xsd:element name="body" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema>

<?xml version="1.0"?> <note> <to>Mary</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget me this

weekend!</body> </note> www.w3schools.com

<?xml version="1.0"?> <note> <to>Mary</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget me this

weekend!</body> </note> www.w3schools.com

Page 9: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

The NamespacesThe Namespaces

XML Schema Namespace– The elements and datatypes that are used

to construct schemas (schema, element, complexType, sequence, string, etc.) come from the http://www.w3.org/2001/XMLSchema namespacehttp://www.w3.org/2001/XMLSchema

elementcomplexType

schema

sequence

string

integer

boolean

Page 10: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

The Namespaces…The Namespaces…

Reference to the schema in the instance document

<?xml version="1.0"?><note xmlns:xsi=

"http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "note.xsd">

<to>Mary</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>

Reference to the schema in the instance document

<?xml version="1.0"?><note xmlns:xsi=

"http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "note.xsd">

<to>Mary</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>

Page 11: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

The Namespaces…The Namespaces…

XML Schema-instance Namespace

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

schemaLocationtype

noNamespaceSchemaLocation

nil

Page 12: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

The Levels of ValidationThe Levels of Validation

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

Validate that note.xmlconforms to the rules describedin note.xsd

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

Page 13: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Common XML Schema Data TypesCommon XML Schema Data Types

XML Schema has a lot of built-in data types. Some of them:– xs:string – xs:decimal – xs:integer – xs:boolean – xs:date – xs:time

Page 14: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Schema – Example 2Schema – Example 2

<!ELEMENT BookStore (Book)+>

<!ELEMENT Book (Title, Author, Date, ISBN, Publisher)>

<!ELEMENT Title (#PCDATA)>

<!ELEMENT Author (#PCDATA)>

<!ELEMENT Date (#PCDATA)>

<!ELEMENT ISBN (#PCDATA)>

<!ELEMENT Publisher (#PCDATA)>

<!ELEMENT BookStore (Book)+>

<!ELEMENT Book (Title, Author, Date, ISBN, Publisher)>

<!ELEMENT Title (#PCDATA)>

<!ELEMENT Author (#PCDATA)>

<!ELEMENT Date (#PCDATA)>

<!ELEMENT ISBN (#PCDATA)>

<!ELEMENT Publisher (#PCDATA)>

Page 15: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Schema – Example 2…Schema – Example 2…<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Date" minOccurs="1" maxOccurs="1"/> <xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> <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:schema>

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Date" minOccurs="1" maxOccurs="1"/> <xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> <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:schema>

Reusing declarations with the

ref attribute

Page 16: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Schema – Example 2…Schema – Example 2…

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema“><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>

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema“><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>

Inline Element Declarations

Anonymous type

Page 17: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Occurrence Constraints on Occurrence Constraints on ElementsElements The attributes minOccurs and maxOccurs can

be used to indicate the number of times an element can appear

The default value for both the attributes minOccurs and maxOccurs is 1. If both the attributes are omitted, the element should appear exactly once.

Page 18: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Default and Fixed Values for Simple Default and Fixed Values for Simple Elements Elements

Elements can have either a default value OR a fixed value set

A default value is automatically assigned to the element when no other value is specified

A fixed value is also automatically assigned to the element when no other value is specified

But, unlike default values, if you specify another value than the fixed value, the document is considered invalid

<xs:element name="color" type="xs:string" default="red"/>

<xs:element name="color" type="xs:string" fixed="red"/>

<xs:element name="color" type="xs:string" default="red"/>

<xs:element name="color" type="xs:string" fixed="red"/>

Page 19: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Complex TypesComplex Types

Page 20: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Complex Types – Named TypesComplex Types – Named Types

The complex type definitions create new complex types, which can be reused in the element declarations

New complex types are defined using the complexType element

ComplexType definition typically contains a set of – element declarations, – element references, and – attributes declarations

Page 21: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Example –Complex (Named) Type Example –Complex (Named) Type DefinitionDefinition

Instance Document

<?xml version="1.0"?>

<note>

<to>Mary</to>

<from>John</from>

<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>

</note>

Instance Document

<?xml version="1.0"?>

<note>

<to>Mary</to>

<from>John</from>

<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>

</note>

Page 22: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Example –Complex (Named) Type Example –Complex (Named) Type Definition…Definition…

<?xml version="1.0"?>

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

<xs:element name="note" type="noteType"/>

<xs:complexType name="noteType">

<xs:sequence>

<xs:element name="to" type="xs:string"/>

<xs:element name="from" type="xs:string"/>

<xs:element name="heading" type="xs:string"/>

<xs:element name="body" type="xs:string"/>

</xs:sequence>

</xs:complexType>

</xs:schema>

<?xml version="1.0"?>

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

<xs:element name="note" type="noteType"/>

<xs:complexType name="noteType">

<xs:sequence>

<xs:element name="to" type="xs:string"/>

<xs:element name="from" type="xs:string"/>

<xs:element name="heading" type="xs:string"/>

<xs:element name="body" type="xs:string"/>

</xs:sequence>

</xs:complexType>

</xs:schema>

Page 23: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Simple TypesSimple Types

Page 24: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Simple TypesSimple Types

Simple types could be – Built-in simple types (xs:string, xs:int, etc.)– Those derived from simple types or

existing derived ones• Deriving can be done by restricting the values

that the new type can take on, as a subset of the values of the existing simple type

• Restricting the range of values for the new simple type can be done with the xsd:restriction element

• Facets constrain the values

Page 25: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Derived Simple TypesDerived Simple Types

A new simple type salaryType derived from the integer type:

<xsd:element name="salary" type="salaryType"/>

<xsd:simpleType name="salaryType">

<xsd:restriction base="xsd:integer">

<xsd:minInclusive value="5000"/>

<xsd:maxInclusive value="10000"/>

</xsd:restriction>

</xsd:simpleType>

<xsd:element name="salary" type="salaryType"/>

<xsd:simpleType name="salaryType">

<xsd:restriction base="xsd:integer">

<xsd:minInclusive value="5000"/>

<xsd:maxInclusive value="10000"/>

</xsd:restriction>

</xsd:simpleType>

Page 26: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Derived Simple Types…Derived Simple Types…

A new simple type bookType derived using the enumeration facet

<xs:element name="book" type="bookType"/><xs:simpleType name="bookType"> <xs:restriction base="xs:string"> <xs:enumeration value="Technical"/> <xs:enumeration value="Managerial"/> <xs:enumeration value="Fiction"/> </xs:restriction> </xs:simpleType>

<xs:element name="book" type="bookType"/><xs:simpleType name="bookType"> <xs:restriction base="xs:string"> <xs:enumeration value="Technical"/> <xs:enumeration value="Managerial"/> <xs:enumeration value="Fiction"/> </xs:restriction> </xs:simpleType>

The type "carType" can be

used by other elements because it is not a part of

the "car" element

The type "carType" can be

used by other elements because it is not a part of

the "car" element

Page 27: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

An Anonymous Simple Type An Anonymous Simple Type

This simple type cannot be reused as it is part of the element declaration

<xs:element name="book"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Technical"/> <xs:enumeration value="Managerial"/> <xs:enumeration value="Fiction"/> </xs:restriction> </xs:simpleType></xs:element>

<xs:element name="book"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Technical"/> <xs:enumeration value="Managerial"/> <xs:enumeration value="Fiction"/> </xs:restriction> </xs:simpleType></xs:element>

Page 28: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Summary – Declaring ElementsSummary – Declaring Elements

A simple type(e.g., xs:string)or the name ofa complexType(e.g., noteType)

<xs:element name="name" minOccurs="int" maxOccurs="int"> <xs:complexType> … </xs:complexType></xs:element>

<xs:element name="name" minOccurs="int" maxOccurs="int"> <xs:complexType> … </xs:complexType></xs:element>

<xs:element name="name" type="type" minOccurs="int" maxOccurs="int"/>

<xs:element name="name" type="type" minOccurs="int" maxOccurs="int"/>

A nonnegative integer

A nonnegative integer or the value “unbounded”

Page 29: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Summary – Declaring Elements…Summary – Declaring Elements…

<xs:element name="name" minOccurs="int" maxOccurs="int"> <xs:simpleType> … </xs:simpleType></xs:element>

<xs:element name="name" minOccurs="int" maxOccurs="int"> <xs:simpleType> … </xs:simpleType></xs:element>

Page 30: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Summary – Defining Reusable Summary – Defining Reusable TypesTypes

<xs:complexType name="name"> <xs:sequence> … </xs:sequence></xs:complexType>

<xs:complexType name="name"> <xs:sequence> … </xs:sequence></xs:complexType>

<xs:simpleType name="name"> <xs:restriction> … </xs:restriction></xs:simpleType>

<xs:simpleType name="name"> <xs:restriction> … </xs:restriction></xs:simpleType>

Page 31: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Schema AttributesSchema Attributes

An element that has an attribute should always be declared as complex type

The attribute declaration must reference simple types always

<?xml version="1.0"?>

<book category="technical"

xmlns:xsi=

"http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation= "book.xsd">

<title>XML Schemas</title>

<ISBN>200-ABCD</ISBN>

<price currency="USD">12.5</price>

</book>

<?xml version="1.0"?>

<book category="technical"

xmlns:xsi=

"http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation= "book.xsd">

<title>XML Schemas</title>

<ISBN>200-ABCD</ISBN>

<price currency="USD">12.5</price>

</book>

Book.xml

Page 32: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Schema Attributes…Schema Attributes…<?xml version="1.0"?>

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

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

<xs:complexType name="bookType">

<xs:sequence>

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

<xs:element name="ISBN" type="ISBNType"/>

<xs:element name="price" type="priceType"/>

</xs:sequence>

<xs:attribute name="category" type="xs:string"/>

</xs:complexType>

<xs:simpleType name="ISBNType">

<xs:restriction base="xs:string">

<xs:pattern value="\d{3}-[A-Z]{4}"/>

</xs:restriction>

</xs:simpleType>

<xs:complexType name="priceType">

<xs:simpleContent>

<xs:extension base="xs:decimal">

<xs:attribute name="currency" type="xs:string"/>

</xs:extension>

</xs:simpleContent>

</xs:complexType>

</xs:schema>

<?xml version="1.0"?>

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

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

<xs:complexType name="bookType">

<xs:sequence>

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

<xs:element name="ISBN" type="ISBNType"/>

<xs:element name="price" type="priceType"/>

</xs:sequence>

<xs:attribute name="category" type="xs:string"/>

</xs:complexType>

<xs:simpleType name="ISBNType">

<xs:restriction base="xs:string">

<xs:pattern value="\d{3}-[A-Z]{4}"/>

</xs:restriction>

</xs:simpleType>

<xs:complexType name="priceType">

<xs:simpleContent>

<xs:extension base="xs:decimal">

<xs:attribute name="currency" type="xs:string"/>

</xs:extension>

</xs:simpleContent>

</xs:complexType>

</xs:schema>

Book.xsd

Page 33: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Attribute DefaultsAttribute Defaults

Attributes can have either a default value OR a fixed value specified

<xs:attribute name="currency" type="xs:string" default="INR"/>

<xs:attribute name="currency" type="xs:string" default="INR"/>

<xs:attribute name="currency" type="xs:string" fixed="INR"/>

<xs:attribute name="currency" type="xs:string" fixed="INR"/>

Page 34: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Attribute OccurrenceAttribute Occurrence

Attribute occurrence (once or none) is indicated by use attribute

Values for use attribute:– required, optional, prohibited– Default value is optional

<xs:attribute name="currency" type="xs:string" use="optional"/>

<xs:attribute name="currency" type="xs:string" use="optional"/>

<xs:attribute name="currency" type="xs:string" use="required"/>

<xs:attribute name="currency" type="xs:string" use="required"/>

Page 35: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Complex Types – More Complex Types – More Element Content ModelsElement Content Models

Page 36: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Complex Elements – More Complex Elements – More ExamplesExamples Element Contents

– Elements containing only a simple type value (text)

– Elements containing other elements– Elements containing other elements and

attributes– Empty elements (with or without

attributes)– Elements containing simple type value

(text) and attributes

Page 37: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Empty ElementsEmpty Elements

The existence of an attribute makes an element complex type, though it may not contain any content at all

<phone ftype="home" number="238654"/><phone ftype="home" number="238654"/>

<xs:element name="phone"><xs:complexType> <xs:attribute name="ftype" type="xs:string"/> <xs:attribute name="number" type="xs:string"/>

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

<xs:element name="phone"><xs:complexType> <xs:attribute name="ftype" type="xs:string"/> <xs:attribute name="number" type="xs:string"/>

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

Page 38: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Element with Text and Attribute Element with Text and Attribute

<xs:element name="phone"> <xs:complexType> <xs:simpleContent>

<xs:extension base="xs:string"> <xs:attribute name="ftype" type="xs:string"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element>

<xs:element name="phone"> <xs:complexType> <xs:simpleContent>

<xs:extension base="xs:string"> <xs:attribute name="ftype" type="xs:string"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element>

<phone ftype="home">347563256</phone> <phone ftype="home">347563256</phone>

Page 39: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Using anyTypeUsing anyType

anyType allows for no constraints on the content at all

If you do not specify any type in the declaration, it defaults to anyType

<xs:element name="elem1" type="xs:anyType"/> <xs:element name="elem1" type="xs:anyType"/>

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

Page 40: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Element Occurrence – Order Element Occurrence – Order IndicatorsIndicators You can use the following indicators to

mention how the elements should occur within a complex element/type– All

• Indicates that the child elements can appear in any order, but each can occur at most once (minOccurs=0, maxOccurs=1)

– Choice• Indicates either of the child elements only can

occur

– Sequence• Indicates that the child elements should occur

only in the order specified

Page 41: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Element Occurrence Indicator - AllElement Occurrence Indicator - All

<?xml version="1.0"?>

<PersonInfo

xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation= "personinfo-all.xsd">

<person>

<name>Ram</name>

<title>Executive</title>

<dept>Sales</dept>

<email>[email protected]</email>

<phone>8723453194</phone>

</person>

<person>

<name>Sham</name>

<title>Manager</title>

<dept>Sales</dept>

<phone>8723453195</phone>

<email>[email protected]</email>

</person>

</PersonInfo>

<?xml version="1.0"?>

<PersonInfo

xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation= "personinfo-all.xsd">

<person>

<name>Ram</name>

<title>Executive</title>

<dept>Sales</dept>

<email>[email protected]</email>

<phone>8723453194</phone>

</person>

<person>

<name>Sham</name>

<title>Manager</title>

<dept>Sales</dept>

<phone>8723453195</phone>

<email>[email protected]</email>

</person>

</PersonInfo>

Page 42: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Element Occurrence Indicator – Element Occurrence Indicator – All…All…

<?xml version="1.0"?>

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

<xs:element name="PersonInfo">

<xs:complexType>

<xs:sequence>

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

<xs:complexType>

<xs:all>

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

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

<xs:element name="dept" type="xs:string"/>

<xs:element name="email" type="xs:string"/>

<xs:element name="phone" type="xs:string"/>

</xs:all>

</xs:complexType>

</xs:element>

</xs:sequence>

</xs:complexType>

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

<?xml version="1.0"?>

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

<xs:element name="PersonInfo">

<xs:complexType>

<xs:sequence>

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

<xs:complexType>

<xs:all>

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

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

<xs:element name="dept" type="xs:string"/>

<xs:element name="email" type="xs:string"/>

<xs:element name="phone" type="xs:string"/>

</xs:all>

</xs:complexType>

</xs:element>

</xs:sequence>

</xs:complexType>

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

Page 43: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Element Occurrence Indicator – Element Occurrence Indicator – ChoiceChoice

<?xml version="1.0"?>

<employees

xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation= "employee-choice.xsd">

<employee>

<name>Ram</name>

<title>Executive</title>

<salary>20000</salary>

</employee>

<employee>

<name>Sham</name>

<title>Manager</title>

<salary>30000</salary>

</employee>

</employees>

<?xml version="1.0"?>

<employees

xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation= "employee-choice.xsd">

<employee>

<name>Ram</name>

<title>Executive</title>

<salary>20000</salary>

</employee>

<employee>

<name>Sham</name>

<title>Manager</title>

<salary>30000</salary>

</employee>

</employees>

Page 44: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Element Occurrence Indicator – Choice…Element Occurrence Indicator – Choice…

<?xml version="1.0"?>

<employees

xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation= "employee-choice.xsd">

<contract>

<name>Anil</name>

<rate>500</rate>

<valid-till>2005-06-30</valid-till>

</contract>

<contract>

<name>Sunil</name>

<rate>500</rate>

<valid-till>2005-07-30</valid-till>

</contract>

</employees>

<?xml version="1.0"?>

<employees

xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation= "employee-choice.xsd">

<contract>

<name>Anil</name>

<rate>500</rate>

<valid-till>2005-06-30</valid-till>

</contract>

<contract>

<name>Sunil</name>

<rate>500</rate>

<valid-till>2005-07-30</valid-till>

</contract>

</employees>

Page 45: XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML

Element Occurrence Indicator – Choice…Element Occurrence Indicator – Choice…

<?xml version="1.0"?>

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

<xs:element name="employees">

<xs:complexType>

<xs:choice>

<xs:element name="employee" type="regularType" maxOccurs="unbounded"/>

<xs:element name="contract" type="contractType" maxOccurs="unbounded"/>

</xs:choice>

</xs:complexType>

</xs:element>

<xs:complexType name="regularType">

<xs:sequence>

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

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

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

</xs:sequence>

</xs:complexType>

<xs:complexType name="contractType">

<xs:sequence>

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

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

<xs:element name="valid-till" type="xs:date"/>

</xs:sequence>

</xs:complexType>

</xs:schema>

<?xml version="1.0"?>

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

<xs:element name="employees">

<xs:complexType>

<xs:choice>

<xs:element name="employee" type="regularType" maxOccurs="unbounded"/>

<xs:element name="contract" type="contractType" maxOccurs="unbounded"/>

</xs:choice>

</xs:complexType>

</xs:element>

<xs:complexType name="regularType">

<xs:sequence>

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

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

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

</xs:sequence>

</xs:complexType>

<xs:complexType name="contractType">

<xs:sequence>

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

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

<xs:element name="valid-till" type="xs:date"/>

</xs:sequence>

</xs:complexType>

</xs:schema>