XML Schema

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

Welcome to the XML Schema course in WikiBook Format. You are welcome to add or enhance this book by linking to sections in different pages. Please do not modify the overall structure without using the discussion pages.

Note: This course is designed as a reusable Learning Object and must take into consideration the constraints of many learning environments. Please think carefully about adding material that would not allow the course to be reused by a broad variety of audiences. Computer language and operating system-specific issues such as Java, Microsoft, Mac, GNU/Linux and Windows dependencies should be isolated into separate labs that can be optimally included by instructors.


Contents

[edit] Course Description

It describes the structure of an XML Schema and explains how XML Schemas are used to validate XML documents. It is designed to be reusable and portable for other courses and thus should be kept as modular as possible. These materials are designed to be implementation technology neutral so the course can be integrated into open source learning management systems such as Moodle. No examples should be given in either using Microsoft Java or Microsoft .Net. There are separate labs for these technologies.

[edit] Course Prerequisites

Students of this course should already be familiar with the fundamental principles of XML and have some background on Data Types

[edit] Course Guidelines

All the examples should use ISO/IEC 11179 three-part UpperCamelCase notation (ObjectPropertyTerm). This is designed to be consistent with ebXML and NIEM standards. See Data Element Name and Representation term for more information.

[edit] History of the XML Schema

XML Schema is a standard created by the world wide web consortium http://www.w3c.org. Unlike DTDs, XML Schema uses XML file formats to define the XML Schema itself. Once you learn the structure of an XML file, you don't have to learn another syntax.

[edit] What are XML Schemas Used For?

XML Schemas are primarily used to validate the structure and data types of an XML document. By structure we mean that an XML Schema defines

  1. what data elements are expected
  2. what order the data elements are expected in
  3. what nesting these data elements have
  4. what data elements are optional and what data elements are required

XML Schemas can also be used by XML data mapping tools for quickly extracting data from databases and transferring them in XML files.

One of the best analogies is the blueprint analogy. Just like there are architectural blueprints that describe the structural design of a house, an XML Schema provides the "structural design" of a file.

[edit] When XML Schema become inefficient at validating complex rules

Although XML Schemas are excellent at sequential validation of data elements and data types, XML Schema tend to be cumbersome at expressing highly complex business rules. For example when you are at the end of a large file it is difficult to state a rule that checks if a data element has some value that another data element at the beginning of the file should have had another values. This can be done by using XML transforms and XPath expressions

[edit] Structure of an XML Schema Document

XML files are structured as trees of data elements. Each XML file must begin with a XML processor instruction (<?xml version="1.0"?>) and then be followed with a root data element. Unlike the older DTD format, XML Schema files are written in XML format and also have a tree structure.

[edit] The root element: <xs:schema>

All XML Schema files must begin and end with the <xs:schema> tag.

  <?xml version="1.0"?>
  <xs:schema>
  ...other structure here...
  </xs:schema>

The schema MUST end with an </xs:schema> end tag.

[edit] The xs: Prefix

All XML Schema elements should use the "xs:" namespace prefix. Although any prefix can be used, the convention is to use "xs" for XML Schema.

[edit] Elements

Elements are defined in XML Schema using the <element> tag:

  <xs:element name="PersonGivenName" type="xs:string"/>
  <xs:element name="PersonFamilyName" type="xs:string"/>
  <xs:element name="PersonBirthDate" type="xs:date"/>

Here is an example of data that would be validated by the above structures:

  <PersonGivenName>Sue</PersonGivenName>
  <PersonFamilyName>Smith</PersonFamilyName>
  <PersonBirthDate>1990-09-23</PersonBirthDate>

[edit] Attributes

TBD

[edit] Complete Contacts Example

