XML schemas


Published on

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

XML schemas

  1. 1. XML Schemas<br />CIS-189<br />
  2. 2. Alternative to DTD’s as way to define structure<br />Defining a language (vocabulary)<br />Structure may be also referred to as vocabulary<br />Ensures that data matches specifications<br />Serves as basis for other XML-related technologies<br />XML Schemas<br />
  3. 3. Use XML for definition<br />Doesn’t have separate structure like DTD’s<br />Schema must be well-formed<br />Provides for built-in and user-defined data types<br />Can be easily reused<br />Supports concepts such as inheritance<br />One object is based on another<br />A definition may be reused and modified without starting from scratch each time<br />Working with Schemas<br />
  4. 4. Support World Wide Web Namespace recommendations<br />A namespace allows the same name (data type/definition) to be used in different Schemas and properly understood<br />A course may be defined as department, course number, title, credits and prerequisites for the College Catalog<br />A course for grading may be defined as department, course number and credits for a grading application<br />Using namespaces allows both definitions to be used, by specifying if working with a course defined for the catalog or grading<br />Schemas and Namespaces<br />
  5. 5. Schema file uses an .xsd extension<br />Root element is the schema<br />Can nest all elements within the schema<br />Everything is hierarchical<br />OR<br />Can have multiple elements as child elements of the schema root<br />Allows use of a definition any place in the document (data) file<br />Elements which are child elements of schema are global<br />Creating Schemas<br />
  6. 6. Allows more specificity than DTD’s<br />Can specify dates, numbers, ranges<br />Datatypes fall into two categories:<br />Simple deals with basic values<br />Complex describes more intricate values or structures<br />Schema Datatypes<br />
  7. 7. Simple data type is about text, numbers, date<br />Sometime referred to as “primitives”<br />Data types built in to Schema vocabulary (and related elements, attributes) are in the XML Schema namespace<br />Need reference to namespace to have valid XML specified (where to find the defined type)<br />Elements that are Simple Datatypes don’t have attributes<br />Including an attribute makes an element Complex<br />Simple Datatypes<br />
  8. 8. String<br />Boolean<br />Numbers<br />Integer<br />Decimal<br />Float<br />Double<br />Custom (simpleType)<br />Date/time<br />Time<br />TimeInstant<br />Duration<br />Date<br />Month<br />Year<br />Century<br />RecurringDate<br />RecurringDay<br />Simple Types<br />
  9. 9. <?xml version=“1.0”?><br /><xsd:schemaxmlns:xsd=“http://www.w3.org/2001/XMLSchema”><br /> <xsd:element name=“department” type=“xsd:string”/><br /> <xsd:element name=“number” type=“xsd:string”/><br /> <xsd:element name=“title” type=“xsd:string”/><br /> <xsd:element name=“credits” type=“xsd:integer”/><br /></schema><br />Schema Defining a Course Using Simple Types<br />xmlns:xsd= specifies where the namespace can be found<br />xsd: specifies the namespace where the definition exists<br />Assign attribute values to create the definition of an element in your vocabulary<br />
  10. 10. The simpleType allows customization of base types<br />Can create limits on values<br />Specify ranges<br />Specify lists<br />Degrees is a simple type, based on string:<br /><xsd:simpleType=“Degrees”><br /> <xsd:restriction base=“xsd:string”><br /> <xsd:enumeration value=“AA” /><br /> <xsd:enumeration value=“AS” /><br /> </xsd: restriction><br /></xsd:simpleType><br />Defining (Simple) Datatypes<br />
  11. 11. Complex types allows combination of different elements and specification of order, new data types<br />Can create an element Course which is comprised of simple types<br />A valid Course must have department, number, title and credits elements in order:<br /><xsd:element name=“course”><br /> <xsd:complexType><br /> <xsd:sequence><br /> <xsd:element name=“department” type=“xsd:string”/><br /> <xsd:element name=“number” type=“xsd:string”/><br /> <xsd:element name=“title” type=“xsd:string”/><br /> <xsd:element name=“credits” type=“xsd:integer”/><br /> </xsd:sequence><br /> </xsd:complexType><br /></xsd:element><br />ComplexDatatypes<br />
  12. 12. When using a schema, need to create a reference from data (.xml) file<br />Use either the schemaLocation or noNamespaceSchemaLocation attribute of the root element<br /><course xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:noNamespaceShemaLocation=“course.xsd”><br />Using a Schema<br />
  13. 13. Creating a simple type<br />A phone number is text, up to 20 characters<br /><xs:simpleType name="PhoneSimpleType“><br /> <xs:restrictionbase="xs:string“><br /> <xs:maxLength value="20" /><br /> </xs:restriction><br /></xs:simpleType><br />Book Code – Employees.xsd<br />
  14. 14. Using a simple type:<br />The element homephone uses the simple type “PhoneSimpleType” to limit phone numbers to a maximum of 20 characters<br /><xs:element name="homephone" type="PhoneSimpleType" /><br />Book Code – Employees.xsd 2<br />
  15. 15. Employees is a complex type made of employee, which uses the EmployeeType<br /><xs:element name="employees“><br /> <xs:complexType><br /> <xs:sequence><br /> <xs:element name="employee" type="EmployeeType" minOccurs="0" maxOccurs="unbounded" /><br /> </xs:sequence><br /></xs:complexType><br /></xs:element><br />Book Code – Employees.xsd 3<br />
  16. 16. Setting the root element for a schema<br /><?xml version="1.0" encoding="utf-8"?><br /><xs:schemaattributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"><br />Book Code – Employees.xsd 4<br />xs becomes “shorthand” to refer to the Internet location for definitions; following element definitions all include that reference (xs)<br />