86
W3C XML Schema W3C XML Schema

W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

  • Upload
    others

  • View
    39

  • Download
    0

Embed Size (px)

Citation preview

Page 1: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

W3C XML SchemaW3C XML Schema

Page 2: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Topics

Motivation Simple types Complex types Element vs. Attribute Occurrences List type Union type Explicit vs. Implicit Element content Annotation Choices and Group Namespaces

Page 3: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Motivations forMotivations forW3C XML SchemaW3C XML Schema

Page 4: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

XML Schema Status

W3C candidate recommendation as of Oct. 2000http://www.w3.org/XML/Schema.html XML schema aware tools

Several free and commercial versions available (Check the above site)

NetBeans 5.5 and after (Free) XMLSpy (Commercial, Not Free) Apache Xerces DTD to XML Schema conversion tool

Page 5: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Motivations of XML Schema

Provide more powerful and flexible schema language than DTD

Represent XML document syntax in XML languageXML tools can be readily used

Support non-textual data types Important to B2B, e-Commerce

Handle complex syntax

Page 6: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Valid vs. Schema-valid

XML schema is not part of XML 1.0 XML document that is validated with

DTD is “valid” XML document that conforms to XML

schema is “schema-valid” XML document that conforms to a

particular XML schema is called “instance document” of that schema

Page 7: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Definitions vs.Definitions vs.DeclarationsDeclarations

Page 8: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Definition and Declaration

DefinitionCreate 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

Page 9: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Example

<!-- Definition: Creation of a type --><xsd:simpleType name="zipUnion"> <xsd:union memberTypes="USState listOfMyIntType"/></xsd:simpleType>

<!-- Declaration --><element name=zips type=“zipUnion”>

Page 10: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Schema Data Types:Schema Data Types:Simple Types &Simple Types &Complex TypesComplex Types

Page 11: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Schema Data Types

Simple typeDo not have sub-elements

Do not have “element” sub-elements Do not have “attribute” sub-elements

Predefined type or derived from predefined type

Complex type Have either “element” sub-elements or

“attribute” sub-elements

Page 12: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Simple TypesSimple Types

Page 13: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Predefined Simple Types

String, CDATA, token, byte, unsignedByte, binary, integer, positiveInteger, negativeInteger, nonNegativeInteger, nonPositiveInteger, int, unsignedInt, long, unsignedLong, short, unsignedShort, decimal, float, double, boolean, time, timeInstant, timePeriod, timeDuration, date, month, year, century, recurringDay, recurringDate, recurringDuration, Name, Qname, NCName, uriReference, language, ID, IDREF, IDREFS, ENTITY, ENTITIES, NOTATION, NMTOKEN, NMTOKENS

Page 14: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Examples of Predefined Simple type<element name=“Title” type=“string”/><element name=“Heading” type=“string”/><element name=“Topic” type=“string”/><element name=“Price” type=“decimal”/>

<attribute name=“focus” type=“string”/>

Page 15: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Derived Simple Type

Derived from existing simple types (predefined or derived)

Typically restricting existing simple type The legal range of values for a new type

is subset of the ones of existing typeExisting type is called base typeUse restriction element along with facets

to restrict the range of values Facets are rules of restriction

Page 16: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Example of Derived Simple Type 1 (Numeric range)<xsd:simpleType name="myInteger"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="10000"/> <xsd:maxInclusive value="99999"/> </xsd:restriction></xsd:simpleType>

Defining myInteger type whose range of value is between 10000 and 99999

minInclusive and maxInclusive are facets that can be applied to integer type

Page 17: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Example of Derived Simple Type 2 (Regular expression)<xsd:simpleType name="SKU"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{3}-[A-Z]{2}"/> </xsd:restriction> </xsd:simpleType>

Defining new type called SKU pattern is a facet that can be applied to string

Regular expression three digits followed by a hyphen followed by two

upper-case ASCII letters

Page 18: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Example of Derived Simple Type 3 (Enumeration)<xsd:simpleType name="USState"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="AK"/> <xsd:enumeration value="AL"/> <xsd:enumeration value="AR"/> <!-- and so on ... --> </xsd:restriction></xsd:simpleType>

enumeration facet limits a simple type to a set of distinct values

Page 19: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Complex TypeComplex Type

Page 20: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Complex Type

Defined using “complexType” element Typically contain

element declarationselement referencesattribute declarations

Page 21: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

complexType Example 1