Here is a full example of a complete XML Schema file of personal contacts.

 <?xml version="1.0" encoding="UTF-8"?>
 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 elementFormDefault="qualified"
 attributeFormDefault="unqualified">
    <xs:element name="Contacts">
       <xs:annotation>
          <xs:documentation>File of personal contacts.</xs:documentation>
       </xs:annotation>
       <xs:complexType>
          <xs:sequence maxOccurs="unbounded">
             <xs:element name="Person">
                <xs:complexType>
                   <xs:sequence>
                      <xs:element name="PersonGivenName"/>
                      <xs:element name="PersonFamilyName"/>
                      <xs:element name="PersonBirthDate"/>
                   </xs:sequence>
                </xs:complexType>
             </xs:element>
          </xs:sequence>
       </xs:complexType>
    </xs:element>
 </xs:schema>

[edit] Common XML Schema Primitive Data Types

Type Usage
xsd:anyURI Example <xsd:element name = “url” type = “xsd:anyURI” />
Legal value example http://www.w3.com
xsd:boolean Example <xsd:element name = “hasChildren” type = “xsd:boolean” />
Legal value examples true or false or 1 or 0
xsd:byte Example <xsd:element name = “stdDev” type = “xsd:byte” />
Legal value examples -128 through 127
xsd:date Example <xsd:element name = “dateEst” type = “xsd:date” />
Legal value example 2004-03-15
xsd:dateTime Example <xsd:element name = “xMas” type = “xsd:dateTime” />
Legal value example 2003-12-25T08:30:00
xsd:decimal Example <xsd:element name = “pi” type = “xsd:decimal” />
Legal value example 3.1415292
xsd:double Example <xsd:element name = “pi” type = “xsd:double” />
Legal value example 3.1415292 or INF or NaN
xsd:duration Example <xsd:element name = “MITDuration” type = “xsd:duration” />
Legal value example P8M3DT7H33M2S
xsd:float Example <xsd:element name = “pi” type = “xsd:float” />
Legal value examples 3.1415292 or INF or NaN
xsd:gDay Example <xsd:element name = “dayOfMonth” type = “xsd:gDay” />
Legal value example ---11
xsd:gMonth Example <xsd:element name = “monthOfYear” type = “xsd:gMonth” />
Legal value example --02--
xsd:gMonthDay Example <xsd:element name = “valentine” type = “xsd:gMonthDay” />
Legal value example --02-14
xsd:gYear Example <xsd:element name = “year” type = “xsd:gYear” />
Legal value example 1999
xsd:gYearMonth Example <xsd:element name = “birthday” type = “xsd:gYearMonth” />
Legal value example 1972-08
xsd:ID Example <xsd:attribute name="id" type="xsd:ID"/>
Legal value example id-102
xsd:IDREF Example <xsd:attribute name="version" type="xsd:IDREF"/>
Legal value example id-102
xsd:IDREFS Example <xsd:attribute name="versionList" type="xsd:IDREFS"/>
Legal value example id-102 id-103 id-100
xsd:int Example <xsd:element name = “age” type = “xsd:int” />
Legal value example 77
xsd:integer Example <xsd:element name = “age” type = “xsd:integer” />
Legal value example 77
xsd:long Example <xsd:element name = “cannelNumber” type = “xsd:int” />
Legal value example 214
xsd:negativeInteger Example <xsd:element name = “belowZero” type = “xsd:negativeInteger” />
Legal value example -123
xsd:nonNegativeInteger Example <xsd:element name = “numOfchildren” type = “xsd:nonNegativeInteger” />
Legal value example 2
xsd:nonPositiveInteger Example <xsd:element name = “debit” type = “xsd:nonPositiveInteger” />
Legal value example 0
xsd:positiveInteger Example <xsd:element name = “credit” type = “xsd:positiveInteger” />
Legal value example 500
xsd:short Example <xsd:element name = “numOfpages” type = “xsd:short” />
Legal value example 476
xsd:string Example <xsd:element name = “name” type = “xsd:string” />
Legal value example Joseph
xsd:time Example <xsd:element name = “credit” type = “xsd:time” />
Legal value example 13:02:00

