Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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-0/  http://www.w3.org/TR/xmlschema-1/ (Structures) http://www.w3.org/TR/xmlschema-1/  http://www.w3.org/TR/xmlschema-2/ (Datatypes) http://www.w3.org/TR/xmlschema-2/

5 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 Instance Document (.xml file) Schema (.xsd file) Schema Validator

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  Complex Elements – they contain other elements.. element content.

8 Schema – Example 1 Mary John Reminder Don't forget me this weekend! www.w3schools.com Mary John Reminder Don't forget me this weekend! www.w3schools.com

9 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 http://www.w3.org/2001/XMLSchema element complexType schema sequence string integer boolean

10 The Namespaces… Reference to the schema in the instance document Mary John Reminder Don't forget me this weekend! Reference to the schema in the instance document Mary John Reminder Don't forget me this weekend!

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

12 The Levels of Validation note.xmlnote.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

15 Schema – Example 2… Reusing declarations with the ref attribute

16 Schema – Example 2… Inline Element Declarations Anonymous type

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

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 Mary John Reminder Don't forget me this weekend! Instance Document Mary John Reminder Don't forget me this weekend!

22 Example –Complex (Named) Type Definition…

23 Simple Types

24  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:

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

27 An Anonymous Simple Type  This simple type cannot be reused as it is part of the element declaration

28 Summary – Declaring Elements A simple type (e.g., xs:string) or the name of a complexType (e.g., noteType) … … <xs:element name="name" type="type" minOccurs="int" maxOccurs="int"/> <xs:element name="name" type="type" minOccurs="int" maxOccurs="int"/> A nonnegative integer A nonnegative integer or the value “unbounded”

29 Summary – Declaring Elements… … …

30 Summary – Defining Reusable Types … … … …

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 category="technical" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "book.xsd"> XML Schemas 200-ABCD 12.5 <book category="technical" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "book.xsd"> XML Schemas 200-ABCD 12.5 Book.xml

32 Schema Attributes… Book.xsd

33 Attribute Defaults  Attributes can have either a default value OR a fixed value specified

34 Attribute Occurrence  Attribute occurrence (once or none) is indicated by use attribute  Values for use attribute: – required, optional, prohibited – Default value is optional

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

38 Element with Text and Attribute 347563256

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

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 <PersonInfo xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "personinfo-all.xsd"> Ram Executive Sales ram@abc.com 8723453194 Sham Manager Sales 8723453195 sham@abc.com <PersonInfo xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "personinfo-all.xsd"> Ram Executive Sales ram@abc.com 8723453194 Sham Manager Sales 8723453195 sham@abc.com

42 Element Occurrence Indicator – All…

43 Element Occurrence Indicator – Choice <employees xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "employee-choice.xsd"> Ram Executive 20000 Sham Manager 30000 <employees xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "employee-choice.xsd"> Ram Executive 20000 Sham Manager 30000

44 Element Occurrence Indicator – Choice… <employees xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "employee-choice.xsd"> Anil 500 2005-06-30 Sunil 500 2005-07-30 <employees xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "employee-choice.xsd"> Anil 500 2005-06-30 Sunil 500 2005-07-30

45 Element Occurrence Indicator – Choice…

46 Validation Tools  http://tools.decisionsoft.com/schemaValidate.html http://tools.decisionsoft.com/schemaValidate.html  http://apps.gotdotnet.com/xmltools/xsdvalidator/De fault.aspx http://apps.gotdotnet.com/xmltools/xsdvalidator/De fault.aspx


Download ppt "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."

Similar presentations


Ads by Google