<xsd:complexType name="USAddress" > <xsd:sequence> <xsd:element name="name" type="xsd:string" /> <xsd:element name="street" type="xsd:string" /> <xsd:element name="city" type="xsd:string" /> <xsd:element name="state" type="xsd:string" /> <xsd:element name="zip" type="xsd:decimal" /> </xsd:sequence> <xsd:attribute name="country" type="xsd:NMTOKEN" use="fixed" value="US"/></xsd:complexType>

Page 22: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

complexType Example 1

Definition of USAddress type It contains 5 element declarations and

one attribute declaration USAddress definition contains only

declarations involving simple types: string, decimal, and NMTOKEN

Page 23: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

complexType Example 2

<xsd:complexType name="PurchaseOrderType"> <xsd:sequence> <xsd:element name="shipTo" type="USAddress"/> <xsd:element name="billTo" type="USAddress"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="items" type="Items"/> </xsd:sequence> <xsd:attribute name="orderDate" type="xsd:date"/></xsd:complexType>

Page 24: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

complexType Example 2

Definition of PurchaseOrder type Contains element declarations

referencing complex types, e.g. USAddress, Items

Contains element declaration referencing “pre-defined” simple types: date

Page 25: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Elements vs.Elements vs.AttributesAttributes

Page 26: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Element vs. Attribute

Element declarations can reference both simple types or complex types

All attribute declarations can reference only simple types Because they cannot contain other sub-

elements

Page 27: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

ref Attribute

To use 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

Page 28: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

ref Example

<xsd:schema xmlns:xsd="http://www.w3.org/2000/08/XMLSchema">

<xsd:element name="purchaseOrder" type="PurchaseOrderType"/><xsd:element name="comment" type="xsd:string"/>

<xsd:complexType name="PurchaseOrderType"> <xsd:sequence> <xsd:element name="shipTo" type="USAddress"/> <xsd:element name="billTo" type="USAddress"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="items" type="Items"/> </xsd:sequence> <xsd:attribute name="orderDate" type="xsd:date"/></xsd:complexType>

Page 29: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

OccurrencesOccurrences

Page 30: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Occurrences of Elements

minOccurs maxOccurs fixed = “Hannah”

If the element appears (optional), the value must be “Hannah”, otherwise the value is set to “Hannah” by the parser

default = “Hannah” If the element appears (optional), the

value is set to what is specified, otherwise value is set to “Hannah” by the parser

Page 31: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Example

<element name=“test” type=“string” minOccurs=“1” maxOccurs=“1” minOccurs=“1” maxOccurs=“1” fixed=“Hannah” minOccurs=“2” maxOccurs=“unbounded” minOccurs=“0” maxOccurs=“1” fixed=“Hannah” minOccurs=“0” maxOccurs=“1” default=“Hannah” minOccurs=“0” maxOccurs=“2” default=“Hannah” minOccurs=“0” maxOccurs=“0”

>

Page 32: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Occurrences of Attributes

Attributes can occur once or not at all “use” attribute

required optional fixeddefault

“value” attribute

Page 33: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Example

<attribute name=“test” type=“string”use=“required”use=“required” value=“37”use=“optional”use=“fixed”, value=“37”use=“default” value=“37”use=“prohibited”

>

Page 34: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Example

<xsd:complexType name="USAddress"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="street" type="xsd:string"/> ... </xsd:sequence> <xsd:attribute name="country" type="xsd:NMTOKEN" use="fixed" value="US"/> </xsd:complexType>

Appearance of a country attribute is optional Its value must be US if it does appear If it does not appear, parser will create a country

attribute with value US

Page 35: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Attributes

EnumerationsimpleType element with base attributebase attribute specifies the type

Page 36: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Complete ExampleComplete Example

Page 37: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Example

<complexType name="ContentsType"> <element name="Chapter" maxOccurs="*"> <complexType> <element name="Heading" type="string" minOccurs="0" /> <element name="Topic" maxOccurs="*"> <complexType content="string"> <attribute name="subSections" type="integer" /> </complexType> </element> <attribute name="focus" default="Java"> <simpleType base="string"> <enumeration value="XML" /> <enumeration value="Java" /> </simpleType> </attribute> </complexType> </element></complexType>

Page 38: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Complete Example (1st page)

<?xml version="1.0"?><schema targetNamespace="http://www.oreilly.com/catalog/javaxml/" xmlns="http://www.w3.org/1999/XMLSchema" xmlns:JavaXML="http://www.oreilly.com/catalog/javaxml/">

