xml schema. why validate xml? xml documents can generally have any structure xml grammars define...

25
XML Schema

Upload: jodie-fox

Post on 05-Jan-2016

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

XML SchemaXML Schema

Page 2: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

Why Validate XML?Why Validate XML?

• XML documents can generally have any structure

• XML grammars define specific document structures

• Validation is the act of checking an XML document against a DTD or XML schema defined for a grammar

• XML documents can generally have any structure

• XML grammars define specific document structures

• Validation is the act of checking an XML document against a DTD or XML schema defined for a grammar

Page 3: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

XSDXSD

<schema xmlns="http://www.w3.org/2001/XMLSchema"

targetNamespace="http://tempuri.org/example"

xmlns:ex="http://tempuri.org/example">

…schema definitions go here…

</schema>

<schema xmlns="http://www.w3.org/2001/XMLSchema"

targetNamespace="http://tempuri.org/example"

xmlns:ex="http://tempuri.org/example">

…schema definitions go here…

</schema>

Page 4: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

What Are Built-In Datatypes?

What Are Built-In Datatypes?

• W3C XML schemas define many built-in datatypes

• Built-in datatypes can be primitive or derived

• Examples: integer,long,short…

• W3C XML schemas define many built-in datatypes

• Built-in datatypes can be primitive or derived

• Examples: integer,long,short…

Page 5: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

Element DeclarationsElement Declarations

<xs:element name="elementname" type="datatype“

(fixed="fixedvalue" | default="defaultvalue")minOccurs="value" maxOccurs="value" … />

<xs:element name="author" type="xs:string"maxOccurs="unbounded" /><xs:element name="pages" type="xs:integer" /><xs:element name="published" type="xs:date"

minOccurs="0" />

<xs:element name="elementname" type="datatype“

(fixed="fixedvalue" | default="defaultvalue")minOccurs="value" maxOccurs="value" … />

<xs:element name="author" type="xs:string"maxOccurs="unbounded" /><xs:element name="pages" type="xs:integer" /><xs:element name="published" type="xs:date"

minOccurs="0" />

Page 6: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

Attribute DeclarationsAttribute Declarations

<xs:attribute name="attributename" type="datatype"

(fixed="fixedvalue" | default="defaultvalue")use= ("optional" | "prohibited" | "required") … />

<xs:attribute name="isbn" type="xs:string" use="required" />

<xs:attribute name="attributename" type="datatype"

(fixed="fixedvalue" | default="defaultvalue")use= ("optional" | "prohibited" | "required") … />

<xs:attribute name="isbn" type="xs:string" use="required" />

Page 7: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

Derive New Simple TypesDerive New Simple Types

<xs:simpleType name="customerID">

<xs:restriction base="xs:string">

<xs:length value="5" />

</xs:restriction>

</xs:simpleType>

<xs:simpleType name="customerID">

<xs:restriction base="xs:string">

<xs:length value="5" />

</xs:restriction>

</xs:simpleType>

Page 8: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

<xs:simpleType name="numberOfMonths"><xs:restriction base="xs:byte">

<xs:minInclusive value="1" /><xs:maxInclusive value="12" />

</xs:restriction></xs:simpleType>

<xs:simpleType name="size"><xs:restriction base="xs:string">

<xs:enumeration value="small" /><xs:enumeration value="large" />

</xs:restriction></xs:simpleType>

<xs:simpleType name="timestamp"><xs:union memberTypes="xs:dateTime xs:long" />

</xs:simpleType>

<xs:simpleType name="numberOfMonths"><xs:restriction base="xs:byte">

<xs:minInclusive value="1" /><xs:maxInclusive value="12" />

</xs:restriction></xs:simpleType>

<xs:simpleType name="size"><xs:restriction base="xs:string">

<xs:enumeration value="small" /><xs:enumeration value="large" />

</xs:restriction></xs:simpleType>

<xs:simpleType name="timestamp"><xs:union memberTypes="xs:dateTime xs:long" />

</xs:simpleType>

Page 9: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

<xs:simpleType name="licensePlate"><xs:restriction base="xs:string">

<xs:pattern value="[A-Z]{3}-\d{3}" /></xs:restriction>

</xs:simpleType>

<xs:simpleType name="listOfCustomers"><xs:restriction>

<xs:simpleType><xs:list>

<xs:simpleType><xs:restriction base="xs:string">

<xs:length value="5" /></xs:restriction>

