Download presentation
Presentation is loading. Please wait.
Published byLee Osborne Modified over 8 years ago
1
XML Schemas Dr. Awad Khalil Computer Science Department AUC
2
What is an XML Schema? XML Schema is an XML based alternative to DTD. XML Schema is an XML based alternative to DTD. An XML schema describes the structure of an XML document. An XML schema describes the structure of an XML document. The XML Schema language is also referred to as XML Schema Definition (XSD). The XML Schema language is also referred to as XML Schema Definition (XSD).
3
The purpose of XML Schema Defines elements that can appear in a document Defines elements that can appear in a document Defines attributes that can appear in a document Defines attributes that can appear in a document Defines which elements are child elements Defines which elements are child elements Defines the order of child elements Defines the order of child elements Defines the number of child elements Defines the number of child elements
4
The purpose of XML Schema (Cont’d) Defines whether an element is empty or can include text Defines whether an element is empty or can include text Defines data types for elements and attributes Defines data types for elements and attributes Defines default and fixed values for elements and attributes Defines default and fixed values for elements and attributes Provides a rich set of primitive datatypes Provides a rich set of primitive datatypes Provides a datatype system that allows built-in and user-defined datatypes to be created. Provides a datatype system that allows built-in and user-defined datatypes to be created.
5
XML Schemas vs. DTDs XML Schemas support data types XML Schemas support data types XML Schemas are written in XML XML Schemas are written in XML XML Schemas support namespaces XML Schemas support namespaces XML Schemas Secure Data Communication XML Schemas Secure Data Communication XML Schemas provide more detailed and robust content models XML Schemas provide more detailed and robust content models XML Schemas provide multiple vocabulary support as well as dynamic schemas XML Schemas provide multiple vocabulary support as well as dynamic schemas XML Schemas are extensible to future additions XML Schemas are extensible to future additions XML Schemas are richer and more useful than DTDs XML Schemas are richer and more useful than DTDs
6
XML Schemas support data types
7
XML Schemas support data types (cont’d) Easier to describe permissible document content Easier to describe permissible document content Easier to validate the correctness of data Easier to validate the correctness of data Easier to work with data from a database Easier to work with data from a database Easier to define data facets (restrictions on data) Easier to define data facets (restrictions on data) Easier to convert data between different data types Easier to convert data between different data types
8
XML Schemas are written in XMLDTD: XML Schema:
9
XML Schemas are written in XML (Cont’d) You don't have to learn another language You don't have to learn another language You can use your XML editor to edit your Schema files You can use your XML editor to edit your Schema files You can use your XML parser to parse your Schema files You can use your XML parser to parse your Schema files You can manipulate your Schema with the XML DOM You can manipulate your Schema with the XML DOM You can transform your Schema with XSLT You can transform your Schema with XSLT
10
XML Schemas support Namespaces Widget Dave
11
XML Schemas Secure Data Communication A date like 1999-03-11 might (in some countries) be interpreted as 3. November or (in some other countries) as 11. March. A date like 1999-03-11 might (in some countries) be interpreted as 3. November or (in some other countries) as 11. March. 1999-03-11 1999-03-11 ensures a mutual understanding of the content because the XML data type date requires the format CCYY-MM-DD. ensures a mutual understanding of the content because the XML data type date requires the format CCYY-MM-DD.
12
XML Schemas provide more detailed and robust content models DTDs: Simple sequence or choice lists Simple sequence or choice lists Can’t use these with mixed content Can’t use these with mixed content Can only specify zero, one, or many occurrences of an element. Can only specify zero, one, or many occurrences of an element. No named element or attribute groups. No named element or attribute groups. XML schemas: Provide more detailed content models Provide more detailed content models Mutually exclusive with mixed content Mutually exclusive with mixed content Can specify number of occurrences and named model groups. Can specify number of occurrences and named model groups.
13
Provides multiple vocabulary support & dynamic schemas In DTDs: One DTD per document is used. One DTD per document is used. XML Schemas : As many schemas as needed can be used per document. As many schemas as needed can be used per document. Let us use dynamic subsets of standardized schemas, that is, schema portions that are selected in response to some user input. Let us use dynamic subsets of standardized schemas, that is, schema portions that are selected in response to some user input. For example (perhaps one like our BookCatalog example) For example (perhaps one like our BookCatalog example)
14
XML Schemas are Extensible Reuse your Schema in other Schemas Reuse your Schema in other Schemas Create your own data types derived from standard types Create your own data types derived from standard types Reference multiple schemas from the same document Reference multiple schemas from the same document Examples: Official Post Office schema for mailing addresses Official Post Office schema for mailing addresses ITU schema for telephone numbers ITU schema for telephone numbers IETF schema for Internet and e-mail addresses, IETF schema for Internet and e-mail addresses,
15
XML Tove Jani Reminder Don't forget me this weekend!
16
DTD
17
XML Schema <xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchemahttp://www.w3.org/2001/XMLSchema targetNamespace=http://www.w3schools.comhttp://www.w3schools.com xmlns=http://www.w3schools.com>http://www.w3schools.com
18
A Reference to a DTD Tove Jani Reminder Don't forget me this weekend!
19
A Reference to an XML Schema <note xmlns=http://www.w3schools.comhttp://www.w3schools.com xmlns:xsi=http://www.w3.org/2001/XMLSchema-instancehttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation= "http://www.w3schools.com/schema/note.xsd"> Tove Jani Reminder Don't forget me this weekend!
20
An XML Schema Declaration <xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchemahttp://www.w3.org/2001/XMLSchema targetNamespace=http://www.w3schools.comhttp://www.w3schools.com xmlns=http://www.w3schools.com elementFormDefault=http://www.w3schools.com "qualified">......
21
Simple XSD Element XSD Simple Elements XSD Simple Elements XML Schemas define the elements of your XML files. XML Schemas define the elements of your XML files. A simple element is an XML element that can contain only text. It cannot contain any other elements or attributes. A simple element is an XML element that can contain only text. It cannot contain any other elements or attributes. Simple elements cannot have attributes. If an element has attributes, it is considered to be of complex type. But the attribute itself is always declared as a simple type. This means that an element with attributes always has a complex type definition. Simple elements cannot have attributes. If an element has attributes, it is considered to be of complex type. But the attribute itself is always declared as a simple type. This means that an element with attributes always has a complex type definition.
22
How to Define a Simple Element XML elements: Refsnes 34 1968-03-27 Corresponding simple element definitions:
23
Common XML Schema Data Types Here is a list of the most common types: xs:string xs:decimal xs:integer xs:boolean xs:date xs:time
24
Declare Default and Fixed Values for Simple ElementsDefault: Fixed: The same can be applied to attributes The same can be applied to attributes
25
Creating Optional and Required Attributes All attributes are optional by default All attributes are optional by defaultOptional: Required:
26
XSD Restrictions/Facets With XML Schemas, you can add your own restrictions to your XML elements and attributes. These restrictions are called facets. With XML Schemas, you can add your own restrictions to your XML elements and attributes. These restrictions are called facets. Restrictions are used to control acceptable values for XML elements or attributes. Restrictions are used to control acceptable values for XML elements or attributes.
27
Restrictions on Values
28
Restrictions on a Set of Values
29
Restrictions on a Series of Values
30
Other Restrictions on a Series of Values
31
Restrictions on White Space Characters
32
Restrictions on Length
33
Restrictions for Datatypes Enumeration Enumeration Fraction Fraction Digits Digits Lengthmax Lengthmax Exclusivemax Exclusivemax Inclusivemax Inclusivemax Lengthmin Lengthmin Exclusivemin Exclusivemin Inclusivemin Inclusivemin Lengthpattern Lengthpattern totalDigits totalDigits whiteSpace whiteSpace
34
Complex XSD Element Provide a mechanism to validate a document instance containing type Describe element attribute existence and content. Describe content of element type Derive its definition from another simple or complex type Control ability to derive additional types
35
Defining a Complex Element 1) Element can be declared by naming the element 2) By having an attribute for an element that reference complextype name as follows:
36
Complex type definition types 1) Empty elements: An empty XML element: An empty XML element: An empty XML schema element: An empty XML schema element:
37
Complex type definition types 2) Element only: XML document: John Smith An XML schema document:
38
Complex type definition types 3)Text Only: XML document: XML document: 35 An XML schema document: An XML schema document:
39
Complex type definition types 4 ) Mixed: An XML Document: Dear Mr. John Smith. Your order 1032 will be shipped on 2001-07-13. An XML Schema:
40
XSD Complex Types Indicators 1)Order indicators : :
41
XSD Complex Types Indicators : :
42
XSD Complex Types Indicators : :
43
XSD Complex Types Indicators 2)Occurrence indicators : :
44
XSD Complex Types Indicators<minOccurs>:
45
XSD Complex Types Indicators 3)Group indicators Group name: Group name:
46
XSD Complex Types Indicators …… >
47
Extending the XML documentAny:
48
How XML Schema is designed? To create the schema we could simply follow the structure in the XML document and define each element as we find it. To create the schema we could simply follow the structure in the XML document and define each element as we find it. In the next design method, the schema is divided by defining all elements and attributes first, and then reference them using the ref attribute. In the next design method, the schema is divided by defining all elements and attributes first, and then reference them using the ref attribute. The third design method defines classes or types, that enables us to reuse element definitions. This is done by naming the simpleTypes and complexTypes elements, and then point to them through the type attribute of the element. The third design method defines classes or types, that enables us to reuse element definitions. This is done by naming the simpleTypes and complexTypes elements, and then point to them through the type attribute of the element.
49
Commercial XML editors IBM (XML4C and XML4J can use XML Schema to validate documents) IBM (XML4C and XML4J can use XML Schema to validate documents) Oracle’s XML parser for Java v2 Oracle’s XML parser for Java v2 XML Spy 3.0 XML Spy 3.0 XML Authority 1.2 XML Authority 1.2
50
Thank You
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.