32
XML Schema XML Schema XML XML http://yht4ever.blogspot.com [email protected] B070066 - NIIT Quang Trung 08/2007

XML Schema

Embed Size (px)

DESCRIPTION

Incompleted

Citation preview

Page 1: XML Schema

XML SchemaXML Schema

XMLXMLhttp://yht4ever.blogspot.com

[email protected] - NIIT Quang Trung

08/2007

Page 2: XML Schema

XML Schema Slide 2

Contents

Create group of element and attribute

Attribute in XML Schemas

Elements in XML Schemas

Data Types in XML Schemas

Introduction to XML Schema

Page 3: XML Schema

XML Schema Slide 3

Introduction to XML Schema

An XML schema defines the list of elements and attributes that can be used in an XML document.

It also specifies the order in which these elements appear in the XML document, and their data types.

Microsoft has developed the XML Schema Definition (XSD) language to define the schema of an XML document

Page 4: XML Schema

XML Schema Slide 4

Advantages of XML Schema

XSD provides more control over the type of data that can be assigned to elements and attributes as compared to DTD.

XSD enables you to create your own data types.

XSD enables you to specify restrictions on data.

The syntax for defining an XSD is the same as the syntax used for XML documents.

XML schema content models can be used to validate mixed content, which cannot be validated by DTD content models.

XML schema is extensible.

Page 5: XML Schema

XML Schema Slide 5

Data Types in XML Schemas

A data type specifies the type of content that an element can hold.

XSD provides a list of predefined data types. These data types can be classified as follows: Primitive: Are the fundamental data types of XSD. Derived: Are defined by using other data types called

base types. Atomic: Are primitive or derived data types that cannot

be broken down into smaller units. List: Are derived data types that contain a set of values

of atomic data types. Union: Are derived from the atomic and list data types.

Page 6: XML Schema

XML Schema Slide 6

Elements in XML Schemas

There are two types of elements, simple and complex.

•Simple Element

•A simple element does not contain any child elements or attributes.

•The syntax for declaring a simple element is as follows:<xsd:element name="element-name" type="data type" minOccurs="nonNegativeInteger" maxOccurs="nonNegativeInteger|unbounded"/>

Page 7: XML Schema

XML Schema Slide 7

Example

<PRODUCTDATA>

<PRODUCT PRODUCTID="P001" CATEGORY="BOOKS">

<PRODUCTNAME>Gone With the Wind</PRODUCTNAME>

<DESCRIPTION> The backdrop of this book is the American Civil War</DESCRIPTION>

<PRICE>25.00</PRICE>

<QUANTITY>35</QUANTITY>

</PRODUCT>

</PRODUCTDATA>

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

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

<xsd:element name="PRICE" type="xsd:positiveInteger"/>

<xsd:element name="QUANTITY" type="xsd:nonNegativeInteger"/>

Page 8: XML Schema

XML Schema Slide 8

Elements in XML Schemas (cont.)

•Complex Element

•A complex element contains other elements, attributes, and mixed content.

•The syntax to define a complex data type is as follows:<xsd:complexType name="data type name">

Content model declaration

</xsd:complexType>

Page 9: XML Schema

XML Schema Slide 9

Elements in XML Schemas (cont.)

Defining a New Simple Data Type Based on an Existing Simple Data Type

<xsd:simpleType name="phoneno">

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

<xsd:length value="8"/>

<xsd:pattern value="\d{3}-\d{4}"/>

</xsd:restriction>

</xsd:simpleType>

Page 10: XML Schema

XML Schema Slide 10

Elements in XML Schemas (cont.)

<xsd:simpleType name="num">

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

<xsd:maxInclusive value="400"/>

<xsd:minInclusive value="10"/>

</xsd:restriction>

</xsd:simpleType>

Page 11: XML Schema

XML Schema Slide 11

Elements in XML Schemas (cont.)

•The element to be declared as a complex type must be associated with a complex data type.

