Topics Covered What is Schema How to use the XML Schema language in applications
Structure
What is it? An XML Schema defines the legal structure that an XML document may take, much like a blueprint. This is accomplished by: defining elements and child elements defining attributes defining the order of child elements defining element content defining data types for elements and attributes defining default and fixed values
Definition and Declaration Definition Create new types (both simple and complex types) Declaration Enable elements and attributes with specific names and types (both simple and complex) to appear in document instances
Schema vs. DTD Schemas are extensible. Schemas have more depth and power. Schemas are written in XML. Schemas support data types. Schemas support namespaces.
Document Type Definition An Information Modeling Syntax a b x y DTD “a is the parent of b. x and y are children of b.” <a> <b> <x>…</x> <y>…</y> </b> </a> Enforces Structure x and y are always interpreted as String, regardless of how they are used Schema vs. DTD
XML Schema Information Modeling Syntax in XML a b x y XML Schema <a> <b> <x>…</x> <y>…</y> </b> </a> Enforces Structure < xsl:element name=“x” type=“ boolean ” /> < xsl:attribute name=“z” type=“integer” /> x and y now have enforceable data types Schema vs. DTD
Comparing Element Declarations Differences in data typing and syntax DTD <!ELEMENT Age (#PCDATA)> XML Schema <element name=“Age” type=“integer”/> XML <Age>2009 </Age>
Why Support Data Types? Easier to describe allowable document content. Easier to validate data. Easier to work with data from a database. Easier to define data restrictions. Easier to define data formats. Easier to convert data between types.
Schema Validation Schemas can be used to validate a document in two ways: Content Model Validation Checks order and nesting of elements (similar to DTD validation) DataType Validation Checks the element content for valid type and range example: month element is an integer between 1 and 12 <month>5</month> VALID!! <month>15</month> INVALID!!
Comparison with DTDs DTDs use their own unique syntax (SGML) XML Schema uses XML syntax DTDs are concise XML Schema is verbose
Comparison with DTDs XML Schema can be parsed and manipulated programmatically like any other XML document DTDs cannot (at least not without a bit of work) Note: Custom DTD parsers are available, but the ability to parse a DTD is not inherent in most XML Parsers. XML Schema enforce Data Typing DTDs cannot (DTDs see everything as Strings)
Comparison with DTDs XML Schema allows open-ended data models Vocabulary extension and inheritance DTDs support only a closed model XML Schema supports attribute groups DTDs offer only limited attribute group support
Comparison with DTDs Schemas are Extensible Reuse Schemas in other Schemas. Create proprietary data types from public types. Reference multiple Schemas in one document.
Comparison with DTDs XML Schema supports namespace integration Allows the association of individual nodes of a document with type declarations in a schema DTDs allow only one association (between the document and the DTD)
Multiple levels of checking BookCatalogue.xml BookCatalogue1.xsd xml-schema.dtd Does the xml document conform to the rules laid out in the xml-schema? Is the xml-schema a valid xml document, i.e., does it conform to the rules laid out in the xml-schema DTD?
Schema Features Rich Datatypes ( integers, time, date, boolean .. ) User-defined types Extendable types Open, closed or refinable content models open: Additional elements may appear refinable : Additional elements may appear if they are defined in the schema Grouping Namespace support
Schemas v. DTDs Schemas DTDs Support namespaces n/a Written in XML syntax n/a Full, object-oriented extensibility Very limited Extensive datatype support Extended via string substitutions Open, closed or refinable content models Closed only
End of DTDs? DTDs have: Widespread use and support Many legacy applications, documents Too much time & money invested Experienced programmers/consultants There is a DTD to define XML Schema NO!
Benefits of XML and Schemas You don’t need to learn a new language. Editor of choice will work on Schemas. Current parser will work on Schemas. You can manipulate your Schema with the XML DOM. Schemas are transformable with XSLT.
Schema Workflow XML Document Normalized XML Document Schema Error message Schema Processor Valid Invalid
XSD (XML Schema Definition) How To Look at this simple XML document called "note.xml": <?xml version="1.0"?> <note> < to>Tom</ to> < from>Jerry</ from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> This is a simple DTD file called "note.dtd" that defines the elements of the XML document above ("note.xml"): <!ELEMENT note (to, from, heading, body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)>
The <schema> element The <schema> is the root element of every XML schema <?xml version="1.0"?> < xsd:schema > ... ... </ xsd:schema > The <schema> element may contain some attributes. A schema declaration often looks something like this : <?xml version="1.0"?> < xsd:schema xmlns:xsd =" http://www.w3.org/2001/XMLSchema " targetNamespace =" http://www.aimit.com " xmlns =" http://www.aimit.com " elementFormDefault =" qualified "> ... ... </ xsd:schema > The schema element must specify the namespace for schemas as its xmlns:xsd attribute
XML <schema> element explained xmlns:xsd =" http:// www.w3.org/2001/XMLSchema” indicates that the elements and data types used in the schema (schema, element, complexType , sequence, string, boolean , etc.) come from the "http://www.w3.org/2001/XMLSchema" namespace. It also specifies that the elements and data types that come from the "http://www.w3.org/2001/XMLSchema" namespace should be prefixed with xsd : targetNamespace ="http:// www.aimit.com " indicates that the elements defined by this schema (note, to, from, heading, body.) come from the "http:// www.aimit.com " namespace. xmlns ="http:// www.aimit.com " indicates that the default namespace is "http:// www.aimit.com ". elementFormDefault ="qualified" indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified.
The XSD document Since the XSD is written in XML, it can get confusing, what we are talking about (XML or XSD?) The file extension is . xsd The root element is <schema> The XSD starts like this: <?xml version="1.0"?> < xs:schema xmlns:xs ="http://www.w3.rg/2001/XMLSchema">
BookStore Book Title Author Date ISBN Publisher http://www.books.org (targetNamespace) Book Namespace ( targetNamespace )
<?xml version="1.0"?> < xsd:schema xmlns:xsd ="http://www.w3.org/2001/XMLSchema" targetNamespace ="http://www.books.org" xmlns ="http://www.books.org" elementFormDefault ="qualified"> < xsd:element name=" BookStore "> < xsd:complexType > < xsd:sequence > < xsd:element ref="Book" minOccurs ="1" maxOccurs ="unbounded"/> </ xsd:sequence > </ xsd:complexType > </ xsd:element > < xsd:element name="Book"> < xsd:complexType > < xsd:sequence > < xsd:element ref="Title" minOccurs ="1" maxOccurs ="1"/> < xsd:element ref="Author" minOccurs ="1" maxOccurs ="1"/> < xsd:element ref="Date" minOccurs ="1" maxOccurs ="1"/> < xsd:element ref="ISBN" minOccurs ="1" maxOccurs ="1"/> < xsd:element ref="Publisher" minOccurs ="1" maxOccurs ="1"/> </ xsd:sequence > </ xsd:complexType > </ xsd:element > < xsd:element name="Title" type=" xsd:string "/> < xsd:element name="Author" type=" xsd:string "/> < xsd:element name="Date" type=" xsd:string "/> < xsd:element name="ISBN" type=" xsd:string "/> < xsd:element name="Publisher" type=" xsd:string "/> </ xsd:schema > This is referencing a Book element declaration. The Book is in which namespace? Since there is no namespace qualifier it is referencing the Book element in the default namespace, which is the targetNamespace ! Thus, this is a reference to the Book element declaration in this schema. The default namespace is http://www.books.org which is the targetNamespace !
<?xml version="1.0"?> < xsd:schema xmlns:xsd ="http://www.w3.org/2001/XMLSchema" targetNamespace ="http://www.books.org" xmlns ="http://www.books.org" elementFormDefault ="qualified" > < xsd:element name=" BookStore "> < xsd:complexType > < xsd:sequence > < xsd:element ref="Book" minOccurs ="1" maxOccurs ="unbounded"/> </ xsd:sequence > </ xsd:complexType > </ xsd:element > < xsd:element name="Book"> < xsd:complexType > < xsd:sequence > < xsd:element ref="Title" minOccurs ="1" maxOccurs ="1"/> < xsd:element ref="Author" minOccurs ="1" maxOccurs ="1"/> < xsd:element ref="Date" minOccurs ="1" maxOccurs ="1"/> < xsd:element ref="ISBN" minOccurs ="1" maxOccurs ="1"/> < xsd:element ref="Publisher" minOccurs ="1" maxOccurs ="1"/> </ xsd:sequence > </ xsd:complexType > </ xsd:element > < xsd:element name="Title" type=" xsd:string "/> < xsd:element name="Author" type=" xsd:string "/> < xsd:element name="Date" type=" xsd:string "/> < xsd:element name="ISBN" type=" xsd:string "/> < xsd:element name="Publisher" type=" xsd:string "/> </ xsd:schema > This is a directive to any instance documents which conform to this schema: Any elements that are defined in this schema must be namespace-qualified when used in instance documents.
Other Attributes Attribute Description id Optional. Creates a n unique ID for the element. attributeFormDefault Optional. The form for attributes declared in target name space of the schema. Must be either “qualified” or “unqualified” elementFormDefault Optional. The form for elements declared in the target name space of the schema. Must either be “qualified” or “unqualified” blockDefault Optional. Specifies the default value of the block attribute on element and complexType elements in namespace. finalDefault Optional. Specifies the default value of the final attribute on element, simpleType, and complexType elements in the namespace. targetNamespace Optional. An URI ref to the namespace of this schema. version Optional. Specifies the version of the schema. xmlns A URI ref specifying one or more namespaces for the schema. any attributes Optional. Specifies other attributes with non-schema namespace.
Referencing a schema in an XML instance document <?xml version="1.0"?> < BookStore xmlns ="http://www.books.org" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://www.books.org BookStore.xsd" > <Book> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>July, 1998</Date> <ISBN>94303-12021-43892</ISBN> < Publisher>McMillan Publishing</Publisher> </Book> ... </ BookStore > 1. First, using a default namespace declaration, tell the schema- validator that all of the elements used in this instance document come from the http://www.books.org namespace. 2. Second, with schemaLocation tell the schema- validator that the http://www.books.org namespace is defined by BookStore.xsd (i.e., schemaLocation contains a pair of values ). 3. Third, tell the schema- validator that the schemaLocation attribute we are using is the one in the XML Schema-instance namespace. 1 2 3
schemaLocation type noNamespaceSchemaLocation http://www.w3.org/2001/XMLSchema-instance XMLSchema -instance Namespace nil
Referencing a schema in an XML instance document BookStore.xml BookStore.xsd targetNamespace="http://www.books.org" schemaLocation="http://www.books.org BookStore.xsd" - defines elements in namespace http://www.books.org - uses elements from namespace http://www.books.org A schema defines a new vocabulary. Instance documents use that new vocabulary.
A Reference to a DTD - Comparison <? xml version="1.0"?> <!DOCTYPE note SYSTEM "http :// www.aimit.com/dtd/note.dtd "> <note> < to>Tom</ to> < from>Jerry</ from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
A Reference to an XML Schema … <?xml version="1.0"?> <note xmlns =" http:// www.aimit.com " xmlns:xsi =" http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation =" http:// www.aimit.com note.xsd "> < to>Tom</ to> < from>Jerry</ from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
A Reference to an XML Schema xmlns ="http:// www.aimit.com " specifies the default namespace declaration. This declaration tells the schema- validator that all the elements used in this XML document are declared in the "http:// www.aimit.com " namespace. xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" XML Schema Instance namespace The X ML S chema I nstance reference is required xsi:schemaLocation ="http ://www.aimit.com note.xsd" this schemaLocation attribute has two values. The first value is the namespace to use. The second value is the location of the XML schema to use for that namespace
Cross-Referencing XML with Schemas File “Person.xml” File “Person.xsd” <?xml version="1.0" encoding="UTF-8"?> < Person xmlns:xsi =" http://www.w3.org/ 2001/ XMLSchema -instance " xsi:noNamespaceSchemaLocation =" Person.xsd "> < First > Katrina </ First > < Last > Khaif </ Last > < Age > 2 4 </ Age > </ Person > <?xml version="1.0" encoding="UTF-8"?> < xs:schema xmlns:xs =" http://www.w3.org/2001/ XMLSchema "> < xs:element name =" Person "> < xs:complexType > < xs:sequence > < xs:element name =" First “ type =" xs:string "/> < xs:element name =" Middle “ type =" xs:string “ minOccurs =" "/> < xs:element name =" Last “ type =" xs:string "/> < xs:element name =" Age “ type =" xs:integer "/> </ xs:sequence > </ xs:complexType > </ xs:element > </ xs:schema >
Qualify XMLSchema , Default targetNamespace In the first example, we explicitly qualified all elements from the XML Schema namespace. The targetNamespace was the default namespace. BookStore Book Title Author Date ISBN Publisher http://www.books.org ( targetNamespace ) http://www.w3.org/2001/XMLSchema element complexType schema sequence string integer boolean
Default XMLSchema , Qualify targetNamespace Alternatively (equivalently), we can design our schema so that XMLSchema is the default namespace. BookStore Book Title Author Date ISBN Publisher http://www.books.org (targetNamespace) http://www.w3.org/2001/XMLSchema element complexType schema sequence string integer boolean
<?xml version="1.0"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns:bk="http://www.books.org" elementFormDefault="qualified"> <element name="BookStore"> <complexType> <sequence> <element ref="bk:Book" minOccurs="1" maxOccurs="unbounded"/> </sequence> </complexType> </element> <element name="Book"> <complexType> <sequence> <element ref="bk:Title"/> <element ref="bk:Author"/> <element ref="bk:Date"/> <element ref="bk:ISBN"/> <element ref="bk:Publisher"/> </sequence> </complexType> </element> <element name="Title" type="string"/> <element name="Author" type="string"/> <element name="Date" type="string"/> <element name="ISBN" type="string"/> <element name="Publisher" type="string"/> </schema> Here we are referencing a Book element. Where is that Book element defined? In which namespace ? The bk : prefix indicates what namespace this element is in. bk : has been set to be the same as the targetNamespace .
" bk :" References the targetNamespace BookStore Book Title Author Date ISBN Publisher http://www.books.org (targetNamespace) http://www.w3.org/2001/XMLSchema bk element complexType schema sequence string integer boolean Consequently, bk:Book refers to the Book element in the targetNamespace.
XML Abstract Data Model The XML Abstract Data Model composes of Schema Components . is used to describe XML Schemas . Schema Component is the generic term for the building blocks that compose the abstract data model of the schema .
13 Kinds of Schema Components Simple type definitions Complex type definitions Attribute declarations Element declarations Attribute group definitions Identity-constraint definitions Model group definitions Notation declarations Annotations Model groups Particles Wildcards Attribute Uses Secondary Primary Helper
Relationships among Schema Components
Primary Schema Components XML Namespaces Element Declarations Attribute Declarations Simple Type Definitions Complex Type Definitions
XML Schema (. xsd ) XML document & XML Schema Information Items … Elements Attributes Declaration Type Definition XML Document (.xml)
Declaration & Definition Declaration Components are associated by (qualified) names to information items being validated. It is like declaring objects in OOP. Definition Components define internal schema components that can be used in other schema components. Type definition is like defining classes in OOP .
Definitions vs. Declarations A definition describes a complex or simple type that either contains element or attribute declarations or references element or attribute declarations defined elsewhere in the document. Here’s a definition: < xsd:complexType name=” bookType ”> < xsd:sequence > < xsd:element name=”title” type =” xsd:string ”/> < xsd:element name=”author” type =” xsd:string ”/> < xsd:element name=”description” type =” xsd:string ”/> </ xsd:sequence > </ xsd:complexType > A declaration defines an element or attribute, specifying the name and datatype for the component. Here’s a declaration : < xsd:element name=”book”> < xsd:complexType > < xsd:sequence > < xsd:element name=”title” type =” xsd:string ”/> < xsd:element name=”author” type =” xsd:string ”/> < xsd:element name=”description” type =” xsd:string ”/> </ xsd:sequence > </ xsd:complexType > </ xsd:element >
Consider XML Instance Document Example < book isbn=" 0-7356-1465-2 "> <title> XML Step by Step </title> < author > Michael young </ author > <publisher> Microsoft press </publisher > </book>
Declaration vs. Definition Declarations Enable elements and attributes with specific names and types (both simple and complex) to appear in document instances. < xsd:element name=" purchaseOrder " type=" PurchaseOrderType "/> < xsd:element name=" comment " type=" xsd:string "/> Definitions create new types (both simple and complex) < xsd:complexType name=" PurchaseOrderType "> < xsd:sequence > < xsd:element name=" shipTo " type="Address"/> < xsd:element name=" billTo " type="Address"/> < xsd:element ref=" comment " minOccurs ="0"/> < xsd:element name="items" type="Items"/> </ xsd:sequence > < xsd:attribute name=" orderDate " type=" xsd:date "/> </ xsd:complexType >
Type Definitions Why Type Definitions? Simple Type Definition VS. Complex Type Definition Attributes & Elements without element children Complex Type Definition Simple Type Definition Elements
“Simple” and “Complex ” elements A “simple” element is one that contains text and nothing else A simple element cannot contain other elements A simple element cannot be empty However, the text can be of many different types, and may have various restrictions applied to it If an element isn’t simple, it’s “complex” A complex element may have attributes A complex element may be empty, or it may contain text, other elements, or both text and other elements
Defining a simple element A simple element is defined as < xs:element name=" name " type=" type " /> where: name is the name of the element the most common values for type are xs:boolean xs:integer xs:date xs:string xs:decimal xs:time Other attributes a simple element may have: default=" default value " if no other value is specified fixed=" value " no other value may be specified
Examples of Simple element using Built-in Simple Types < xsd:element name=“weight” type=“ xsd:string ”/> < xsd:element name=“population” type=“ xsd:integer ” />
Declare Default and Fixed Values for Simple Elements Simple elements can have a default value OR a fixed value set. A default value is automatically assigned to the element when no other value is specified. In the following example the default value is "red": < xs:element name="color" type=" xs:string " default="red"/> A fixed value is also automatically assigned to the element. You cannot specify another value. In the following example the fixed value is "red": < xs:element name="color" type=" xs:string " fixed="red"/>
Primitive Type XML Schema has a complex system of types . Different types may describe: the allowed values of attributes, the allowed content of elements, or the allowed content and the allowed attributes of elements. There is a subset of types, called the simple types , that can be used in either of the first two roles.
Simple Types Built-in simple type Description Example(s) xsd:string A sequence of any of the legal XML characters This is a string. xsd:boolean The value true or false, or 1 or 0 (indicating true or false, respectively) true false 1 xsd:decimal A number that may contain a decimal component -5.2 -3.0 1 2.5 xsd:integer A whole number - 389 -7 5 229
Simple Types Built-in simple type Description Example(s) xsd:positivelnteger A positive whole number(not including 0) 5 229 xsd:negativelnteger A negative whole number (not including 0) -389 -7 xsd:date A calendar date, represented as CCYY-MM-DD 1948-05-21 2001-10-15
Simple Types Built-in simple type Description Example(s) xsd:time A time of day, represented as hh:mm:ss.ss 11:30:00.00(11:30 A.M.) 14:29:03 (2:29 P.M. and 3 seconds) 05:16:00.0(5:16 A.M.) xsd:dateTime A date and time of day, represented as CCYY-MM-DD T hh:mm:ss.ss 1948-05-21 T 17:28:00.00 xsd:gMonth A Gregorian calendar month, represented as - MM- -05- (May) -12- (December) xsd:gYear A Gregorian calendar year, represented as CCYY 1948 2001
Simple Types Built-in simple type Description Example(s) xsd:gDay A day of a Gregorian calendar month, represented as -DD -05 -31 xsd:gYearMonth A Gregorian calendar year and month, represented as CCYY-MM 1948-05 (May, 1948) xsd:anyURI A URI (Uniform Resource Identifier). http:// www.myOnline.com
XSD Complex Elements A complex element contains other elements and/or attributes. empty elements elements that contain only other elements elements that contain only text elements that contain both other elements and text <employee> < firstname >Mani</ firstname > < lastname > Dsouza </ lastname > </employee>
Example of complex elements A complex XML element, "product", which is empty: <product pid ="1345"/> A complex XML element, "employee", which contains only other elements: <employee> < firstname > Salman </ firstname > < lastname >Khan</ lastname > </employee > A complex XML element, "food", which contains only text: <food type =“Vanilla"> Ice cream</food> A complex XML element, "description", which contains both elements and text: <description> It happened on <date lang =" norwegian ">03.03.99 </date> .... </description
Elements can be declared in two ways < xsd:element name=" name " type=" type " minOccurs =" int " maxOccurs =" int "/> A simple type (e.g., xsd:string ) or the name of a complexType (e.g., BookPublication ) < xsd:element name=" name " minOccurs =" int " maxOccurs =" int "> < xsd:complexType > … </ xsd:complexType > </ xsd:element > 1 2 A nonnegative integer A nonnegative integer or "unbounded" Note: minOccurs and maxOccurs can only be used in nested (local) element declarations.
Note that: < xsd:element name="A" type="xyz"/> < xsd:complexType name="xyz"> < xsd:sequence > < xsd:element name="B" …/> < xsd:element name="C" …/> </ xsd:sequence > </ xsd:complexType > is equivalent to: < xsd:element name="A"> < xsd:complexType > < xsd:sequence > < xsd:element name="B" …/> < xsd:element name="C" …/> </ xsd:sequence > </ xsd:complexType > </ xsd:element > Element A references the complexType xyz. Element A has the complexType definition inlined in the element declaration.
Type Attribute or complexType Child Element, but not Both! An element declaration can have a type attribute, or a complexType child element, but it cannot have both a type attribute and a complexType child element. < xsd:element name="A" type ="xyz"> < xsd: complexType > … </ xsd:complexType > </ xsd:element >
Defining an attribute Attributes are always declared as simple types An attribute is defined as < xs:attribute name=" name " type=" type " /> where: name and type are the same as for xs:element Other attributes a simple element may have: default=" default value " if no other value is specified fixed=" value " no other value may be specified
Example: Attribute declaration <element name =“ StdRecord ”> < complexType > <sequence> <element name =“ SName ” type=“string”/> <element name =“Course” type=“string”/> <element name =“Gender” type=“token”/> <element name=“ DateOfBirth ” type=“date”/> </sequence> <attribute name =“ RegNo ” type=“integer”/> </ complexType > </element> Instance Document < Record RegNo =“420”> < SName >Shakti Kapoor </ SName > <Course>MS</Course> <Gender>M</Gender> < DateOfBirth >1963-06-01</ DateOfBirth > </Record>
Attributes Simple elements cannot have attributes. If an element has attributes, it is considered to be of complex type. The attribute itself is always declared as a simple type.. Example: An XML element with an attribute < lastname lang ="EN ">Khan</ lastname > A corresponding simple attribute definition: < xs:attribute name=" lang " type=" xs:string "/>
Declare Default and Fixed Values for Attributes Attributes can have a default value OR a fixed value specified A default value is automatically assigned to the attribute when no other value is specified. In the following example the default value is "EN": < xs:attribute name=" lang " type=" xs:string " default="EN"/> A fixed value is also automatically assigned to the attribute. You cannot specify another value. In the following example the fixed value is "EN": < xs:attribute name=" lang " type=" xs:string " fixed="EN"/>
XML Schema Types Simple types Basic datatypes Can be used for attributes and element text Extendable Complex types Defines structure of elements Extendable Types can be named or “anonymous”
The Simple Type: < simpleType > The Simplest Type Declaration: < simpleType name = “ FirstName ” type = “string”/> Based on a primitive or the derived built-in datatypes Cannot contain sub-elements or attributes Can declare constraining properties (“facets”) minLength , maxLength , Length, etc May be used as base type of a complexType
Simple Types Simple Types are of three varieties: Atomic: Built-in or derived, e.g. < xsd:simpleType name=" myInteger "> < xsd:restriction base=" xsd:integer "> < xsd:minInclusive value="10000"/> < xsd:maxInclusive value="99999"/> </ xsd:restriction > </ xsd:simpleType > List: multiple items of the same type < listOfMyInt >20003 15037 95977 95945</ listOfMyInt > Union: Union or two or more Simple Types
XML Schema Datatypes Two kinds of datatypes : Built-in and User-defined Built-in Primitive Datatypes string , double , duration , etc Derived Datatypes : CDATA , integer , date , byte , etc Derived from the primitive types Example: integer is derived from decimal User-defined Derived from built-in or other user-defined datatypes specify constraints on an existing type (the base type) Constraints are given in terms of facets totalDigits , maxInclusive , etc.
Date and Time Data Types Code.xsd <xsd:element name=“gestation” type=“xsd:timeDuration”/> (represent a certain amount of time) Code.xml <gestation>P3M15D</gestation> (PnYnMnDTnHnMnS) Period n - non negative T begins optional time section
Code.xsd xsd:element name=“bedtime” type=“xsd.time:/> Code.xml <bedtime>20:15:05-05:00>/bedtime> Date and Type Data Types
Built-in Datatypes (cont.) Primitive Datatypes gDay gMonth hexBinary base64Binary anyURI QName NOTATION Atomic, built-in format: --- DD format: -- MM a hex string a base64 string http://www.aimit.ac.in a namespace qualified name a NOTATION from the XML spec
Built-in Datatypes (cont.) Derived types normalizedString token language IDREFS ENTITIES NMTOKEN NMTOKENS Name NCName ID IDREF ENTITY integer nonPositiveInteger Subtype of primitive datatype A string without tabs, line feeds, or carriage returns String w/o tabs, l/f, leading/trailing spaces, consecutive spaces any valid xml:lang value, e.g., EN , FR , ... must be used only with attributes must be used only with attributes must be used only with attributes must be used only with attributes part (no namespace qualifier) must be used only with attributes mmust be used only with attributes ust be used only with attributes 456 negative infinity to 0
Built-in Datatypes (cont.) Derived types negativeInteger long int short byte nonNegativeInteger unsignedLong unsignedInt unsignedShort unsignedByte positiveInteger Subtype of primitive datatype negative infinity to -1 -9223372036854775808 to 9223372036854775807 -2147483648 to 2147483647 -32768 to 32767 -127 to 128 0 to infinity to 18446744073709551615 to 4294967295 to 65535 to 255 1 to infinity Note: the following types can only be used with attributes : ID, IDREF, IDREFS, NMTOKEN, NMTOKENS, ENTITY, and ENTITIES.
Restrictions Restrictions are used to define acceptable values for XML elements or attributes. When used with XML elements they are known as “ facets ”.
Creating your own Datatypes A new datatype can be defined from an existing datatype (called the "base" type) by specifying values for one or more of the optional facets for the base type .
General Form of Creating a New Datatype by Specifying Facet Values < xsd:simpleType name= " name "> < xsd:restriction base= " xsd: source "> < xsd: facet value = " value "/> < xsd: facet value = " value "/> … </ xsd:restriction > </ xsd:simpleType > Facets: - length - minlength - maxlength - pattern - enumeration - minInclusive - maxInclusive - minExclusive - maxExclusive ... Sources: - string - boolean - number - float - double - duration - dateTime - time ...
Restrictions Constraint Description enumeration Defines a list of acceptable values. fractionDigits Specifies the maximum number of deicmal places allowed. Must be equal to or greater than zero. length Specifies the exact number of characters or list items allowed. Must be absolute. maxExclusive Specifies the upper bounds for numeric values. maxInclusive Specifies the upper bounds for numeric values. maxLength Specifies the maximum number of characters or list items allowed. Must be absolute. minExclusive Specifies the lower bounds for numeric values. minInclusive Specifies the lower bounds for numeric values. minLength Specifies the minimum number of characters or list items allowed. Must be absolute.
Restrcitions Constraint Description 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 is handled.
Example of Creating a New Datatype by Specifying Facet Values < xsd:simpleType name=" TelephoneNumber "> < xsd:restriction base=" xsd:string "> < xsd:length value =“15/> < xsd:pattern value="\ d{4}-\d{10}"/> </ xsd:restriction > </ xsd:simpleType > 1. This creates a new datatype called ' TelephoneNumber '. 2. Elements of this type can hold string values, 3. But the string length must be exactly 15 characters long and 4. The string must follow the pattern: dddd-dddddddddd , where 'd' represents a 'digit'. (Obviously, in this example the regular expression makes the length facet redundant.) 1 2 3 4
Creating your own Datatypes The string primitive datatype has six optional facets: length minLength maxLength pattern enumeration whitespace (legal values: preserve, replace, collapse)
Length The facets length , minLength , maxLength allow to constrain the length of an item like a string (also allow to constrain the number of items in a list type ). Values of length , minLength , minLength should be non-negative integers. Example: < xsd:simpleType name="state"> < xsd:restriction base=" xsd:string "> < xsd:length value="2"/> </ xsd:restriction > </ xsd:simpleType > defines a type state representing strings containing exactly two characters. These facets supported by all primitive types other than numeric and date- and time-related types . Also supported by list types.
White Space This facet controls how white space in a value received from the parser is processed, prior to Schema validation. It can take three values: preserve : no white space processing, beyond what base XML does. replace : Convert every white space character (Line Feed, etc ) to a space character ( #x20 ). collapse : Like replace . All leading or trailing spaces are then removed. Also sequences of spaces are replaced by single spaces. Note analogies to “ Normalization of Attribute Values ” in base XML. All simple types except union types have this attribute, but usually you don’t explicitly set it in restriction : just inherit values from built in types. All built in types have collapse , except string which has preserve and normalizedString which has replace .
Pattern Perhaps the most powerful facet is pattern, which allows to specify a regular expression: any allowed value must satisfy the pattern of this expression. Example < xsd:simpleType name="weekday"> < xsd:restriction base=" xsd:string "> < xsd:pattern value="( Mon|Tues|Wednes|Thurs|Fri )day"/> </ xsd:restriction > </ xsd:simpleType > defines a type weekday representing the names of the week days.
Regular Expressions XML Schema has its own notation for regular expressions, but very much based on the corresponding Perl notation. For the most part Schema use a subset of the Perl 5 grammar for regular expressions. Includes most of the purely “declarative” features from Perl regular expressions, but omits many “procedural” features related to search, matching algorithm, substitution, etc.
Metacharacters The following characters, called metacharacters , have special roles in Schema regular expressions: . \ ? * + | { } ( ) [ ] To match these characters literally in patterns, must escape them with \, e.g.: The pattern “2\+2” matches the string “2+2”. The pattern “f\(x\)” matches the string “f(x)”.
Escape Sequences \n \r \t \\ \| \- \^ \? \* \+ \{ \} \( \) \[ \] linefeed carriage return tab The backward slash \ The vertical bar | The hyphen - The caret ^ The question mark ? The asterisk * The plus sign + The open curly brace { The close curly brace } The open paren ( The close paren ) The open square bracket [ The close square bracket ]
Escape Sequences In general one should use XML character references to include hard-to-type characters. But for convenience Schema regular expressions allow: \n matches a newline character (same as &# xA ;) \r matches a carriage return character (same as &# xD ;) \t matches a tab character (same as 	)
Escape Sequences All other escape sequences (except \- and \^, used only in character class expressions) match any single character out of some set of possible values. For example \d matches any decimal digit, so the pattern “Boeing \d\d\d” matches the strings “Boeing 747”, “Boeing 777”, etc.
Multicharacter Escapes The simplest patterns matching classes of characters are: . matches any character except carriage return or newline. \d matches any decimal digit. \s matches any white space character. \i matches any character that can start an XML name. \c matches any character that can appear in an XML name. \w matches any “word” character (excludes punctuation, etc.)
Multicharacter Escapes The escapes \D, \S, \I, \C and \W are negative forms, e.g. \D matches any character except a decimal digit.
Category Escapes A large and interesting family of escapes is based on the Unicode standard. General form is \p{ Name } where Name is a Unicode-defined class name. The negative form \P{ Name } matches any character not in the class . Simple examples include: \p{L} (any letter), \p{Lu} (upper case letters), \p{ Ll } (lower case letters), etc. More interesting cases are based on the Unicode block names for alphabets, e.g.: \p{ IsBasicLatin } , \p{IsLatin-1Supplement} , \p{ IsGreek } , \p{ IsArabic } , \p{ IsDevanagari } , \p{ IsHangulJamo } , \p{ IsCJKUnifiedIdeographs } , etc , etc , etc.
Category Escapes \p{L} \p{Lu} \p{ Ll } \p{N} \p{ Nd } \p{P} \p{ Sc } A letter, from any language An uppercase letter, from any language A lowercase letter, from any language A number - Roman, fractions, etc A digit from any language A punctuation symbol A currency sign, from any language
Character Class Expressions Allow you to define terms that match any character from a custom set of characters. Basic syntax is familiar from Perl and UNIX: [ List-of-characters ] or the negative form: [^ List-of-characters ] Here List-of-characters can include individual characters, and also ranges of the form First - Last where First and Last are characters.
Character Class Expressions Examples: [RGB] matches one of R , G , or B . [0-9A-F] or [\ dA -F] match one of , 1 , …, 9 , A , B ,…, F . [^\r\n] matches anything except CR, NL (same as . ).
Class Subtractions A feature of XML Schema, not present in Perl 5. A class character expression can take the form: [ List-of-characters - Class-char- expr ] or: [^ List-of-characters - Class-char- expr ] where Class-char- expr is another class character expression. Example: [a- zA -Z-[ aeiouAEIOU ]] matches any consonant in the Latin alphabet.
Sequences and Alternatives Finally, the universal core of regular expressions. If Pattern 1 and Pattern 2 are regular expressions, then: Pattern 1 Pattern 2 matches any string made by putting a string accepted by Pattern 1 in front of a string accepted by Pattern 2 . Pattern 1 | Pattern 2 matches any string that would be accepted by Pattern 1 , or any string accepted by Pattern 2 . Parentheses just group things together: ( Pattern 1 ) matches any string accepted by Pattern 1 .
Sequences and Alternatives An example given earlier: ( Mon|Tues|Wednes|Thurs|Fri )day matches any of the strings Monday , Tuesday , Wednesday , Thursday , or Friday . Equivalent to Monday|Tuesday|Wednesday|Thursday|Friday .
Restriction Example - Series < xs:element name="initials"> < xs:simpleType > < xs:restriction base=" xs:string "> < xs:pattern value="[a- zA -Z][a- zA -Z][a- zA -Z]"/> </ xs:restriction > </ xs:simpleType > </ xs:element >
Quantifiers If Pattern 1 is a regular expression: Pattern 1 ? matches the empty string or any string accepted by Pattern 1 . Pattern 1 + matches any string accepted by Pattern 1 , or by Pattern 1 Pattern 1 , or by Pattern 1 Pattern 1 Pattern 1 , or … Pattern 1 * matches the empty string or any string accepted by Pattern 1 + .
Regular Expressions Regular Expression Chapter \d Chapter  \d Chapter\s\d a*b [ xyz]b a?b a+b [a-c]x [-ac]x [ac-]x [^0-9]x \ Dx Example Chapter 1 Chapter 1 Chapter followed by a blank followed by a digit b , ab , aab , aaab , … xb , yb , zb b , ab ab , aab , aaab , … ax, bx , cx -x, ax, cx ax, cx, -x any non-digit char followed by x any non-digit char followed by x
Quantifiers If n , m are numbers, XML Schema also allow the shorthand forms: Pattern 1 { n } is equivalent to Pattern 1 repeated n times. Pattern 1 { m , n } matches any string accepted by Pattern 1 repeated m times or m + 1 times or … or n times. Pattern 1 { m ,} matches any string accepted by Pattern 1 repeated m or more times.
Regular Expressions (cont.) Regular Expression ( ho){2} there (ho\s){2} there . abc ( a|b )+ x a{1,3}x a{2 ,} x \w\s\w Example hoho there ho ho there any ( one ) char followed by abc ax, bx , aax , bbx , abx , bax ,... ax , aax , aaax aax , aaax , aaaax , … word character (alphanumeric plus dash) followed by a space followed by a word character
Regular Expressions (cont.) [a- zA -Z-[ Ol ]]* A string comprised of any lower and upper case letters, except "O" and "l" \. The period "." (Without the backward slash the period means "any character")
Example of Derived Simple Type (Regular expression) < xsd:simpleType name=“ ProductKey "> < xsd:restriction base=" xsd:string "> < xsd:pattern value="\d{3}-[A-Z]{2}"/> </ xsd:restriction > </ xsd:simpleType >
Restrictions – Password Example < xs:element name="password"> < xs:simpleType > < xs:restriction base=" xs:string "> < xs:pattern value="[a-zA-Z0-9]{8}"/> </ xs:restriction > </ xs:simpleType > </ xs:element > This element must contain a literal string composed of upper or lower case letters, or numerals. It must have 8 characters in total.
Using Patterns in Restriction All simple types (including lists and enumerations) support the pattern facet, e.g .: < simpleType name=“ multiplesOfFive "> <restriction base=" xs:integer "> <pattern value=“[+-]?\d*[05]"/> </restriction> </ simpleType > defines a subtype of integer including all numbers ending with digits or 5 .
Example R.E. [1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5] 0 to 99 100 to 199 200 to 249 250 to 255 This regular expression restricts a string to have values between 0 and 255. … Such a R.E. might be useful in describing an IP address ...
IP Datatype Definition < xsd:simpleType name="IP"> < xsd:restriction base=" xsd:string "> < xsd:pattern value="(([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3} ([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"> < xsd:annotation > < xsd:documentation > Datatype for representing IP addresses. Examples, 129.83.64.255, 64.128.2.71, etc. This datatype restricts each field of the IP address to have a value between zero and 255, i.e., [0-255].[0-255].[0-255].[0-255] Note: in the value attribute (above) the regular expression has been split over two lines. This is for readability purposes only. In practice the R.E. would all be on one line. </ xsd:documentation > </ xsd:annotation > </ xsd:pattern > </ xsd:restriction > </ xsd:simpleType >
Regular Expressions (concluded) \p{L} \p{Lu} \p{Ll} \p{N} \p{Nd} \p{P} \p{Sc} A letter, from any language An uppercase letter, from any language A lowercase letter, from any language A number - Roman, fractions, etc A digit from any language A punctuation symbol A currency sign, from any language <xsd:simpleType name="money"> <xsd:restriction base="xsd:string"> <xsd:pattern value=" \p{Sc}\p{Nd}+(\.\p{Nd}\p{Nd})? "/> </xsd:restriction> </xsd:simpleType> <xsd:element name="cost" type="money"/> "currency sign from any language , followed by one or more digits from any language , optionally followed by a period and two digits from any language " <cost>$45.99</cost> <cost>¥300</cost>
Enumeration The enumeration facet allows one to select a finite subset of allowed values from a base type, e.g.: < xsd:simpleType name="weekday"> < xsd:restriction base=" xs:string "> < xsd:enumeration value="Monday"/> < xsd:enumeration value="Tuesday"/> < xsd:enumeration value="Wednesday"/> < xsd:enumeration value="Thursday"/> < xsd:enumeration value="Friday"/> </ xsd:restriction > </ xsd:simpleType > Behaves like a very restricted version of pattern ? All primitive types except boolean support the enumeration facet. List and union types also support this facet.
Example of Derived Simple Type 3 (Enumeration) < xsd:simpleType name="State"> < xsd:restriction base=" xsd:string "> < xsd:enumeration value="AP"/> < xsd:enumeration value=“KA"/> < xsd:enumeration value="TN/> <!-- and so on ... --> </ xsd:restriction > </ xsd:simpleType > enumeration facet limits a simple type to a set of distinct values
Enumeration An enumeration restricts the value to be one of a fixed set of values Example: < xs:element name="season"> < xs:simpleType > < xs:restriction base=" xs:string "> < xs:enumeration value="Spring"/> < xs:enumeration value="Summer"/> < xs:enumeration value="Autumn"/> < xs:enumeration value="Fall"/> < xs:enumeration value="Winter"/> </ xs:restriction > </ xs:simpleType > </ xs:element >
Another Example < xsd:simpleType name="shape"> < xsd:restriction base=" xsd:string "> < xsd:enumeration value="circle"/> < xsd:enumeration value="triangle"/> < xsd:enumeration value="square"/> </ xsd:restriction > </ xsd:simpleType > This creates a new type called shape. An element declared to be of this type must have either the value circle, or triangle, or square.
Using Patterns in Restriction The pattern facet can appear more than once in a single restriction : interpretation is as if patterns were combined with | . Conversely if the pattern facet is specified in restriction of a base type that was itself defined using a pattern , allowed values must satisfy both patterns.
Multiple Facets - " and " them together, or " or " them together? An element declared to be of type TelephoneNumber must be a string of length=8 and the string must follow the pattern: 3 digits, dash, 4 digits. <xsd:simpleType name="shape"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="circle"/> <xsd:enumeration value="triangle"/> <xsd:enumeration value="square"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="TelephoneNumber"> <xsd:restriction base="xsd:string"> <xsd:length value="8"/> <xsd:pattern value="\d{3}-\d{4}"/> </xsd:restriction> </xsd:simpleType> An element declared to be of type shape must be a string with a value of either circle, or triangle , or square. Patterns, enumerations => "or" them together All other facets => "and" them together
Numerical - Facets The facets maxInclusive , maxExclusive , minExclusive , minInclusive are supported only by numeric and date- and time-related types, and define bounds of value ranges. The facets totalDigits , fractionDigits are defined for the primitive type decimal , and thus all numeric types derived from decimal , and for no other types.
Facets of the integer Datatype The integer datatype has 8 optional facets: totalDigits pattern whitespace enumeration maxInclusive maxExclusive minInclusive minExclusive
Restrictions on numbers minInclusive -- number must be ≥ the given value minExclusive -- number must be > the given value maxInclusive -- number must be ≤ the given value maxExclusive -- number must be < the given value totalDigits -- number must have exactly value digits fractionDigits -- number must have no more than value digits after the decimal point
A Simple Type Example (1 of 4) Integer with value ( 1234 , 5678 ] < xsd:simpleType name=‘ MyInteger ’> < xsd:restriction base=‘ xsd:integer ’> < xsd: minExclusive value =‘1234’/> < xsd: maxInclusive value =‘5678’/> </ xsd:restriction > </ xsd:simpleType >
A Simple Type Example (2 of 4) Integer with value [ 1234 , 5678 ) < xsd: simpleType name=‘ MyInteger ’> < xsd:restriction base=‘ xsd:integer ’> < xsd: minInclusive value=‘1234’/> < xsd: maxExclusive value=‘5678’/> </ xsd:restriction > </ xsd:simpleType >
A Simple Type Example (3 of 4) Integer with value [ 1234 , 5678 ] < xsd:simpleType name=‘ MyInteger ’> < xsd:restriction base=‘ xsd:integer ’> < xsd: minInclusive value=‘ 1234 ’/> < xsd: maxInclusive value=‘ 5678 ’/> </ xsd:restriction > </ xsd:simpleType >
Fixing a Facet Value Sometimes when we define a simpleType we may like to have a unchanging value for one (or more) facet. That is, we want to make the facet a constant. < xsd:simpleType name= " ClassSize "> < xsd:restriction base=" xsd:nonNegativeInteger "> < xsd:minInclusive value="10" fixed="true" /> < xsd:maxInclusive value="60"/> </ xsd:restriction > </ xsd:simpleType > simpleTypes which derive from this simpleType may not change this facet .
List Type We define a type representing a white-space-separated list of items using the list element. Comprised of sequences of atomic simple types A list value is split according to its white space content prior to validation of the items in the list Three built-in list types NMTOKENS, IDREFS, ENTITIES User defined List type Derive from atomic types Facets length, minLength , maxLength , enumeration.
Example of List Type The xsd:list element creates a new simple type that allows the element to contain a sequence of values of the base type, which are separated with white space characters (spaces, tabs, or line breaks ). Schema < xsd:simpleType name=" listOfMyIntType "> < xsd:list itemType =" myInteger " /> </ xsd:simpleType > Instance Document < listOfMyInt >20003 15037 95977 95945</ listOfMyInt >
Example: List Type with Facet list of integers where 5 is the maximum number of items allowed in the list. < xs:simpleType name=' derivedlistOfIntegers '> < xs:restriction base=' listOfIntegers '> < xs:maxLength value='5'> </ xs:restriction > </ xs:simpleType > Usage in XML instance document < myelement listOfIntegers ='1 100 9 4000 0'/>
Example: List Type with Facet List Type for Five Colors < xsd:simpleType name=" ColorList "> < xsd:list itemType =" Color "/> </ xsd:simpleType > < xsd:simpleType name=" FiveColors "> < xsd:restriction base=" ColorList "> < xsd:length value="5"/> </ xsd:restriction > </ xsd:simpleType > Use in XML instance document < fiveColors >white black blue red green</ fiveColors >
Example: List Type with Facet < xsd:simpleType name=“ StateList "> < xsd:list itemType =“State"/> </ xsd:simpleType > < xsd:simpleType name=" SixStates "> < xsd:restriction base=“ StateList "> < xsd:length value="6"/> </ xsd:restriction > </ xsd:simpleType > <element name=“ SelectedStates ” type=“ SixStates ”> Define a list of exactly six states ( SelectedStates ), we first define a new list type called StateList from State, and then we derive SixStates by restricting StateList to only six items < SelectedStates >KA KL GA TN AP MP</ SelectedStates >
Notes about the list type You cannot create a list of lists i.e., you cannot create a list type from another list type . You cannot create a list of complexTypes i.e., lists only apply to simpleTypes In the instance document, you must separate each item in a list with white space (blank space, tab, or carriage return ) The only facets that you may use with a list type are: length : use this to specify the length of the list minLength : use this to specify the minimum length of the list maxLength : use this to specify the maximum length of the list enumeration : use this to specify the values that the list may have pattern : use this to specify the values that the list may have
Union Type The xsd:union element creates a new simple type that allows the element to contain a value that conforms to any one of a group of specified base types.
Creating a simpleType that is a Union of Types simpleType 1 simpleType 2 simpleType 1 + simpleType 2 Note: you can create a union of more than just two simpleTypes
Prohibiting Derivation You may, for some reason, have a simple type that you don’t want anybody to derive further types from. Do this by specifying the final attribute on the < simpleType > element. Its value is a list containing a subset of values from list , union , restriction , extension . Give the final attribute the value “ #all ” for blanket prohibition of any derivation from this simple type. Can also prevent the value of individual facets from being changed in subsequent restrictions by specifying fixed="true" on the facet elements.
Prohibit XML Schema Derivations The final attribute Can be used in xs:complexType xs:simpleType xs:element Can take values of restriction extension #all (any derivation) The fixed attribute Can only be used in xs:simpleType When it is set to true, it cannot be further modified < xs:complexType name=" characterType " final="#all "> < xs:sequence > < xs:element name="name" type=" nameType "/> < xs:element name=“age" type=“ ageType "/> < xs:element name="qualification" type=" descType "/> </ xs:sequence > </ xs:complexType > < xs:simpleType name=“ fixedString "> < xs:restriction base=" xs:string "> < xs:maxLength value="32" fixed="true " /> </ xs:restriction > </ xs:simpleType >
Complex Types Complex Types define logical structures with attributes and nested elements They use a sequence , choice or all containing elements that use Simple Types or other Complex Types May reference types defined elsewhere in the schema or imported using import statement
Definition – complex type < xsd:complexType name=" PurchaseOrderType "> < xsd:sequence > < xsd:element name=" shipTo " type ="Address "/> < xsd:element name=" billTo " type ="Address "/> < xsd:element ref="comment" minOccurs ="0"/> < xsd:element name="items" type="Items"/> </ xsd:sequence > < xsd:attribute name=" orderDate " type=" xsd:date "/> </ xsd:complexType > Define the complex type “ purchaseOrderType ” Allow elements & attributes contain a set of element declarations, element references, and attribute declarations. element declarations attribute declaration element reference
Element Content Three different ways Complex types from simple types Mixed content Elements mixed with character content Empty content
XML-Schema : Mixed, Empty and Any Content Mixed content is used if you want to model elements that includes both subelements and character data Empty content is used to define elements that do not include any subelements and character data Any content (the most basic data type) does not constrain the content in any way
Complex Type from a Simple Type < xsd:element name=" internationalPrice "> < xsd:complexType > < xsd:simpleContent > < xsd:extension base=" xsd:decimal "> < xsd:attribute name="currency” type=" xsd:string " /> </ xsd:extension > </ xsd:simpleContent > </ xsd:complexType > </ xsd:element > XML Instance: < internationalPrice currency="EUR ">423.46</ internationalPrice >
Mixed elements Mixed elements may contain both text and elements We add mixed="true" to the xs:complexType element < xs:complexType name="paragraph" mixed="true" > < xs:sequence > < xs:element name=" someName " type=" xs:anyType "/> </ xs:sequence > </ xs:complexType >
Mixed Content Sub-elements mixed with character data < letterBody > <salutation>Dear Mr .<name> Anu Malik </name>. </salutation> Your order of <quantity>1</quantity> < productName >LCD Monitor</ productName > shipped from our warehouse on < shipDate >2009-09-18</ shipDate > . .... </ letterBody >
Empty Elements XML Schema doesn’t have any unique way of representing elements that must be empty. The simplest thing to do this is simply omit the allowed element content in a complex content restriction. Can such an element also be mixed (i.e. have pure text content)? Logically it seems this should be possible But it seems to be forbidden by the XML Schema specification, which singles out this case and says such an element is strictly empty.
Empty Elements To specify Empty type of element, define the element’s type using the xsd:complexType element, but omit the content model. < xsd:element name=”BR”> < xsd:complexType > </ xsd:complexType > </ xsd:element > The following is a conforming element: <BR></BR> as is this one: <BR/>
Complex Type Hierarchy There are no built in complex types, other than the so-called ur -type , represented as xsd:anyType . All other complex types are derived by one or more steps of restriction and extension from xsd:anyType . Complex types can also be created by extension of a simple type, but simple types are also notionally restrictions of xsd:anyType .
XML Schema built-in Datatypes
Restriction A restriction of a base type is a new type. All allowed instances of the new type are also instances of the base type . B ut the restricted type doesn’t allow all possibilities allowed by the base type. Think of the example of restricting xsd:string to 4 characters using the length facet. Strings of length 4 are also allowed by the xsd:string , but the new type is more restrictive. In the complex case, we might have a complex base type that allows attribute att optionally . A restricted type might not allow att at all. Another restriction of the same base might require att . Or we might have a base type that allows 0 or more nested elm elements. The restricted type might require exactly 1 nested elm element.
Extension An extension of a base type is a new type. An extension allows extra attributes or extra content that are not allowed in instances of the base type. At first brush this sounds like the opposite of restriction, but this isn’t strictly true. If, for example, type E extends a type B by adding a required attribute att , then instances of B are not allowed instances of E (because they don’t have the required attribute). So we have that E is an extension of B , but there is no sense in which B could be a restriction of E . Some such inverse relation exists if all extra attributes and content are optional in the extended type, but this isn’t a required feature of extension.
Remarks When one restricts a type one generally must specify all allowed element content and attributes. When one extends a type one generally must specify just the extra element content and attributes.
Complex Content and Simple Content We have seen that XML Schema complex types define both some allowed nested elements , and some allowed attribute specifications . Complex types that allow nested elements are said to have complex content . But Schema distinguish as a special case complex types having simple content —elements with such types may have attributes, but they cannot have nested elements. This is presumably a useful distinction, but it does introduce one more layer of complexity into the syntax for complex type derivation.
Complex Content and Simple Content Simple Type Complex Type Simple Content Complex Content Type allows attributes or elements? allows elements in content?
Simple contents A good example of complex elements with simple content is the < textarea > element in HTML documents. It accepts a text string as the content with no sub (child) element and some attributes. < xs:element name=" textarea "> < xs:complexType > < xs:simpleContent > < xs:extension base=" xs:string "> < xs:attribute name="name" type=" xs:string "/> < xs:attribute name="cols" type=" xs:positiveInteger "/> < xs:attribute name="rows" type=" xs:positiveInteger "/> </ xs:extension > </ xs:simpleContent > </ xs:complexType > </ xs:element >
Simple content type < xsd:element name=" internationalPrice "> < xsd:complexType > < xsd:simpleContent > < xsd:extension base=" xsd:decimal "> < xsd:attribute name="currency” type=" xsd:string " /> </ xsd:extension > </ xsd:simpleContent > </ xsd:complexType > </ xsd:element > < internationalPrice currency="EUR ">423.46</ internationalPrice > simpleContent indicates that the content model of the new type contains only character data and no element declaration
Complex contents Elements including children are defined as complex types with complex content E.g. < xs:element name=" protein_set "> < xs:complexType > < xs: complexContent > < xs:restriction base=" xs:anyType "> < xs:sequence > < xs:element ref="protein" maxOccurs ="unbounded"/> </ xs:sequence > </ xs:restriction > </ xs:complexContent > </ xs:complexType > </ xs:element >
Basic Forms of Complex Type Definition Restriction Extension <complexType> <complexContent> <restriction base=" type "> allowed element content allowed attributes </restriction> </complexContent> </complexType> <complexType> <complexContent> <extension base=" type "> extra element content extra attributes </extension> </complexContent> </complexType> <complexType> <simpleContent> <restriction base=" type "> facet restrictions allowed attributes </restriction> </simpleContent> </complexType> <complexType> <simpleContent> <extension base=" type "> extra attributes </extension> </simpleContent> </complexType>
Requirements on Base Type The base type must be a complex type in all cases except simpleContent /extension (lower right in table shown in the previous slide), in which case the base can be a simple type.
Defining a Complex Type with no Base? Actually the XML Schema specification says that: <complexType> <complexContent> <restriction base="xsd:anyType"> allowed element content allowed attributes </restriction> </complexContent> </complexType> <complexType> allowed element content allowed attributes </complexType> So in reality we were directly restricting the ur -type , which allows any attributes and any content! Is “shorthand” for
Empty Content - 2 < xsd:element name=" internationalPrice "> < xsd:complexType > < xsd:attribute name="currency” type=" xsd:string "/> < xsd:attribute name="value” type=" xsd:decimal "/> </ xsd:complexType > </ xsd:element > A complex type defined without complexContent is interpreted as shorthand for complex content that restricts anyType
XSD Complex Types Indicators We have seven types of indicators: Order indicators: All Choice Sequence Occurrence indicators: maxOccurs minOccurs Group indicators: Group name attributeGroup name
Defining Element Content Where we wrote allowed element content or extra element content in the syntax for complex type definitions, what should appear is a model group . A model group is exactly one of: an < xsd:sequence /> element, or an < xsd:choice /> element, or an < xsd:all /> element. (The element content appearing in the type definition may also be a globally defined model group, referenced through an < xsd:group /> element. The global definition —a named < xsd:group /> element— just contains one of the three elements above.)
Sequence A < xsd:sequence /> model group contains a series of particles . A particle is an < xsd:element /> element, another model group, or a wildcard . As expected, this model just says the element content represented by those items should appear in sequence. E.g. < xsd:sequence > < xsd:element ref="title"/> < xsd:element ref="paragraph" minOccurs ="0“ maxOccurs ="unbounded"/> </ xsd:sequence >
xs:sequence We’ve already seen an example of a complex type whose elements must occur in a specific order : < xs:element name="person"> < xs:complexType > < xs:sequence > < xs:element name=" firstName " type=" xs:string " /> < xs:element name=" lastName " type=" xs:string " /> </ xs:sequence > </ xs:complexType > </ xs:element >
Choice A < xsd:choice /> model group also contains a series of particles, with the same options as for sequence . The element information validated by this model should match exactly one of the particles in the choice . E.g. < xsd:choice minOccurs ="0" maxOccurs ="unbounded"/> < xsd:element ref="paragraph"> < xsd:sequence > < xsd:element ref="figure"/> < xsd:element ref="caption"/> < xsd:sequence > </ xsd:choice >
All The < xsd:all /> model group is peculiar to XML Schema. All particles it contains must be < xsd:element /> s. The element information validated should match a sequence of the particles in any order . There are several constraints: The maxOccurs attribute of each particle must be 1 . The minOccurs attribute of each particle must be or 1 . The < xsd:all /> model group can only occur at the top level of a complex type’s content model, and must itself have minOccurs = maxOccurs = 1 .
xs:all xs:all allows elements to appear in any order < xs:element name="person"> < xs:complexType > < xs:all > < xs:element name=" firstName " type=" xs:string " /> < xs:element name=" lastName " type=" xs:string " /> </ xs:all > </ xs:complexType > </ xs:element >
Element Wildcard The element wildcard particle < xsd:any /> matches and validates any element in the instance document. E.g. < xsd:sequence minOccurs ="0“ maxxOccurs ="unbounded"> < xsd:element ref=“header"/> < xsd:any /> </ xsd:sequence > matches a sequence of consecutive pairs of elements, where the first element in each pair is a header , and the second can be any kind of element.
Options on < xsd:any /> The < xsd:any /> element takes the usual optional maxOccurs , minOccurs attributes. Allows a namespace attribute taking one of the values: ##any (the default), ##other (any namespace except the target namespace), List of namespace names, optionally including either ## targetNamespace or ##local . Controls what elements the wildcard matches, according to namespace. It also allows a processContents attribute taking one of the values strict , skip , lax (default strict ), controlling the extent to which the contents of the matched element are validated.
Specifying the Namespace of Extension Elements <any namespace="##other"/> allows the instance document to contain a new element, provided the element comes from a namespace other than the one the schema is defining (i.e., targetNamespace ). <any namespace="http://www.somewhere.com"/> allows a new element, provided it's from the specified namespace Note: you can specify a list of namespaces, separated by a blank space. One of the namespaces can be ## targetNamespace (see next) <any namespace="## targetNamespace "/> allows a new element, provided it's from the namespace that the schema is defining. <any namespace="##any"/> allows an element from any namespace. This is the default. <any namespace="##local"/> the new element must come from no namespace
Process Contents < xs:complexType name=“ bodyType ”> ... < xsdsequence > < xs:any processContents =“ skip ”/> </ xs:sequence > ... </ xs:complexType > Putting “skip” here says: don’t validate the content of the body
< xs:complexType name=“ bodyType ”> ... < xs:sequence > < xs:any processContents =“ strict ”/> </ xs:sequence > ... </ xs:complexType > Process Contents Putting “strict” here says: you must have declarations and must successfully validate the contents of body.
< xs:complexType name=“ bodyType ”> ... < xs:sequence > < xs:any processContents =“ lax ”/> </ xs:sequence > ... </ xs:complexType > Process Contents Putting “lax” here says: validate only if your schema has declarations for the contents
Declaring Attributes An attribute is declared by using the xsd:attribute schema element. Like elements, attributes must have a name and type An attribute always has a simple type. Attributes can use custom data types Attributes can be restricted in regard to cardinality or default values Attributes can refer to other attribute definitions < xsd:attribute name = “country” type = “ xsd:string ” fixed = “India ”/>
Defining an attribute Attributes themselves are always declared as simple types An attribute is defined as < xs:attribute name=" name " type=" type " /> where: name and type are the same as for xs:element Other attributes a simple element may have: default=" default value " if no other value is specified fixed=" value " no other value may be specified default and fixed are mutually exclusive. use="optional" the attribute is not required (default) use="required" the attribute must be present
Declaring Attributes We can use a built-in simple types to an attribute by assigning the type’s name to the type attribute of the xsd:attribute element. < xsd:attribute name=” IndexPages ” type =” xsd:positiveInteger ” />
Declaring Attributes We can also define a new simple type by including the xsd:simpleType schema element within the xsd:attribute element. < xsd:attribute name =”Type”> < xsd:simpleType > < xsd:restriction base=” xsd:string ”> < xsd:enumeration value =” Comdey ”/> < xsd:enumeration value =”Tragedy”/> < xsd:enumeration value =”Documentary”/> </ xsd:restriction > </ xsd:simpleType > </ xsd:attribute >
Simple Content plus Attributes Here is a declaration of an anchor element that allows simple content plus an href attribute. < xsd:element name="anchor"> < xsd:complexType > < xsd:simpleContent > < xsd:extension base=" xsd:string ”> < xsd:attribute name=" href " type=" xsd:anyURI "/> < xsd:extension > </ xsd:simpleContent > </ xsd:complexType > </ xsd:element >
Text element with attributes If a text element has attributes, it is no longer a simple type < xs:element name="population"> < xs:complexType > < xs:simpleContent > < xs:extension base=" xs:integer "> < xs:attribute name=" year" type=" xs:integer "> </ xs:extension > </ xs:simpleContent > </ xs:complexType > </ xs:element >
Empty element with a attribute < xs:complexType name="counter"> < xs:complexContent > < xs:extension base=" xs:anyType "/> < xs:attribute name="count" type=" xs:integer "/> </ xs:complexContent > </ xs:complexType >
Simple Attribute Declarations Using attribute declaration within a complex element. < xsd:element name="figure"> < xsd:complexType > < xsd:attribute name="source" type=" xsd:string "/> </ xsd:complexType > </ xsd:element >
Complex Content Plus Attributes Here is a declaration of a body element that allows mixed content plus a style attribute. < xsd:element name="body"> < xsd:complexType mixed="true"> < xsd:choice minOccurs ="0" maxOccurs ="unbounded"> <element ref="p"/> <element ref="a"/> </ xsd:choice > < xsd:attribute name="style" type=" xsd:string "/> </ xsd:complexType > </ xsd:element >
Attribute Wildcards An attribute wildcard is represented by an < xsd:anyAttribute /> element. There can be at most one such element in a complex type definition, and it must appear after any normal attribute declarations. Such an declaration allows any attribute, optionally limited by namespace. The namespace and processContents attributes on < xsd:anyAttribute /> work as for < xsd:any /> .
Attributes and Namespaces By default, attributes ( declared inside an < xsd:complexType /> ) do not become part of the target namespace. Instead these attributes are local properties of any element they are attached to. The element itself may or may not belong to a namespace. In instance documents, names of these attributes must not be prefixed with a namespace prefix.
Creating Attributes in a Namespace There are three ways to put attributes into the target namespace: Declare them “globally”, directly inside the top level < xsd:schema /> element. Reference the attribute declaration inside the complex type definition (like element references), or specify the attribute form="qualified" on a local < xsd:attribute /> declaration, or specify the attribute attributeFormDefault ="qualified" on the < xsd:schema /> element. After this, these attributes must be prefixed in instance documents with a namespace prefix. Recall default namespace declarations ( xmlns =" namespace " ) don’t work for attributes: you must introduce a non-empty prefix.
Explicit Type vs. Implicit Type Explicit type One in which a name is given to the type Element that uses the type is generally defined in a different section of the file Object-oriented in that same explicit type is used as the type for several different elements Implicit type (nameless type) Use when the type is not needed by multiple elements
Example of Explicit Type <!-- Type has a name zipUnion --> < xsd:simpleType name=" zipUnion "> < xsd:union memberTypes ="State listOfMyIntType "/> </ xsd:simpleType > <!-- zipUnion type is used in other parts of Schema document --> <element name=zips type=“ zipUnion ”> … <element name= theOtherZips type=“ zipUnion ”> … <element name= theThirdZips type=“ zipUnion ”>
Anonymous User-Defined Simple Type < xsd: element name=" elevation "> < xsd: simpleType > < xsd:restriction base=" xsd:integer "> < xsd:minInclusive value="-1290"/> < xsd:maxInclusive value="29035"/> </ xsd:restriction > </ xsd:simpleType > </ xsd:element > The simpleType definition is defined inline, it is an anonymous simpleType definition. The disadvantage of this approach is that this simpleType may not be reused by other elements.
Types Simple vs. Complex Simple types Complex types Can be either: global and named, or local and anonymous
Local and Global declarations Global Declared at top level of a schema, just immediately below xsd:schema element. They are available to be used throughout rest of the schema. Global element declaration do not determine where an element can appear in the XML document – they only determine what the element will look like. A global element declaration must be explicitly referenced in order to have it appear in a corresponding XML document. Local Locally declared elements . Limited to the complex type definition in which they are declared and may not be used elsewhere in the schema. Such locally declared elements are automatically referenced.
Global and local definitions Elements declared at the “top level” of a <schema> are available for use throughout the schema Elements declared within a xs:complexType are local to that type Thus , in < xs:element name="person"> < xs:complexType > < xs:sequence > < xs:element name=" firstName " type=" xs:string " /> < xs:element name=" lastName " type=" xs:string " /> </ xs:sequence > </ xs:complexType > </ xs:element > the elements firstName and lastName are only locally declared The order of declarations at the “top level” of a <schema> do not specify the order in the XML data document
Declaration and use To use a type we have declared, use it as the value of type="..." Examples: < xs:element name="student" type="person"/> < xs:element name="professor" type="person"/> Scope is important: you cannot use a type if is local to some other type
Local and Global Types in XML Schema Local type: < xsd:element name =“person”> [define locally the person’s type] </ xsd:element > Global type: < xsd:element name =“person” type =“ ttt ”/> < xsd:complexType name =“ ttt ”> [define here the type ttt ] </ xsd:complexType > Global types: can be reused in other elements
Local Names in XML-Schema < xsd:element name =“person”> < xsd:complexType > . . . . . < xsd:element name =“name”> < xsd:complexType > < xsd:sequence > < xsd:element name =“ firstname ” type =“ xsd:string ”/> < xsd:element name =“ lastname ” type =“ xsd:string ”/> </ xsd:sequence > </ xsd:element > . . . . </ xsd:complexType > </ xsd:element > < xsd:element name =“product”> < xsd:complexType > . . . . . < xsd:element name =“name” type =“ xsd:string ”/> </ xsd:complexType > </ xsd:element > name has different meanings in person and in product
What is ref ? r ef is use to reference an existing element or attribute rather than declaring a new element or attribute Existing element must be global element - an element that is declared under root element
Referencing Once you have defined an element or attribute (with name="..." ), you can refer to it with ref="..." Example: < xs:element name="person"> < xs:complexType > < xs:all > < xs:element name=" firstName " type=" xs:string " /> < xs:element name=" lastName " type=" xs:string " /> </ xs:all > </ xs:complexType > </ xs:element > < xs:element name="student" ref="person"> Or just: < xs:element ref="person">
Secondary Schema Components Model Group Definitions Attribute Group Definitions Identity-constraint Definitions Similar to ID / IDREF Notations Declarations Wildcards Similar to a DTD with “ANY” Annotations
Group Element The group element enables you to group together element declarations . Note: the group element is just for grouping together element declarations, no attribute declarations allowed!
Named Model Groups A named model group is a collection, or group, of elements. The syntax for creating a model group is < xs:group name=" name "> elements </ xs:group > Where name is the name of the model group, and elements is a collection of element declarations
Note about group Group definitions must be global < xsd:element name="Book"> < xsd:complexType > < xsd:sequence > < xsd:group name=" PublicationElements "> < xsd:sequence > < xsd:element name="Title" type=" xsd:string " minOccurs ="0"/> < xsd:element name="Author" type=" xsd:string " minOccurs ="0" maxOccurs ="unbounded"/> < xsd:element name="Date" type=" xsd:string "/> </ xsd:sequence > </ xsd:group > < xsd:element name="ISBN" type=" xsd:string "/> < xsd:element name="Publisher" type=" xsd:string "/> </ xsd:sequence > ... </ xsd:complexType > </ xsd:element > Cannot inline the group definition . Instead, you must use a ref here and define the group globally.
Named Attribute Groups Attributes can be grouped into collections called named attribute groups. This is particularly useful for attributes that you want to use with several different elements in a schema. The syntax for a named attribute group is < xs:attributeGroup name="name"> attributes </ xs:attributeGroup > Where name is the name of the attribute group and attributes is a collection of attributes assigned to the group.
Attribute Groups Promotes logical organization Encourages reuse – defined once, referenced many times Facilitates maintenance Improves Schema readability Must be unique within an XML Schema Referenced from complexType definitions
Declaring Attribute Groups <!-- Define the unique group: --> < attributeGroup name = “ CreditCardInfo ” > <attribute name = “ CardNumber ” type = “integer” use = “required” /> <attribute name = “ ExpirationDate ” type = “date” use = “required” /> <attribute name = “ CardHolder ” type = “ FullName ” use = “required” /> </ attributeGroup > <!-- Then you can reference it from a complexType : --> < complexType name = “ CreditInformation ” > < attributeGroup ref = “ CreditCardInfo ” /> </ complexType >
Identity Constraints Recall that the attribute types ID and IDREF imply interesting constraints on values of those attributes: Within any individual XML document, every attribute of type ID must be specified with a different value from every other attribute of type ID. The value of any attribute of type IDREF must be the same as the value of an attribute of type ID specified somewhere in the same document.
Annotation The annotation element provides a mechanism for documenting most other schema elements. Unlike an XML comment, which looks like <!-- comment text -->, the annotation is part of the schema component. An annotation provides for human documentation in one or more languages, as well as programmatic documentation, such as meaningful URIs
Annotation Appears at the beginning of most schema constructions Can have two sub-elements Documentation appInfo Documentation For human readable materials appInfo For tools, stylesheets and other applications
XML-Schema : Annotations XML-schema provides several tags for annotating a schema: documentation (intended for human readers), appInfo (intended for applications) and annotation. This mixed content model allows the inclusion of almost any content, such as text: < xs:element name="author" type="author"> < xs:annotation > < xs:documentation xml:lang ="en"> The author of a book. </ xs:documentation > < xs:documentation xml:lang =" fr "> Designe l'auteur d'un livre . </ xs:documentation > </ xs:annotation > </ xs:element >
XML-Schema : AppInfo Annotations are also a good container for application-specific metadata, such as those used by the schema for schema to describe the list of facets and properties of its primitive datatypes:
No targetNamespace ( noNamespaceSchemaLocation ) Sometimes you may wish to create a schema but without associating the elements with a namespace. The targetNamespace attribute is actually an optional attribute of <schema>. Thus, if you don’t want to specify a namespace for your schema then simply don’t use the targetNamespace attribute. Consequences of having no namespace 1. In the instance document don’t namespace qualify the elements. 2. In the instance document, instead of using schemaLocation use noNamespaceSchemaLocation .
<?xml version="1.0"?> <BookStore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation = "BookStore.xsd"> <Book> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>1998</Date> <ISBN>1-56592-235-2</ISBN> <Publisher>McMillin Publishing</Publisher> </Book> … </BookStore> 1. Note that there is no default namespace declaration. So, none of the elements are associated with a namespace. 2. Note that we do not use xsi:schemaLocation (since it requires a pair of values - a namespace and a URL to the schema for that namespace). Instead, we use xsi:noNamespaceSchemaLocation .
Assembling an Instance Document from Multiple Schema Documents An instance document may be composed of elements from multiple schemas . Validation can apply to the entire XML instance document, or to a single element.
<?xml version="1.0"?> <Library xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.book.org Book.xsd http://www.employee.org Employee.xsd"> <Books> < Book xmlns ="http://www.book.org"> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>1998</Date> <ISBN>1-56592-235-2</ISBN> <Publisher>Macmillan Publishing</Publisher> </Book> <Book xmlns ="http://www.book.org"> <Title>Illusions: The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher> </Book> <Book xmlns ="http://www.book.org"> <Title>The First and Last Freedom</Title> <Author>J. Krishnamurti </Author> <Date>1954</Date> <ISBN>0-06-064831-7</ISBN> <Publisher>Harper & Row</Publisher> </Book> </Books> <Employees> < Employee xmlns ="http://www.employee.org"> <Name>John </ Name> <SSN>123-45-6789</SSN> </Employee> <Employee xmlns ="http://www.employee.org"> < Name>Smith </Name> <SSN>000-11-2345</SSN> </Employee> </Employees> </Library> Validating against two schemas The <Book> elements are defined in Book.xsd, and the <Employee> elements are defined in Employee.xsd. The <Library>, <Books>, and <Employees> elements are not defined in any schema! 1. A schema validator will validate each Book element against Book.xsd. 2. It will validate each Employee element against Employee.xsd. 3. It will not validate the other elements.
Lax Validation vs Strict Validation On the previous slide there were elements (Library, Books, and Employees) for which there was no schema to validate against . Lax validation is where the schema validator skips over elements for which no schema is available . Strict validation is where the schema validator requires validation of every element Most of the validators do strict validation. Consequently, they will reject the instance document on the previous slide.
Assembling a Schema from Multiple Schema Documents The include element allows you to access components in other schemas All the schemas you include must have the same namespace as your schema (i.e., the schema that is doing the include) The net effect of include is as though you had typed all the definitions directly into the containing schema < xsd:schema …> < xsd:include schemaLocation ="LibraryBook.xsd"/> < xsd:include schemaLocation ="LibraryEmployee.xsd"/> … </ xsd:schema > LibraryBook.xsd LibraryEmployee.xsd Library.xsd
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.library.org" xmlns="http://www.library.org" elementFormDefault="qualified"> <xsd:include schemaLocation="LibraryBook.xsd"/> <xsd:include schemaLocation="LibraryEmployee.xsd"/> <xsd:element name="Library"> <xsd:complexType> <xsd:sequence> <xsd:element name="Books"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Employees"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Employee" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> Library.xsd These are referencing element declarations in the other schemas .
Assembling a Schema from a Schema with no targetNamespace A schema can <include> another schema which has no targetNamespace . The included components take on the targetNamespace of the schema that is doing the <include>. This is called the Chameleon Effect . The components in the no-namespace schema are called Chameleon components .
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xsd: complexType name=" ProductType "> <xsd:sequence> <xsd: element name=" Type " type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema> Product.xsd Note that this schema has no targetNamespace !
<?xml version="1.0"?> < xsd:schema xmlns:xsd ="http://www.w3.org/2001/XMLSchema" targetNamespace ="http://www.company.org" xmlns ="http://www.company.org" elementFormDefault ="qualified"> < xsd:include schemaLocation ="Person.xsd"/> < xsd:include schemaLocation ="Product.xsd"/> < xsd:element name="Company"> < xsd:complexType > < xsd:sequence > < xsd:element name="Person" type="Person" maxOccurs ="unbounded"/> < xsd:element name="Product" type=" ProductType " maxOccurs ="unbounded"/> </ xsd:sequence > </ xsd:complexType > </ xsd:element > </ xsd:schema > Company.xsd This schema <include>s Product.xsd. Thus, the components in Product.xsd are namespace-coerced to the company targetNamespace . Consequently, we can reference those components just as though they had originally been declared in a schema with the same targetNamespace .
Assembling a Schema from Multiple Schema Documents with Different Namespaces The import element allows you to access elements and types in a different namespace < xsd:schema …> < xsd:import namespace="A" schemaLocation ="A.xsd"/> < xsd:import namespace="B" schemaLocation ="B.xsd"/> … </ xsd:schema > Namespace A A.xsd Namespace B B.xsd C.xsd
Camera Schema Camera.xsd Nikon.xsd Olympus.xsd Pentax.xsd
<?xml version="1.0"?> < xsd:schema xmlns:xsd ="http://www.w3.org/2001/XMLSchema" targetNamespace ="http://www.camera.org" xmlns:nikon ="http://www.nikon.com" xmlns:olympus ="http://www.olympus.com" xmlns:pentax ="http://www.pentax.com" elementFormDefault ="qualified"> < xsd:import namespace="http:// www.nikon.com" schemaLocation ="Nikon.xsd"/> < xsd:import namespace="http://www.olympus.com" schemaLocation ="Olympus.xsd"/> < xsd:import namespace="http://www.pentax.com" schemaLocation ="Pentax.xsd"/> < xsd:element name="camera"> < xsd:complexType > < xsd:sequence > < xsd:element name="body" type=" nikon:body_type "/> < xsd:element name="lens" type=" olympus:lens_type "/> < xsd:element name=" manual_adapter " type=" pentax:manual_adapter_type "/> </ xsd:sequence > </ xsd:complexType > </ xsd:element > < xsd:schema > Camera.xsd These import elements give us access to the components in these other schemas. Here the Body type Is defined in the Nikon namespace
<?xml version="1.0"?> <c:camera xmlns:c="http://www.camera.org" xmlns:nikon="http://www.nikon.com" xmlns:olympus="http://www.olympus.com" xmlns:pentax="http://www.pentax.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.camera.org Camera.xsd http://www.nikon.com Nikon.xsd http://www.olympus.com Olympus.xsd http://www.pentax.com Pentax.xsd"> <c:body> <nikon:description>Ergonomically designed casing for easy handling</nikon:description> </c:body> <c:lens> <olympus:zoom>300mm</olympus:zoom> <olympus:f-stop>1.2</olympus:f-stop> </c:lens> <c:manual_adapter> <pentax:speed>1/10,000 sec to 100 sec</pentax:speed> </c:manual_adapter> </c:camera> The Camera instance uses elements from the Nikon, Olympus, and Pentax namespaces. Camera.xml
Redundant! On the previous slide, the value of schemaLocation contained four pairs of values - one for camera, and three for each schema that it uses. The later three are redundant. Once you give the schema-validator the URL to the camera schema it will examine the camera schema and see the import elements, thus it will deduce the other schemas being used (Nikon, Olympus, and Pentax) The next slide shows the non-redundant version.