[edit] Summary of XML Schema Elements

Element Explanation
all Specifies that the child elements can appear in any order. Each child element can occur 0 or 1 time
annotation Specifies the top-level element for schema comments
any Enables the author to extend the XML document with elements not specified by the schema
anyAttribute Enables the author to extend the XML document with attributes not specified by the schema
appInfo Specifies information to be used by the application (must go inside annotation)
attribute Defines an attribute
attributeGroup Defines an attribute group to be used in complex type definitions
choice Allows only one of the elements contained in the <choice> declaration to be present within the containing element
complexContent Defines extensions or restrictions on a complex type that contains mixed content or elements only
complexType Defines a complex type element
documentation Defines text comments in a schema (must go inside annotation)
element Defines an element, by default just one ocurrence
extension Extends an existing simpleType or complexType element
field Specifies an XPath expression that specifies the value used to define an identity constraint
group Defines a group of elements to be used in complex type definitions
import Adds multiple schemas with different target namespace to a document
include Adds multiple schemas with the same target namespace to a document
key Specifies an attribute or element value as a key (unique, non-nullable, and always present) within the containing element in an instance document
keyref Specifies that an attribute or element value correspond to those of the specified key or unique element
list Defines a simple type element as a list of values
notation Describes the format of non-XML data within an XML document
redefine Redefines simple and complex types, groups, and attribute groups from an external schema
restriction Defines restrictions on a simpleType, simpleContent, or a complexContent
schema Defines the root element of a schema
selector Specifies an XPath expression that selects a set of elements for an identity constraint
sequence Specifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times
simpleContent Contains extensions or restrictions on a text-only complex type or on a simple type as content and contains no elements
simpleType Defines a simple type and specifies the constraints and information about the values of attributes or text-only elements
union Defines a simple type as a collection (union) of values from specified simple data types
unique Defines that an element or an attribute value must be unique within the scope

[edit] Schema Restrictions and Facets for data types

Constraint Description
enumeration Defines a list of acceptable values
fractionDigits Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero
length Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero
maxExclusive Specifies the upper bounds for numeric values (the value must be less than this value)
maxInclusive Specifies the upper bounds for numeric values (the value must be less than or equal to this value)
maxLength Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero
minExclusive Specifies the lower bounds for numeric values (the value must be greater than this value)
minInclusive Specifies the lower bounds for numeric values (the value must be greater than or equal to this value)
minLength Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero
pattern Defines the exact sequence of characters that are acceptable
totalDigits Specifies the exact number of digits allowed. Must be greater than zero
whiteSpace Specifies how white space (line feeds, tabs, spaces, and carriage returns) are handled

[edit] Instance Document Attributes

Attribute Usage
xsi:nil Explanation Indicates that a certain element does not have a value or that the value is unknown.   The element must be set to nillable inside the schema document:

<xsd:element name=”last_name” type=”xsd:string” nillable=true”/>

Example <full_name xmlns:xsi= ”http://www.w3.org/2001/XMLSchema-instance”>    <first_name>Madonna</first_name>

<last_name xsi:nil=”true”/> </full_name>

xsi:noNamespaceSchemaLocation Explanation Locates the schema for elements that are not in any namespace
Example <radio xsi:noNamespaceSchemaLocation= ”http://www.opentourism.org/xmtext/radio.xsd”>

<!—radio stuff goes here -- > </radio>

xsi:schemaLocation Explanation Locates schemas for elements and attributes that are in a specified namespace
Example <radio xmlns= ”http://www.opentourism.org/xmtext/NS/radio xmlns:xsi= ”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation= ”http://www.arches.uga.eduNS/radio”http://www.opentourism.org/xmtext/radio.xsd”>

<!—radio stuff goes here -- > </radio>

xsi:type Explanation Can be used in instance documents to indicate the type of an element.
Example <height xsi:type=”xsd:decimal”>78.9</height>

[edit] See also

Personal tools
Create a book