•A complex type element can be associated with a complex data type using the syntax: <xsd:element name=“ElementName" type=“ComplexDataType"/>In the preceding syntax, ElementName is the name of the element and ComplexDataType is the complex data type declared earlier.

Page 12: XML Schema

XML Schema Slide 12

Elements in XML Schemas (cont.)

<xsd:complexType name="prdt">

<xsd:sequence>

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

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

<xsd:element name="PRICE" type="xsd:positiveInteger"/>

<xsd:element name="QUANTITY" type="xsd:nonNegativeInteger"/>

</xsd:sequence>

</xsd:complexType>

Page 13: XML Schema

XML Schema Slide 13

Review

Simple element declarationComplex element declarationSimple type declarationComplex type declaration

Page 14: XML Schema

XML Schema Slide 14

Declaring Attributes in a Schema

Attribute declarations can be defined in two ways: Simple Type Definitions Global Attribute Declarations

The syntax for declaring an attribute in XSD is as follows: <attribute name="attributename" ref="attributename" type="datatypename" use="value" value="value"> </attribute>

Consists of various attributes:• name: Used to specify the name of a user-defined attribute. • ref: Used to refer to a user-defined attribute declared either in

the same or in any other XSD document.

Page 15: XML Schema

XML Schema Slide 15

Declaring Attributes in a Schema (cont.)

Consists of various attributes (cont.):• type: Takes a value that specifies the data type of

the user-defined attribute. • use: Specifies the way in which an attribute can

be used in an XML document.– optional– default– required– fixed

Page 16: XML Schema

XML Schema Slide 16

Declaring Attributes in a Schema (cont.)

Global Attributes Are declared outside all element declarations. Facilitate attribute reusability. For such attributes, the schema element is the

parent element. For example

<xsd:schema><xsd:attribute name="NAME" type="xsd:string"/

</xsd:schema>

Page 17: XML Schema

XML Schema Slide 17

Declaring Attributes in a Schema (cont.)

In order to restrict values that can be assigned to an attribute: Declare the attribute and associate it with a user-

defined simple data type. Create a simple data type by using the XSD

simpleType element. Use the XSD restriction element within the

simpleType element to restrict the values that can be assigned to the elements or attributes that use the simple data type.

Page 18: XML Schema

XML Schema Slide 18

Exercises

Create a Schema for music.xml

Page 19: XML Schema

XML Schema Slide 19

Create groups of elements and attributes

Feature of creating grouped elements and attributes facilitates the following tasks: Creating a reusable group of elements and attributes. Selecting a single element from a group. Specifying the sequence of elements.

XSD provides the following elements to group user-defined elements and attributes: sequence group choice all attributeGroup

Page 20: XML Schema

XML Schema Slide 20

Create groups of elements and attributes (cont.)

Sequence

• Ensures that the elements declared within the opening and closing tags appear in a specific order.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"><xsd:element name="EMPLOYEE" type="emptype"/><xsd:complexType name="emptype"><xsd:sequence>

<xsd:element name="FIRSTNAME" type="xsd:string"/><xsd:element name="LASTNAME" type="xsd:string"/><xsd:element name="DESIG" type="xsd:string"/><xsd:element name="DEPARTMENT"

type="xsd:string"/>

</xsd:sequence></xsd:complexType></xsd:schema>

Page 21: XML Schema

XML Schema Slide 21

Create groups of elements and attributes (cont.)

The group Element Grouping of elements is beneficial when you

want a set of related elements to be referred using a common name.

The syntax for declaring a group element is as follows:<group maxOccurs="nonNegetiveInteger | unbounded“ minOccurs="nonNegetiveInteger" name="NCName" ref="QName"> </group>

Page 22: XML Schema

XML Schema Slide 22

Create groups of elements and attributes (cont.)

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

<xsd:group name="empname"><xsd:sequence>

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

</xsd:sequence></xsd:group>

