Beginning XML 4th Edition
Chapter 5: XML Schemas
Chapter 5 Objectives Benefits of XML Schemas How to create and use XML Schemas How to document your XML Schemas
Benefits of XML Schemas XML Schemas use XML syntax XML Schemas Namespace Support XML Schema Types Simple Complex XML Schema Content Models
The XML Schema Document Example
Try It Out What’s in a Name?
Schema Declarations Example <schema targetNamespace=“URI” attributeFormDefault=“qualified or unqualified” elementFormDefault=“qualified or unqualified” version=“version number”>
The Schema XML Namespace Examples or or
Target Namespaces Example 1 Example 2
Element and Attribute Qualification Qualified John Fitzgerald Doe Qualified and Prefixed John Fitzgerald Doe Unqualified John Fitzgerald Doe
Declarations Example <element name= “name of the element” type=“global type” ref=“global element declaration” form=“qualified or unqualified” minOccurs=“non negative number” maxOccurs=“non negative number or ‘unbounded’” default=“default value” fixed=“fixed value”>
Global versus Local Global declarations are declarations that appear as direct children of the element. Global element declarations can be reused throughout the XML Schema. Local declarations do not have the element as their direct parent and are valid only in their specific context.
Global versus Local Example
Creating a Local Type complexType
Creating a Local Type simpleType
Using a Global Type Example 1 <schema xmlns=" xmlns:target=" targetNamespace=" elementFormDefault="qualified">
Using a Global Type Example 2 <xs:schema xmlns:xs=" xmlns=" targetNamespace=" elementFormDefault="qualified">
Try It Out Creating Reusable Global Types Referring to Global Element Declarations
Naming Elements
Element Qualified Form elementFormDefault Is an attribute that can be set to qualified unqualified
Cardinality It is important to note that the minOccurs and maxOccurs attributes are not permitted within global element declarations. Instead, you should use these attributes within the element references in your content models.
Default and Fixed Values Default Fixed
Element Wildcards <any minOccurs=“non negative number” maxOccurs=“non negative number or unbounded” namespace=“allowable namespaces” processContents=“lax or skip or strict”> ValueDescription ##anyAllows elements from all namespaces to be included as part of the wildcard. ##otherAllows elements from namespaces other than the targetNamespace to be included as part of the wildcard. ##targetNamespaceAllows elements from only the targetNamespace to be included as part of the wildcard. ##localAllows any well-formed elements that are not qualified by a namespace to be included as part of the wildcard. Whitespace-separated list of allowable namespace URIs Allows elements from any listed namespaces to be included as part of the wildcard. Possible list values also include ##targetNamespace and ##local.
Element Wildcards Example <any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
Declarations Example 1 <complexType mixed="true or false" name="Name of complexType"> Example2
Declarations Example <group name=“name of global group”>
Try It Out Using a Global Group
Content Models A declaration A reference to a global declaration An declaration
Declarations Example 1 <sequence minOccurs=“non negative number” maxOccurs=“non negative number or unbounded”> Example 2
Declarations Example 1 <choice minOccurs=“non negative number” maxOccurs=“non negative number or unbounded”> Example 2
References Example 1 <group ref=“ global group definition” minOccurs=“ non negative number” maxOccurs=“ non negative number or unbounded”> Example 2
Declarations First, the declaration must be the only content model declaration that appears as a child of a definition. Secondly, the declaration may contain only declarations as its children. It is not permitted to contain,, or declarations. Finally, the declaration’s children may appear once each in the instance document. This means that within the declaration the values for minOccurs are limited to 0 or 1.
Declaration Example John Fitzgerald Doe or John Doe Fitzgerald
Try It Out Making Contact
Declarations Example <attribute name=“name of the attribute” type=“global type” ref=“global attribute declaration” form=“qualified or unqualified” use=“optional or prohibited or required” default=“default value” fixed=“fixed value”>
Creating a Local Type Example
Using a Global Type Example (no prefix) <schema xmlns=" xmlns:contacts=" targetNamespace=" elementFormDefault="qualified">
Using a Global Type Example (prefix) <xs:schema xmlns:xs=" xmlns=" targetNamespace=" elementFormDefault="qualified">
Attribute Wildcards Example <anyAttribute namespace=“##local processContents=“lax”/>
Attribute Wildcards ValueDescription ##anyAllows attributes from all namespaces to be included as part of the wildcard. ##otherAllows attributes from namespaces other than the targetNamespace to be included as part of the wildcard. ##targetNamespaceAllows attributes from only the targetNamespace to be included as part of the wildcard. ##localAllows attributes that are not qualified by a namespace to be included as part of the wildcard. Whitespace separated list of allowable namespace URIs Allows attributes from any listed namespaces to be included as part of the wildcard. Possible list values also include ##targetNamespace and ##local.
Try It Out Adding Attributes
Declarations Example 1 <attributeGroup name="name of global attribute group"> Example 2
Declarations Example 1 (illegal) Example 2 (illegal)
Try It Out Using a Global Attribute Group
Creating Elements with Simple Content Example (simple content)
Creating Elements with Simple Content and Attributes Example (simple content and attribute)
Data Types TypeDescription stringAny character data normalizedStringA whitespace normalized string where all spaces, tabs, carriage returns, and line feed characters are converted to single spaces tokenA string that does not contain sequences of two or more spaces, tabs, carriage returns, or line feed characters byteA numeric value from -128 to 127 unsignedByteA numeric value from 0 to 255 base64BinaryBase64 encoded binary information hexBinaryHexadecimal encoded binary information integerA numeric value representing a whole number positiveIntegerAn integer whose value is greater than 0
Try It Out Built-in XML Schema Data Types
User-Defined Data Types Example Declarations <simpleType name=“name of the simpleType” final= “#all or list or union or restriction”> There are three primary derived types: *Restriction types *List types *Union types
Declarations Example
Try It Out Creating a Restriction Simple Type
Declarations Example
Declarations Example 1
Declarations Example 2 Valid Unknown Not valid unknown Unknown
Try It Out Contacts – More Simple Types
Creating a Schema from Multiple Documents Declarations <import namespace=“” schemaLocation=“”> Declarations <include schemaLocation=“”>
Try It Out Making Contact – Importing XML Schema Delarations Making Contact – Including XML Schema Declarations
Documenting XML Schemas Comments Attributes from other namespaces Annotations
Comments Example <!-- This complexType allows you to describe a person's name broken down by first, middle and last parts of the name. You can also specify a greeting by including the title attribute. --> <!-- The NameGroup is a global group defined in this XML Schema. -->
Attributes from Other Namespaces Example <schema xmlns=" xmlns:target=" xmlns:doc=" targetNamespace=" elementFormDefault="qualified"> <complexType name="NameType" doc:comments="This complexType allows you to describe a person's name broken down by first, middle and last parts of the name. You can also specify a greeting by including the title attribute."> <group ref="target:NameGroup" doc:comments="The NameGroup is a global group defined in this XML Schema."/>
Annotations Example <schema xmlns=“ targetNamespace=“ xmlns:target=“ elementFormDefault=“qualified”> The name vocabulary was created for a Chapter 2 sample. We have upgraded it to an XML Schema. The appinfo of this <annotation> element points to a sample XML file. The sample should be used only as an example