<element name="Book" type="JavaXML:BookType" />

<complexType name="BookType"> <element name="Title" type="string" /> <element name="Contents" type="JavaXML:ContentsType" /> <element name="Copyright" type="string" /> </complexType>

Page 39: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Continued <complexType name="ContentsType"> <element name="Chapter" maxOccurs="*"> <complexType> <element name="Heading" type="string" minOccurs="0" /> <element name="Topic" maxOccurs="*"> <complexType content="string"> <attribute name="subSections" type="integer" /> </complexType> </element> <attribute name="focus" default="Java"> <simpleType base="string"> <enumeration value="XML" /> <enumeration value="Java" /> </simpleType> </attribute> </complexType> </element> <element name="SectionBreak" minOccurs="0" maxOccurs="*"> <complexType content="empty" /> </element> </complexType>

</schema>

Page 40: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

List TypeList Type

Page 41: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

List Type

Comprised of sequences of atomic simple types

Three built-in list typesNMTOKENS, IDREFS, ENTITIES

User defined List typeDerive from atomic types

facets length, minLength, maxLength,

enumeration

Page 42: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Example of List Type

Schema

<xsd:simpleType name="listOfMyIntType"> <xsd:list itemType="myInteger"/></xsd:simpleType>

Instance Document

<listOfMyInt>20003 15037 95977 95945</listOfMyInt>

Page 43: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Example: List Type with Facet

<xsd:simpleType name="USStateList"> <xsd:list itemType="USState"/></xsd:simpleType>

<xsd:simpleType name="SixUSStates"> <xsd:restriction base="USStateList"> <xsd:length value="6"/> </xsd:restriction></xsd:simpleType>

<element name=“sixStates” type=“SixUSStates”>

Define a list of exactly six US states (SixUSStates), we first define a new list type called USStateList from USState, and then we derive SixUSStates by restricting USStateList to only six items

<sixStates>PA NY CA NY LA AK</sixStates>

Page 44: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Union TypeUnion Type

Page 45: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Union Type

Enables an element or attribute value to be one or more instances of one type drawn from the union of multiple atomic and list types

facets: pattern and enumeration

Page 46: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Union Type for Zipcodes

<xsd:simpleType name="zipUnion"> <xsd:union memberTypes="USState listOfMyIntType"/></xsd:simpleType>

<element name=zips type=“zipUnion”>

<zips>CA</zips><zips>95630 95977 95945</zips><zips>AK</zips>

Page 47: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Explicit Type vs.Explicit Type vs.Implicit TypeImplicit Type

Page 48: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Explicit Type vs. Implicit Type

Explicit type One in which a name is given to the typeElement that uses the type is generally

defined in a different section of the fileObject-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

Page 49: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Example of Explicit Type

<!-- Type has a name zipUnion --><xsd:simpleType name="zipUnion"> <xsd:union memberTypes="USState 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”>

Page 50: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Example of Implicit Type

<xsd:complexType name="Items"> <!– Explicit complexType <xsd:sequence> <xsd:element name="item" minOccurs="0" maxOccurs="unbounded"> <xsd:complexType> <!-- Implicit complexType --> <xsd:sequence> <xsd:element name="productName" type="xsd:string"/> <xsd:element name="quantity"> <xsd:simpleType> <!-- Implicit simpleType --> <xsd:restriction base="xsd:positiveInteger"> <xsd:maxExclusive value="100"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="USPrice" type="xsd:decimal"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/> </xsd:sequence> <xsd:attribute name="partNum" type="SKU"/> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType>

Page 51: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

ElementElementContentContent

Page 52: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Element Content

How content of an element gets constructed

Three different waysComplex types from simple typesMixed content

Elements mixed with character contentEmpty content

Page 53: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Complex Types from Simple Types<USPrice>345.67</USPrice> (usage in document instance)<xsd:element name="USPrice" type="decimal"/> (in XML schema)

<internationalPrice currency="EUR">423.46</internationalPrice> ??? (what would be done in XML schema?)

Need to create complexType based on simple type Simple type cannot have attributes Have to have attribute declaration Based on decimal simple type

Page 54: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

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>

simpleContent indicates that the content model of the new type contains only character data and no element declaration

Page 55: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Mixed Content

Sub-elements mixed with character data

