XML schemas
-
Upload
randy-riness-south-puget-sound-community-college -
Category
Education
-
view
765 -
download
6
Transcript of XML schemas
XML SchemasCIS-189
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
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
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
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
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
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
String Boolean Numbers
◦ Integer◦ Decimal◦ Float◦ Double
Custom (simpleType)
Date/time◦ Time◦ TimeInstant◦ Duration◦ Date◦ Month◦ Year◦ Century◦ RecurringDate◦ RecurringDay
Simple Types
<?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
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
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
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
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
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
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
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)