</xs:simpleType></xs:list>

</xs:simpleType><xs:length value="3" />

</xs:restriction></xs:simpleType>

<xs:simpleType name="licensePlate"><xs:restriction base="xs:string">

<xs:pattern value="[A-Z]{3}-\d{3}" /></xs:restriction>

</xs:simpleType>

<xs:simpleType name="listOfCustomers"><xs:restriction>

<xs:simpleType><xs:list>

<xs:simpleType><xs:restriction base="xs:string">

<xs:length value="5" /></xs:restriction>

</xs:simpleType></xs:list>

</xs:simpleType><xs:length value="3" />

</xs:restriction></xs:simpleType>

Page 10: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

How to Reference Simple TypesHow to Reference Simple Types

<xs:element name="listOfCustomers">

<xs:simpleType>

<xs:list itemType="xs:string" />

</xs:simpleType>

<xs:element>

<xs:element name="listOfCustomers">

<xs:simpleType>

<xs:list itemType="xs:string" />

</xs:simpleType>

<xs:element>

Page 11: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

<xs:attribute name="oldPlate"><xs:simpleType>

<xs:restriction base="xs:string"><xs:pattern value="[A-Z]{3}-\d{3}" />

</xs:restriction></xs:simpleType>

</xs:attribute><xs:attribute name="newPlate">

<xs:simpleType><xs:restriction base="xs:string">

<xs:pattern value="[A-Z]{3}-\d{3}" /></xs:restriction>

</xs:simpleType></xs:attribute>

<xs:attribute name="oldPlate"><xs:simpleType>

<xs:restriction base="xs:string"><xs:pattern value="[A-Z]{3}-\d{3}" />

</xs:restriction></xs:simpleType>

</xs:attribute><xs:attribute name="newPlate">

<xs:simpleType><xs:restriction base="xs:string">

<xs:pattern value="[A-Z]{3}-\d{3}" /></xs:restriction>

</xs:simpleType></xs:attribute>

Page 12: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

Better ideaBetter idea

<xs:attribute name="oldPlate" type="licensePlate" />

<xs:attribute name="newPlate" type="licensePlate" />

<xs:simpleType name="licensePlate">

<xs:restriction base="xs:string">

<xs:pattern value="[A-Z]{3}-\d{3}" />

</xs:restriction>

</xs:simpleType>

<xs:attribute name="oldPlate" type="licensePlate" />

<xs:attribute name="newPlate" type="licensePlate" />

<xs:simpleType name="licensePlate">

<xs:restriction base="xs:string">

<xs:pattern value="[A-Z]{3}-\d{3}" />

</xs:restriction>

</xs:simpleType>

Page 13: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

Complex DatatypesComplex Datatypes

<xs:element name="book"><xs:complexType>

<xs:sequence><xs:element name="title" type="xs:string" /><xs:element name="isbn" type="xs:string" /><xs:element name="publisher" type="xs:string"

/></xs:sequence>

</xs:complexType></xs:element>

<xs:element name="book"><xs:complexType>

<xs:sequence><xs:element name="title" type="xs:string" /><xs:element name="isbn" type="xs:string" /><xs:element name="publisher" type="xs:string"

/></xs:sequence>

</xs:complexType></xs:element>

Page 14: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

Explicit complex type for reuseExplicit complex type for reuse

<xs:complexType name="booktype"><xs:sequence>

<xs:element name="title" type="xs:string" /><xs:element name="isbn" type="xs:string" /><xs:element name="publisher" type="xs:string" />

</xs:sequence></xs:complexType>

<xs:element name="book" type="booktype" />

<xs:complexType name="booktype"><xs:sequence>

<xs:element name="title" type="xs:string" /><xs:element name="isbn" type="xs:string" /><xs:element name="publisher" type="xs:string" />

</xs:sequence></xs:complexType>

<xs:element name="book" type="booktype" />

Page 15: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

• Element that can only contain one of a predefined set

<xs:element name="contactmethod">

<xs:complexType>

<xs:choice>

<xs:element name="tel" type="xs:string" />

<xs:element name="email" type="xs:string" />

<xs:element name="fax" type="xs:string" />

</xs:choice>

</xs:complexType>

</xs:element>

• Element that can only contain one of a predefined set

<xs:element name="contactmethod">

<xs:complexType>

<xs:choice>

<xs:element name="tel" type="xs:string" />

<xs:element name="email" type="xs:string" />

