4 XML Schema
XML Schema Based on XML grammar, tools and technology Alternative to DTD syntax XML Schema (W3C) Definition in accordance with the XML syntax Allows the definition of complex data types
Validation Content Model Validation DTD Content Model Validation XML Schema Part 1: Structures XML Schema (W3C) Content Model Validation Datatype Validation XML Schema Part 2: Datatypes
Features Data typing concept Data types for attributes and element content User-defined data types Detailed and flexible content modeling Attribute and element grouping Refinement of content models, inheritance Modularization Self-documentation Namespace support
Example <?xml version="1.0" encoding="utf-8"?> <book isbn="0836217462"> <title>Being a Dog Is a Full-Time Job</title> <author>Charles M. Schulz</author> <character> <name>Snoopy</name> <friend-of>Peppermint Patty</friend-of> <since>1950-10-04</since> <qualification>extroverted beagle</qualification> </character> <name>Peppermint Patty</name> <since>1966-08-22</since> <qualification>bold, brash and tomboyish</qualification> </book> library.xml
Elements and Attributes <?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> .... Element Declaration <xsd:element name="name" type="xsd:string"/> <xsd:element name="friend-of" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="since" type="xsd:date"/> .... Built-in simple Type Cardinality <xsd:attribute name="isbn" type="xsd:string"/> .... Attribute Declaration </xsd:schema>
Simple Data Types string normalizedString boolean byte number long Built-in simple Types string boolean number float double decimal duration datetime time date ... normalizedString byte long int short positiveInteger negativeInteger unsignedLong unsignedShort ... Built-in Types derived from simple Types
"Russian Doll Design" Schema <?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <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="character" minOccurs="0" maxOccurs="unbounded"> <xsd:element name="name" type="xsd:string"/> <xsd:element name="friend-of" type="xsd:string“ minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="since" type="xsd:date"/> <xsd:element name="qualification" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:attribute name="isbn" type="xsd:string"/> </xsd:schema> Element of type complexType Compositor
"Salami Slice Design" Schema <?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns:xsd=“http://www.w3.org/2001/XMLSchema“ <xsd:element name="title" type="xsd:string"/> <xsd:element name="author" type="xsd:string"/> <xsd:element name="name" type="xsd:string"/> <xsd:element name="friend-of" type="xsd:string"/> <xsd:element name="since" type="xsd:date"/> <xsd:element name="qualification" type="xsd:string"/> <xsd:attribute name="isbn" type="xsd:string"/> Definition of Elements of type "simple" Definition of Attributes
"Salami Slice Design" Schema <xsd:element name="character"> <xsd:complexType> <xsd:sequence> <xsd:element ref="name"/> <xsd:element ref="friend-of" minOccurs="0" maxOccurs="unbounded"/> <xsd:element ref="since"/> <xsd:element ref="qualification"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="book"> <xsd:element ref="title"/> <xsd:element ref="author"/> <xsd:element ref="character" <xsd:attribute ref="isbn"/> </xsd:schema> Definition of "complexType" Elements
Schema with "Named Types" <?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema"> <xsd:simpleType name="nameType"> <xsd:restriction base="xsd:string"> <xsd:maxLength value="32"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="sinceType"> <xsd:restriction base="xsd:date"/> <xsd:simpleType name="descType"> <xsd:restriction base="xsd:string"/> <xsd:simpleType name="isbnType"> <xsd:pattern value="[0-9]{10}"/> Facet Definition of "named simpleTypes" Facet
... Schema with "Named Types" <xsd:complexType name="characterType"> <xsd:sequence> <xsd:element name="name" type="nameType"/> <xsd:element name="friend-of" type="nameType" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="since" type="sinceType"/> <xsd:element name="qualification" type="descType"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="bookType"> <xsd:element name="title" type="nameType"/> <xsd:element name="author" type="nameType"/> <xsd:element name="character" type="characterType" <xsd:attribute name="isbn" type="isbnType" use="required"/> <xsd:element name="book" type="bookType"/> </xsd:schema> Definition of "named complexTypes" Declaration of book Element
Grouping Attributes and Elements <xsd:group name="mainBookElements"> <xsd:sequence> <xsd:element name="title" type="nameType"/> <xsd:element name="author" type="nameType"/> </xsd:sequence> </xsd:group> <xsd:attributeGroup name="bookAttributes"> <xsd:attribute name="isbn" type="isbnType" use="required"/> <xsd:attribute name="available" type="xsd:string"/> </xsd:attributeGroup> <xsd:complexType name="bookType"> <xsd:sequence> <xsd:group ref="mainBookElements"/> <xsd:element name="character“ type="characterType" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> <xsd:attributeGroup ref="bookAttributes"/> </xsd:complexType>
Compositors <xsd:group name="nameTypes"> <xsd:choice> <xsd:element name="name" type="xsd:string"/> <xsd:sequence> <xsd:element name="firstName" type="xsd:string"/> <xsd:element name="middleName" type="xsd:string" minOccurs="0"/> <xsd:element name="lastName" type="xsd:string"/> </xsd:sequence> </xsd:choice> </xsd:group> <xsd:complexType name="bookType"> <xsd:all> <xsd:element name="title" type="xsd:string"/> <xsd:element name="author" type="xsd:string"/> <xsd:element name="character" type="characterType" minOccurs="0" maxOccurs="unbounded"/> </xsd:all> <xsd:attribute name="isbn" type="isbnType" use="required"/> </xsd:complexType>
Derivations from Simple Types xsd:restriction xsd:maxLength, xsd:minLength xsd:minInclusive,xsd:maxInclusive xsd:totalDigits, xsd:fractionDigits .... xsd:pattern xsd:union xsd:list ... Facets
Content Types Empty Content with Attribute <xsd:element name="book"> <xsd:complexType> <xsd:attribute name="isbn" type="isbnType"/> </xsd:complexType> </xsd:element> Empty Content with Attribute <xsd:element name="book"> <xsd:complexType> <xsd:simpleContent> <xsd:extension base="xsd:string"> <xsd:attribute name="isbn" type="isbnType"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> </xsd:element> Character Data Content with Attribute
Mixed Content <xsd:element name="book"> <xsd:complexType mixed="true"> <xsd:all> <xsd:element name="title" type="xsd:string"/> <xsd:element name="author" type="xsd:string"/> </xsd:all> <xsd:attribute name="isbn" type="xsd:string"/> </xsd:complexType> </xsd:element> <book isbn="0836217462"> Funny book by <author>Charles M. Schulz</author>. Its title (<title>Being a Dog Is a Full-Time Job</title>) says it all! </book>
Documentation <xsd:element name="book"> <xsd:annotation> <xsd:documentation xml:lang="en"> Top level element. </xsd:documentation> <xsd:documentation xml:lang="fr"> Element racine </xsd:documentation> <xsd:appinfo source="http://example.com/foo/"> <bind xmlns="http://example.com/bar/"> <class name="Book"/> </bind> </xsd:appinfo> </xsd:annotation> ...
Namespaces library.xsd library.xml <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://example.org/ns/books/" targetNamespace="http://example.org/ns/books/" elementFormDefault="unqualified" attributeFormDefault="unqualified" > .... </xsd:schema> library.xsd <book isbn="0836217462" xmlns="http://example.org/ns/books/"> <title> Being a Dog is a Full-Time Job </title> ... </book> library.xml
XML Schema and Instances <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" .... </xsd:schema> library.xsd without targetNamespace <book isbn="0836217462" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="library.xsd"> <title> Being a Dog is a Full-Time Job </title> ... </book> library.xml
... XML Schema and Instances <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://example.org/ns/books/" targetNamespace="http://example.org/ns/books/" .... </xsd:schema> library.xsd with targetNamespace <book isbn="0836217462" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://example.org/ns/books/" xsi:schemaLocation="http://example.org/ns/books/ library.xsd"> <title> Being a Dog is a Full-Time Job </title> ... </book> library.xml
Summary Schema languages apply and enhance the concepts of DTDs to a more modern and consistent level. XML Schema can construct very complex content models. Moreover, it supports a complex datatyping concept.