1 / 45

XML Schema

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

Download Presentation

XML Schema

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XML Schema Vinod Kumar Kayartaya

  2. 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

  3. 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

  4. 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)

  5. 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

  6. Building Blocks of Schema • Element declarations • Complex type definitions • Simple type definitions • Attribute declarations

  7. 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>

  8. 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>

  9. 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

  10. 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>

  11. http://www.w3.org/2001/XMLSchema-instance schemaLocation type noNamespaceSchemaLocation nil The Namespaces… • XML Schema-instance Namespace

  12. 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

  13. 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

  14. 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)>

  15. 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

  16. 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

  17. 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.

  18. 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"/>

  19. Complex Types

  20. 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

  21. 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>

  22. 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>

  23. Simple Types

  24. 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

  25. 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>

  26. 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>

  27. 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>

  28. 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>

  29. Summary – Declaring Elements… <xs:element name="name" minOccurs="int" maxOccurs="int"> <xs:simpleType> … </xs:simpleType> </xs:element>

  30. Summary – Defining Reusable Types <xs:complexType name="name"> <xs:sequence> … </xs:sequence> </xs:complexType> <xs:simpleType name="name"> <xs:restriction> … </xs:restriction> </xs:simpleType>

  31. 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>

  32. 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

  33. 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"/>

  34. 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"/>

  35. Complex Types – More Element Content Models

  36. 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

  37. 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>

  38. 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>

  39. 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"/>

  40. 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

  41. 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>

  42. 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>

  43. 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>

  44. 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>

  45. 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>

More Related