470 likes | 739 Views
XML Schema. Vinod Kumar Kayartaya. What is XML Schema?. XML Schema is an XML based alternative to DTD An XML schema describes the structure of an XML document The purpose of an XML Schema is to define the legal building blocks of an XML document, just like a DTD
E N D
XML Schema Vinod Kumar Kayartaya
What is XML Schema? • XML Schema is an XML based alternative to DTD • An XML schema describes the structure of an XML document • The purpose of an XML Schema is to define the legal building blocks of an XML document, just like a DTD • XML Schemas support data types, which DTDs do not • The XML Schema language is also referred to as XML Schema Definition (XSD) • XML documents can have a reference to a DTD or an XML Schema
What Does XML Schema Define? • An XML Schema: • defines elements that can appear in a document • defines attributes that can appear in a document • defines which elements are child elements • defines the sequence in which the child elements can appear • defines the number of child elements • defines whether an element is empty or can include text • defines data types for elements and attributes • defines default values for elements and attributes
XML Schema Specification • XML Schema specification consists of two parts: • Structures • Datatypes • http://www.w3.org/TR/xmlschema-0/(Primer) • http://www.w3.org/TR/xmlschema-1/ (Structures) • http://www.w3.org/TR/xmlschema-2/ (Datatypes)
Instance Document (.xml file) Schema Validator Schema (.xsd file) XML Terms • Instance Document – an XML document that is valid according to a particular schema • Schema – the XML document that has structural rules defined for an XML document
Building Blocks of Schema • Element declarations • Complex type definitions • Simple type definitions • Attribute declarations
Schema Elements • Simple Elements: • they do not contain other elements <xs:element name="name" type="type"/> • Complex Elements • they contain other elements <xs:element name="name"> <xs:complexType> . . element content . </xs:complexType> </xs:element>
Schema – Example 1 <!ELEMENT note (to, from, heading, body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> <?xml version="1.0"?> <note> <to>Mary</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> www.w3schools.com <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="note"> <xsd:complexType> <xsd:sequence> <xsd:element name="to" type="xsd:string"/> <xsd:element name="from" type="xsd:string"/> <xsd:element name="heading" type="xsd:string"/> <xsd:element name="body" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
http://www.w3.org/2001/XMLSchema complexType element sequence schema boolean string integer The Namespaces • XML Schema Namespace • The elements and datatypes that are used to construct schemas (schema, element, complexType, sequence, string, etc.) come from the http://www.w3.org/2001/XMLSchema namespace
The Namespaces… Reference to the schema in the instance document <?xml version="1.0"?> <note xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "note.xsd"> <to>Mary</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
http://www.w3.org/2001/XMLSchema-instance schemaLocation type noNamespaceSchemaLocation nil The Namespaces… • XML Schema-instance Namespace
The Levels of Validation note.xml note.xsd XMLSchema.xsd (schema-for-schemas) Validate that note.xml conforms to the rules described in note.xsd Validate that note.xsd is a valid schema document, i.e., it conforms to the rules described in the schema-for-schemas
Common XML Schema Data Types • XML Schema has a lot of built-in data types. Some of them: • xs:string • xs:decimal • xs:integer • xs:boolean • xs:date • xs:time
Schema – Example 2 <!ELEMENT BookStore (Book)+> <!ELEMENT Book (Title, Author, Date, ISBN, Publisher)> <!ELEMENT Title (#PCDATA)> <!ELEMENT Author (#PCDATA)> <!ELEMENT Date (#PCDATA)> <!ELEMENT ISBN (#PCDATA)> <!ELEMENT Publisher (#PCDATA)>
Schema – Example 2… <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <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> Reusing declarations with the ref attribute
Schema – Example 2… <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema“> <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <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:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> Anonymous type Inline Element Declarations
Occurrence Constraints on Elements • The attributes minOccurs and maxOccurs can be used to indicate the number of times an element can appear • The default value for both the attributes minOccurs and maxOccurs is 1. If both the attributes are omitted, the element should appear exactly once.
Default and Fixed Values for Simple Elements • Elements can have either a default value OR a fixed value set • A default value is automatically assigned to the element when no other value is specified • A fixed value is also automatically assigned to the element when no other value is specified • But, unlike default values, if you specify another value than the fixed value, the document is considered invalid <xs:element name="color" type="xs:string" default="red"/> <xs:element name="color" type="xs:string" fixed="red"/>
Complex Types – Named Types • The complex type definitions create new complex types, which can be reused in the element declarations • New complex types are defined using the complexType element • ComplexType definition typically contains a set of • element declarations, • element references, and • attributes declarations
Example –Complex (Named) Type Definition Instance Document <?xml version="1.0"?> <note> <to>Mary</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
Example –Complex (Named) Type Definition… <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="note" type="noteType"/> <xs:complexType name="noteType"> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema>
Simple Types • Simple types could be • Built-in simple types (xs:string, xs:int, etc.) • Those derived from simple types or existing derived ones • Deriving can be done by restricting the values that the new type can take on, as a subset of the values of the existing simple type • Restricting the range of values for the new simple type can be done with the xsd:restriction element • Facets constrain the values
Derived Simple Types • A new simple type salaryType derived from the integer type: <xsd:element name="salary" type="salaryType"/> <xsd:simpleType name="salaryType"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="5000"/> <xsd:maxInclusive value="10000"/> </xsd:restriction> </xsd:simpleType>
Derived Simple Types… • A new simple type bookType derived using the enumeration facet The type "carType" can be used by other elements because it is not a part of the "car" element <xs:element name="book" type="bookType"/> <xs:simpleType name="bookType"> <xs:restriction base="xs:string"> <xs:enumeration value="Technical"/> <xs:enumeration value="Managerial"/> <xs:enumeration value="Fiction"/> </xs:restriction> </xs:simpleType>
An Anonymous Simple Type • This simple type cannot be reused as it is part of the element declaration <xs:element name="book"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Technical"/> <xs:enumeration value="Managerial"/> <xs:enumeration value="Fiction"/> </xs:restriction> </xs:simpleType> </xs:element>
Summary – Declaring Elements <xs:element name="name" type="type" minOccurs="int" maxOccurs="int"/> A simple type (e.g., xs:string) or the name of a complexType (e.g., noteType) A nonnegative integer A nonnegative integer or the value “unbounded” <xs:element name="name" minOccurs="int" maxOccurs="int"> <xs:complexType> … </xs:complexType> </xs:element>
Summary – Declaring Elements… <xs:element name="name" minOccurs="int" maxOccurs="int"> <xs:simpleType> … </xs:simpleType> </xs:element>
Summary – Defining Reusable Types <xs:complexType name="name"> <xs:sequence> … </xs:sequence> </xs:complexType> <xs:simpleType name="name"> <xs:restriction> … </xs:restriction> </xs:simpleType>
Schema Attributes • An element that has an attribute should always be declared as complex type • The attribute declaration must reference simple types always Book.xml <?xml version="1.0"?> <book category="technical" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "book.xsd"> <title>XML Schemas</title> <ISBN>200-ABCD</ISBN> <price currency="USD">12.5</price> </book>
Schema Attributes… <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="book" type="bookType"/> <xs:complexType name="bookType"> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="ISBN" type="ISBNType"/> <xs:element name="price" type="priceType"/> </xs:sequence> <xs:attribute name="category" type="xs:string"/> </xs:complexType> <xs:simpleType name="ISBNType"> <xs:restriction base="xs:string"> <xs:pattern value="\d{3}-[A-Z]{4}"/> </xs:restriction> </xs:simpleType> <xs:complexType name="priceType"> <xs:simpleContent> <xs:extension base="xs:decimal"> <xs:attribute name="currency" type="xs:string"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:schema> Book.xsd
Attribute Defaults • Attributes can have either a default value OR a fixed value specified <xs:attribute name="currency" type="xs:string" default="INR"/> <xs:attribute name="currency" type="xs:string" fixed="INR"/>
Attribute Occurrence • Attribute occurrence (once or none) is indicated by use attribute • Values for use attribute: • required, optional, prohibited • Default value is optional <xs:attribute name="currency" type="xs:string" use="optional"/> <xs:attribute name="currency" type="xs:string" use="required"/>
Complex Elements – More Examples • Element Contents • Elements containing only a simple type value (text) • Elements containing other elements • Elements containing other elements and attributes • Empty elements (with or without attributes) • Elements containing simple type value (text) and attributes
Empty Elements • The existence of an attribute makes an element complex type, though it may not contain any content at all <phone ftype="home" number="238654"/> <xs:element name="phone"> <xs:complexType> <xs:attribute name="ftype" type="xs:string"/> <xs:attribute name="number" type="xs:string"/> </xs:complexType> </xs:element>
Element with Text and Attribute <phone ftype="home">347563256</phone> <xs:element name="phone"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="ftype" type="xs:string"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element>
Using anyType • anyType allows for no constraints on the content at all • If you do not specify any type in the declaration, it defaults to anyType <xs:element name="elem1" type="xs:anyType"/> <xs:element name="elem1"/>
Element Occurrence – Order Indicators • You can use the following indicators to mention how the elements should occur within a complex element/type • All • Indicates that the child elements can appear in any order, but each can occur at most once (minOccurs=0, maxOccurs=1) • Choice • Indicates either of the child elements only can occur • Sequence • Indicates that the child elements should occur only in the order specified
Element Occurrence Indicator - All <?xml version="1.0"?> <PersonInfo xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "personinfo-all.xsd"> <person> <name>Ram</name> <title>Executive</title> <dept>Sales</dept> <email>ram@abc.com</email> <phone>8723453194</phone> </person> <person> <name>Sham</name> <title>Manager</title> <dept>Sales</dept> <phone>8723453195</phone> <email>sham@abc.com</email> </person> </PersonInfo>
Element Occurrence Indicator – All… <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="PersonInfo"> <xs:complexType> <xs:sequence> <xs:element name="person" maxOccurs="unbounded"> <xs:complexType> <xs:all> <xs:element name="name" type="xs:string"/> <xs:element name="title" type="xs:string"/> <xs:element name="dept" type="xs:string"/> <xs:element name="email" type="xs:string"/> <xs:element name="phone" type="xs:string"/> </xs:all> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element></xs:schema>
Element Occurrence Indicator – Choice <?xml version="1.0"?> <employees xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "employee-choice.xsd"> <employee> <name>Ram</name> <title>Executive</title> <salary>20000</salary> </employee> <employee> <name>Sham</name> <title>Manager</title> <salary>30000</salary> </employee> </employees>
Element Occurrence Indicator – Choice… <?xml version="1.0"?> <employees xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "employee-choice.xsd"> <contract> <name>Anil</name> <rate>500</rate> <valid-till>2005-06-30</valid-till> </contract> <contract> <name>Sunil</name> <rate>500</rate> <valid-till>2005-07-30</valid-till> </contract> </employees>
Element Occurrence Indicator – Choice… <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="employees"> <xs:complexType> <xs:choice> <xs:element name="employee" type="regularType" maxOccurs="unbounded"/> <xs:element name="contract" type="contractType" maxOccurs="unbounded"/> </xs:choice> </xs:complexType> </xs:element> <xs:complexType name="regularType"> <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:complexType> <xs:complexType name="contractType"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="rate" type="xs:integer"/> <xs:element name="valid-till" type="xs:date"/> </xs:sequence> </xs:complexType> </xs:schema>