2. 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
3. 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
4. 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
5. 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
6. 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
7. 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
8. String Boolean Numbers Integer Decimal Float Double Custom (simpleType) Date/time Time TimeInstant Duration Date Month Year Century RecurringDate RecurringDay Simple Types
9. <?xml version=“1.0”?> <xsd:schemaxmlns: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 xmlns:xsd= specifies where the namespace can be found xsd: specifies the namespace where the definition exists Assign attribute values to create the definition of an element in your vocabulary
10. 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
11. 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> ComplexDatatypes
12. 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
13. Creating a simple type A phone number is text, up to 20 characters <xs:simpleType name="PhoneSimpleType“> <xs:restrictionbase="xs:string“> <xs:maxLength value="20" /> </xs:restriction> </xs:simpleType> Book Code – Employees.xsd
14. 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
15. 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
16. Setting the root element for a schema <?xml version="1.0" encoding="utf-8"?> <xs:schemaattributeFormDefault="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)