<xs:element name="fax" type="xs:string" />

</xs:choice>

</xs:complexType>

</xs:element>

Page 16: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

• Elements that contain optional child elements in any order

<xs:element name="contactinfo"><xs:complexType>

<xs:all><xs:element name="telephone" type="xs:string" /><xs:element name="fax" type="xs:string" /><xs:element name="email" type="xs:string" /><xs:element name="pager" type="xs:string" />

</xs:all></xs:complexType>

</xs:element>

• Elements that contain optional child elements in any order

<xs:element name="contactinfo"><xs:complexType>

<xs:all><xs:element name="telephone" type="xs:string" /><xs:element name="fax" type="xs:string" /><xs:element name="email" type="xs:string" /><xs:element name="pager" type="xs:string" />

</xs:all></xs:complexType>

</xs:element>

Page 17: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

Complex Datatypes with Attributes

Complex Datatypes with Attributes

<xs:element name="employee"><xs:complexType>

<xs:sequence><xs:element name="name" type="xs:string" /><xs:element name="title" type="xs:string" /><xs:element name="salary" type="xs:integer" />

</xs:sequence><xs:attribute name="email" type="xs:string" /><xs:attribute name="department" type="xs:string" />

<xs:complexType></xs:element>

<xs:element name="employee"><xs:complexType>

<xs:sequence><xs:element name="name" type="xs:string" /><xs:element name="title" type="xs:string" /><xs:element name="salary" type="xs:integer" />

</xs:sequence><xs:attribute name="email" type="xs:string" /><xs:attribute name="department" type="xs:string" />

<xs:complexType></xs:element>

Page 18: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

Extend the base content by adding an attribute

Extend the base content by adding an attribute

<xs:element name="Length"><xs:complexType>

<xs:simpleContent><xs:extension base="xs:decimal">

<xs:attribute name="Units" type="xs:string" />

</xs:extension></xs:simpleContent>

</xs:complexType></xs:element>

<Length Units="Feet">12.75</Length>

<xs:element name="Length"><xs:complexType>

<xs:simpleContent><xs:extension base="xs:decimal">

<xs:attribute name="Units" type="xs:string" />

</xs:extension></xs:simpleContent>

</xs:complexType></xs:element>

<Length Units="Feet">12.75</Length>

Page 19: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

How to Reference Existing Declarations

How to Reference Existing Declarations

<xs:element name="email"><xs:simpleType>

<xs:restriction base="xs:string"><xs:pattern value="[a-zA-Z0-9]+@[a-zA-Z0-9]+" />

</xs:restriction></xs:simpleType>

</xs:element><xs:attribute name="title" type="xs:string" /><xs:element name="employee">

<xs:complexType><xs:sequence>

<xs:element name="name" type="xs:string" /><xs:element ref="email" />

</xs:sequence><xs:attribute ref="title" use="required" />

</xs:complexType></xs:element>

<xs:element name="email"><xs:simpleType>

<xs:restriction base="xs:string"><xs:pattern value="[a-zA-Z0-9]+@[a-zA-Z0-9]+" />

</xs:restriction></xs:simpleType>

</xs:element><xs:attribute name="title" type="xs:string" /><xs:element name="employee">

<xs:complexType><xs:sequence>

<xs:element name="name" type="xs:string" /><xs:element ref="email" />

</xs:sequence><xs:attribute ref="title" use="required" />

</xs:complexType></xs:element>

Page 20: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

<xs:element name="contactMethod"><xs:complexType>

<xs:choice><xs:element name="telephone">

<xs:simpleType><xs:restriction base="xs:string">

<xs:pattern value="(\d+|\s*)+" />

</xs:restriction></xs:simpleType>

</xs:element><xs:element ref="email" />

</xs:choice></xs:complexType>

</xs:element>

<xs:element name="contactMethod"><xs:complexType>

<xs:choice><xs:element name="telephone">

<xs:simpleType><xs:restriction base="xs:string">

<xs:pattern value="(\d+|\s*)+" />

</xs:restriction></xs:simpleType>

</xs:element><xs:element ref="email" />

</xs:choice></xs:complexType>

</xs:element>

Page 21: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

How to Create Element GroupsHow to Create Element Groups

<xs:group name="contactDetails"><xs:all>

<xs:element name="telephone" type="xs:string" /><xs:element name="email" type="xs:string" /><xs:element name="web" type="xs:anyURI" minOccurs="0" />

</xs:all></xs:group><xs:element name="employee">