<xsd:element name="EMPLOYEE" type="emptype"/><xsd:complexType name="emptype">

<xsd:sequence><xsd:group ref="empname"/><xsd:element name="ADDRESS" type="xsd:string"/>

</xsd:sequence></xsd:complexType>

</xsd:schema>

Page 23: XML Schema

XML Schema Slide 23

Create groups of elements and attributes (cont.)

The choice Element Enables the selection of a single option from

multiple options. Allows only one of the elements contained in

the group to be present within the parent element.

The syntax for declaring a choice element is as follows:

• <choice id="ID" maxOccurs="nonNegativeInteger|unbounded" minOccurs="nonNegativeInteger"> </choice>

Page 24: XML Schema

XML Schema Slide 24

Create groups of elements and attributes (cont.)

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"><xsd:element name="CUSTOMER" type="custtype"/><xsd:complexType name="custtype">

<xsd:sequence><xsd:group ref="custname"/><xsd:element name="ADDRESS" type="addtype"/>

</xsd:sequence></xsd:complexType><xsd:complexType name="addtype">

<xsd:choice><xsd:element name="RESIDENCE" type="xsd:string"/><xsd:element name="OFFICE" type="xsd:string"/>

</xsd:choice></xsd:complexType><xsd:group name="custname">

<xsd:sequence><xsd:element name="FIRSTNAME" type="xsd:string"/><xsd:element name="LASTNAME" type="xsd:string"/>

</xsd:sequence></xsd:group>

</xsd:schema>

Page 25: XML Schema

XML Schema Slide 25

Create groups of elements and attributes (cont.)

The all Element Enables to use the child elements in any

order. The syntax for using the all element:

• <all maxOccurs="positiveInteger" minOccurs="0|1"> </all>

Page 26: XML Schema

XML Schema Slide 26

Create groups of elements and attributes (cont.)

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"><xsd:element name="EMPLOYEE" type="emptype" /><xsd:complexType name="emptype">

<xsd:all><xsd:element name="FIRSTNAME" type="xsd:string"/><xsd:element name="LASTNAME" type="xsd:string"/><xsd:element name="DESIG" type="xsd:string"/><xsd:element name="DEPARTMENT" type="xsd:string"/>

</xsd:all></xsd:complexType>

</xsd:schema>

Page 27: XML Schema

XML Schema Slide 27

Create groups of elements and attributes (cont.)

The attributeGroup Element Enables to group attributes that can be

reused with different elements. The syntax of the attributeGroup element is as

follows:• <attributeGroup>• attribute1• attribute2• :• </attributeGroup>

Page 28: XML Schema

XML Schema Slide 28

Create groups of elements and attributes (cont.)

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"><xsd:element name="EMPLOYEE" type="emptype"/><xsd:complexType name="emptype">

<xsd:group ref="empname"/><xsd:attributeGroup ref="depdesig"/>

</xsd:complexType><xsd:group name="empname">

<xsd:sequence><xsd:element name="FIRSTNAME" type="xsd:string"/><xsd:element name="LASTNAME"

type="xsd:string"/></xsd:sequence>

</xsd:group><xsd:attributeGroup name="depdesig">

<xsd:attribute name="DEPARTMENT" type="xsd:string"/><xsd:attribute name="DESIGNATION" type="xsd:string"/>

</xsd:attributeGroup></xsd:schema>

Page 29: XML Schema

XML Schema Slide 29

To be continued…

Page 30: XML Schema

XML Schema Slide 30

Reference

XML How to programhttp://www.w3.orgTeach Yourself XML in 21 Days, 3rd EditionLearning XML, 2nd EditionXML tutorial

http://www.w3schools.com/w3c/

Page 31: XML Schema

XML Schema Slide 31

Q&A

Feel free to post questions at http://yht4ever.blogspot.com.

or email to: [email protected] or [email protected]

Page 32: XML Schema

http://yht4ever.blogspot.com