<letterBody><salutation>Dear Mr.<name>Robert Smith</name>.</salutation>Your order of <quantity>1</quantity> <productName>BabyMonitor</productName> shipped from our warehouse on<shipDate>1999-05-21</shipDate>. ....</letterBody>

Page 56: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Mixed Content

<xsd:element name="letterBody"> <xsd:complexType mixed="true"> <xsd:sequence> <xsd:element name="salutation"> <xsd:complexType mixed="true"> ! Implicit definition <xsd:sequence> <xsd:element name="name" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="quantity" type="xsd:positiveInteger"/> <xsd:element name="productName" type="xsd:string"/> <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/> <!-- etc --> </xsd:sequence> </xsd:complexType></xsd:element>

Page 57: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Empty Content

Define a type which do not declare any elements in its contentType’s content model is empty

Page 58: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Empty Content 1

<internationalPrice currency=“EUR” value=“345.23”/>

<xsd:element name="internationalPrice"> <xsd:complexType> <xsd:complexContent> <xsd:restriction base="xsd:anyType"> <xsd:attribute name="currency” type="xsd:string"/> <xsd:attribute name="value” type="xsd:decimal"/> </xsd:restriction> </xsd:complexContent> </xsd:complexType></xsd:element>

Page 59: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Empty Content 2

complexContent To restrict or extend the content model of

a complex type <xsd:restriction base="xsd:anyType">

Page 60: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Empty Content 3

<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

Page 61: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

anyTypeanyType

Page 62: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

anyType

Base type from which all simple and complex types are derived

Does not constrain its contents in any way

Default type when no type is specified<xsd:element name="anything"

type="xsd:anyType" /> is same as<xsd:element name=“anything”/>

Use more constrained types whenever possible

Page 63: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

AnnotationAnnotation

Page 64: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Annotation

Appears at the beginning of most schema constructions

Can have two sub-elementsdocumentationappInfo

documentationFor human readable materials

appInfoFor tools, stylesheets and other

applications

Page 65: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Example of Annotation

<xsd:element name="internationalPrice"> <xsd:annotation> <xsd:documentation> element declared with anonymous type </xsd:documentation> </xsd:annotation> <xsd:complexType>

<xsd:annotation> <xsd:documentation> empty anonymous type with 2 attributes

</xsd:documentation> </xsd:annotation> <xsd:complexContent> <xsd:restriction base="xsd:anyType"> <xsd:attribute name="currency" type="xsd:string" /> <xsd:attribute name="value" type="xsd:decimal" /> </xsd:restriction> </xsd:complexContent>

</xsd:complexType></xsd:element>

Page 66: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Choices &Choices &GroupGroup

Page 67: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Choice and Group

choiceOnly one of its children to appear in an

instance group

Grouping a group of elementsFurther constraints

sequence all

Appear zero or once In any order

Page 68: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Choice and Sequence Groups

<xsd:complexType name="PurchaseOrderType"> <xsd:sequence> <xsd:choice> <xsd:group ref="shipAndBill" /> <xsd:element name="singleUSAddress" type="USAddress" /> </xsd:choice> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="items" type="Items" /> </xsd:sequence> <xsd:attribute name="orderDate" type="xsd:date" /></xsd:complexType>

<xsd:group name="shipAndBill"> <xsd:sequence> <xsd:element name="shipTo" type="USAddress" /> <xsd:element name="billTo" type="USAddress" /> </xsd:sequence></xsd:group>

Page 69: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Example of all

<xsd:complexType name="PurchaseOrderType"> <xsd:all> <xsd:element name="shipTo" type="USAddress"/> <xsd:element name="billTo" type="USAddress"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="items" type="Items" /> </xsd:all> <xsd:attribute name="orderDate" type="xsd:date" /></xsd:complexType>

Page 70: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Attribute Group

Define attribute group using attributeGroup element

Referenced in multiple definitions and declarations

Improve readability and maintenance They have to appear at the end of

complex type definitions

Page 71: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Example of attributeGroup<xsd:attributeGroup name="ItemDelivery"> <xsd:attribute name="partNum" type="SKU"/> <xsd:attribute name="weightKg" type="xsd:decimal"/> <xsd:attribute name="shipBy"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="air"/> <xsd:enumeration value="land"/> <xsd:enumeration value="any"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute></xsd:attributeGroup>

<!-- attributeGroup replaces individual declarations --><xsd:attributeGroup ref="ItemDelivery"/>

Page 72: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