<xs:complexType><xs:sequence>

<xs:element name="name" type="xs:string" /><xs:group ref="contactDetails" />

</xs:sequence></xs:complexType>

</xs:element>

<xs:group name="contactDetails"><xs:all>

<xs:element name="telephone" type="xs:string" /><xs:element name="email" type="xs:string" /><xs:element name="web" type="xs:anyURI" minOccurs="0" />

</xs:all></xs:group><xs:element name="employee">

<xs:complexType><xs:sequence>

<xs:element name="name" type="xs:string" /><xs:group ref="contactDetails" />

</xs:sequence></xs:complexType>

</xs:element>

Page 22: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

<xs:element name="company"><xs:complexType>

<xs:sequence><xs:element name="businessName" type="xs:string"

/><xs:element name="DUNS">

<xs:simpleType><xs:restriction base="xs:integer">

<xs:totalDigits value="9" />

</xs:restriction></xs:simpleType>

</xs:element><xs:element name="CEO" type="employee" /><xs:group ref="contactDetails" />

</xs:sequence></xs:complexType>

</xs:element>

<xs:element name="company"><xs:complexType>

<xs:sequence><xs:element name="businessName" type="xs:string"

/><xs:element name="DUNS">

<xs:simpleType><xs:restriction base="xs:integer">

<xs:totalDigits value="9" />

</xs:restriction></xs:simpleType>

</xs:element><xs:element name="CEO" type="employee" /><xs:group ref="contactDetails" />

</xs:sequence></xs:complexType>

</xs:element>

Page 23: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

How to Create Attribute GroupsHow to Create Attribute Groups

<xs:attributeGroup name="dimensions"><xs:attribute name="height" type="xs:integer" /><xs:attribute name="width" type="xs:integer" /><xs:attribute name="depth" type="xs:integer" />

</xs:attributeGroup><xs:element name="product">

<xs:complexType><xs:sequence>

<xs:element name="name" type="xs:string"><xs:element name="price" type="xs:decimal">

</xs:sequence><xs:attributeGroup ref="dimensions" />

</xs:complexType></xs:element>

<xs:attributeGroup name="dimensions"><xs:attribute name="height" type="xs:integer" /><xs:attribute name="width" type="xs:integer" /><xs:attribute name="depth" type="xs:integer" />

</xs:attributeGroup><xs:element name="product">

<xs:complexType><xs:sequence>

<xs:element name="name" type="xs:string"><xs:element name="price" type="xs:decimal">

</xs:sequence><xs:attributeGroup ref="dimensions" />

</xs:complexType></xs:element>

Page 24: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

Derive New Complex TypesDerive New Complex Types

<xs:complexType name="Address"><xs:sequence>

<xs:element name="street" type="xs:string" /><xs:element name="city" type="xs:string" />

</xs:sequence></xs:complexType><xs:complexType name="USAddress">

<xs:complexContent><xs:extension base="Address">

<xs:sequence><xs:element name="state" type="xs:string" /><xs:element name="zip" type="xs:integer" />

</xs:sequence></xs:extension>

<xs:complexContent></xs:complexType>

<xs:complexType name="Address"><xs:sequence>

<xs:element name="street" type="xs:string" /><xs:element name="city" type="xs:string" />

</xs:sequence></xs:complexType><xs:complexType name="USAddress">

<xs:complexContent><xs:extension base="Address">

<xs:sequence><xs:element name="state" type="xs:string" /><xs:element name="zip" type="xs:integer" />

</xs:sequence></xs:extension>

<xs:complexContent></xs:complexType>

Page 25: XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of

<xs:complexType name="NYAddress"><xs:complexContent>

<xs:restriction base="USAddress"><xs:sequence>

<xs:element name="street" type="xs:string" />

<xs:element name="city" type="xs:string" />

<xs:element name="state" type="xs:string“ fixed="NY" /><xs:element name="zip"

type="xs:integer" /></xs:sequence>

</xs:restriction><xs:complexContent>

</xs:complexType>

<xs:complexType name="NYAddress"><xs:complexContent>

<xs:restriction base="USAddress"><xs:sequence>

<xs:element name="street" type="xs:string" />

<xs:element name="city" type="xs:string" />

<xs:element name="state" type="xs:string“ fixed="NY" /><xs:element name="zip"

type="xs:integer" /></xs:sequence>

</xs:restriction><xs:complexContent>

</xs:complexType>