XML schemas

16
XML Schemas CIS-189

Transcript of XML schemas

Page 1: XML schemas

XML SchemasCIS-189

Page 2: XML schemas

Alternative to DTD’s as way to define structure◦ Defining a language (vocabulary)◦ Structure may be also referred to as vocabulary

Ensures that data matches specifications Serves as basis for other XML-related

technologies

XML Schemas

Page 3: XML schemas

Use XML for definition◦ Doesn’t have separate structure like DTD’s◦ Schema must be well-formed

Provides for built-in and user-defined data types

Can be easily reused Supports concepts such as inheritance

◦ One object is based on another◦ A definition may be reused and modified without

starting from scratch each time

Working with Schemas

Page 4: XML schemas

Support World Wide Web Namespace recommendations

A namespace allows the same name (data type/definition) to be used in different Schemas and properly understood◦ A course may be defined as department, course

number, title, credits and prerequisites for the College Catalog

◦ A course for grading may be defined as department, course number and credits for a grading application

◦ Using namespaces allows both definitions to be used, by specifying if working with a course defined for the catalog or grading

Schemas and Namespaces

Page 5: XML schemas

Schema file uses an .xsd extension Root element is the schema

◦ Can nest all elements within the schema Everything is hierarchical

OR◦ Can have multiple elements as child elements of

the schema root Allows use of a definition any place in the document

(data) file Elements which are child elements of schema are

global

Creating Schemas

Page 6: XML schemas

Allows more specificity than DTD’s◦ Can specify dates, numbers, ranges

Datatypes fall into two categories:◦ Simple deals with basic values◦ Complex describes more intricate values or

structures

Schema Datatypes

Page 7: XML schemas

Simple data type is about text, numbers, date◦ Sometime referred to as “primitives”

Data types built in to Schema vocabulary (and related elements, attributes) are in the XML Schema namespace◦ Need reference to namespace to have valid XML

specified (where to find the defined type) Elements that are Simple Datatypes don’t

have attributes◦ Including an attribute makes an element Complex

Simple Datatypes

Page 8: XML schemas

String Boolean Numbers

◦ Integer◦ Decimal◦ Float◦ Double

Custom (simpleType)

Date/time◦ Time◦ TimeInstant◦ Duration◦ Date◦ Month◦ Year◦ Century◦ RecurringDate◦ RecurringDay

Simple Types

Page 9: XML schemas

<?xml version=“1.0”?><xsd:schema

xmlns:xsd=“http://www.w3.org/2001/XMLSchema”><xsd:element name=“department” type=“xsd:string”/><xsd:element name=“number” type=“xsd:string”/><xsd:element name=“title” type=“xsd:string”/><xsd:element name=“credits” type=“xsd:integer”/>

</schema>

Schema Defining a Course Using Simple Types

xsd: specifies the namespace where the definition exists

Assign attribute values to create the definition of an element in your vocabulary

xmlns:xsd= specifies where the namespace can be found

Page 10: XML schemas

The simpleType allows customization of base types

Can create limits on values◦ Specify ranges◦ Specify lists

Degrees is a simple type, based on string:

<xsd:simpleType=“Degrees”><xsd:restriction base=“xsd:string”>

<xsd:enumeration value=“AA” /><xsd:enumeration value=“AS” />

</xsd: restriction></xsd:simpleType>

Defining (Simple) Datatypes

Page 11: XML schemas

Complex types allows combination of different elements and specification of order, new data types

Can create an element Course which is comprised of simple types A valid Course must have department, number, title and credits

elements in order:

<xsd:element name=“course”><xsd:complexType>

<xsd:sequence><xsd:element name=“department” type=“xsd:string”/><xsd:element name=“number” type=“xsd:string”/><xsd:element name=“title” type=“xsd:string”/><xsd:element name=“credits” type=“xsd:integer”/></xsd:sequence>

</xsd:complexType></xsd:element>

Complex Datatypes

Page 12: XML schemas

When using a schema, need to create a reference from data (.xml) file

Use either the schemaLocation or noNamespaceSchemaLocation attribute of the root element

<course xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:noNamespaceShemaLocation=“course.xsd”>

Using a Schema

Page 13: XML schemas

Creating a simple type◦ A phone number is text, up to 20 characters

<xs:simpleType name="PhoneSimpleType“><xs:restriction base="xs:string“>

<xs:maxLength value="20" /></xs:restriction>

</xs:simpleType>

Book Code – Employees.xsd

Page 14: XML schemas

Using a simple type: The element homephone uses the simple

type “PhoneSimpleType” to limit phone numbers to a maximum of 20 characters

<xs:element name="homephone" type="PhoneSimpleType" />

Book Code – Employees.xsd 2

Page 15: XML schemas

Employees is a complex type made of employee, which uses the EmployeeType

<xs:element name="employees“>

<xs:complexType>

<xs:sequence>

<xs:element name="employee" type="EmployeeType" minOccurs="0" maxOccurs="unbounded" />

</xs:sequence>

</xs:complexType>

</xs:element>

Book Code – Employees.xsd 3

Page 16: XML schemas

Setting the root element for a schema

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

elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

Book Code – Employees.xsd 4

xs becomes “shorthand” to refer to the Internet location for definitions; following element definitions all include that reference (xs)