SchemaSchemaNamespacesNamespaces

Page 73: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Different Namespaces

Namespace for XML Schema document itself (targetNamespace)

Definitions and declarations in a schema can refer to names that may belong to other namespacesThese other namespaces are referred to

as source Namespaces

Page 74: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

targetNamespace

It is the namespace that is going to be assigned to the schema you are creatingThe names defined in a schema are said

to belong to its target namespace It is the namespace an instance is

going to use to access the types it declares

Page 75: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

targetNamespace

Each schema has one target namespace and possibly many source namespaces

Page 76: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Example 1

<xsd:schema targetNamespace='http://www.SampleStore.com/Account'

xmlns:xsd='http://www.w3.org/1999/XMLSchema' xmlns:ACC= 'http://www.SampleStore.com/Account'><xsd:element name='InvoiceNo' type='xsd:positive-integer'/><xsd:element name='ProductID' type='ACC:ProductCode'/><xsd:simpleType name='ProductCode' base='xsd:string'> <xsd:pattern value='[A-Z]{1}d{6}'/></xsd:simpleType>

Page 77: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Example 1: Explanation

The targetNamespace name is http://www.SampleStore.com/Account, which contains the names InvoiceNo, ProductID, and ProductCode

The names schema, element, simpleType, pattern, string, and positive-integer belong to source namespace http://www.w3.org/1999/XMLSchema

The targetNamespace also happens to be one of the source namespaces because the name ProductCode is used in defining other names.

Page 78: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Importing a SchemaImporting a Schemawith schemaLocationwith schemaLocation

Page 79: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Example 2

<xsd:schema targetNamespace='http://www.SampleStore.com/Account'

xmlns:xsd='http://www.w3.org/1999/XMLSchema' xmlns:ACC= 'http://www.SampleStore.com/Account'><xsd:element name='InvoiceNo' type='xsd:positive-integer'/><xsd:element name='ProductID' type='ACC:ProductCode'/><xsd:simpleType name='ProductCode' base='xsd:string'> <xsd:pattern value='[A-Z]{1}d{6}'/></xsd:simpleType>

Page 80: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Example 2: Explanation

Example 1 does not need to specify locations of source schema filesFor the overall "schema of schemas,"

http://www.w3.org/1999/XMLSchema, you need not specify a location because it is well known

For the source namespace http://www.SampleStore.com/Account, you do not need to specify a location since it also happens to be the name of the target namespace that is being defined in this file.

Page 81: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Example 3

<!--XML Schema fragment in file schema1.xsd--><schema

targetNamespace='http://www.SampleStore.com/Account' xmlns='http://www.w3.org/1999/XMLSchema' xmlns:ACC= 'http://www.SampleStore.com/Account' xmlns:PART= 'http://www.PartnerStore.com/PartsCatalog'><import namespace='http://www.PartnerStore.com/PartsCatalog'

schemaLocation='http://www.ProductStandards.org/repository/alpha.xsd'/>

<element name='InvoiceNo' type='positive-integer'/><element name='ProductID' type='ACC:ProductCode'/><simpleType name='ProductCode' base='string'> <pattern value='[A-Z]{1}d{6}'/></simpleType><element name='stickyGlue' type='PART:SuperGlueType'/>

Page 82: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Example 3: Explanation

The PART namespace needs to be imported using the import declaration element whose schemaLocation attribute specifies the location of the file that contains the schema because Is not a well-known namespace Is not a targetNamespace

Page 83: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

SummarySummary

Page 84: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

Summary

Status Motivation Namespaces Vocabularies

elementcomplexTypeattributesimpleTypeenumeration

Page 85: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

schemaLocation

In an instance document, the attribute xsi:schemaLocation

<purchaseReport xmlns="http://www.example.com/Report" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xsi:schemaLocation="http://www.example.com/Report http://www.example.com/Report.xsd" period="P3M" periodEnding="1999-12-31"> <!-- etc --></purchaseReport>

Page 86: W3C XML Schema - Arizona State University · XML document that conforms to XML schema is “schema-valid” XML document that conforms to a particular XML schema is called “instance

References

XML Schema Primer on W3C Candidate Recommendation 24 October 2000, Edited by David Fallside, http://www.w3.org/TR/2000/CR-xmlschema-0-20001024/

“Java and XML” written by Brett McLaughlin, O’Reilly, June 2000 (First edition), Chapter 4 “Constraining XML”, XML Schema section, page 108-123