XML Schema

4,075 views

Published on

Incompleted

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,075
On SlideShare
0
From Embeds
0
Number of Embeds
76
Actions
Shares
0
Downloads
337
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

XML Schema

  1. 1. XML Schema XML http://yht4ever.blogspot.com [email_address] B070066 - NIIT Quang Trung 08/2007
  2. 2. Contents Create group of element and attribute Attribute in XML Schemas Elements in XML Schemas Data Types in XML Schemas Introduction to XML Schema
  3. 3. Introduction to XML Schema <ul><li>An XML schema defines the list of elements and attributes that can be used in an XML document. </li></ul><ul><li>It also specifies the order in which these elements appear in the XML document, and their data types. </li></ul><ul><li>Microsoft has developed the XML Schema Definition (XSD) language to define the schema of an XML document </li></ul>
  4. 4. Advantages of XML Schema <ul><li>XSD provides more control over the type of data that can be assigned to elements and attributes as compared to DTD. </li></ul><ul><li>XSD enables you to create your own data types. </li></ul><ul><li>XSD enables you to specify restrictions on data. </li></ul><ul><li>The syntax for defining an XSD is the same as the syntax used for XML documents. </li></ul><ul><li>XML schema content models can be used to validate mixed content, which cannot be validated by DTD content models. </li></ul><ul><li>XML schema is extensible. </li></ul>
  5. 5. Data Types in XML Schemas <ul><li>A data type specifies the type of content that an element can hold. </li></ul><ul><li>XSD provides a list of predefined data types. These data types can be classified as follows: </li></ul><ul><ul><li>Primitive: Are the fundamental data types of XSD. </li></ul></ul><ul><ul><li>Derived: Are defined by using other data types called base types. </li></ul></ul><ul><ul><li>Atomic: Are primitive or derived data types that cannot be broken down into smaller units. </li></ul></ul><ul><ul><li>List: Are derived data types that contain a set of values of atomic data types. </li></ul></ul><ul><ul><li>Union: Are derived from the atomic and list data types. </li></ul></ul>
  6. 6. Elements in XML Schemas <ul><li>There are two types of elements, simple and complex. </li></ul><ul><ul><li>Simple Element </li></ul></ul><ul><ul><ul><li>A simple element does not contain any child elements or attributes. </li></ul></ul></ul><ul><ul><ul><li>The syntax for declaring a simple element is as follows: </li></ul></ul></ul><ul><ul><ul><li><xsd:element name=&quot;element-name&quot; type=&quot;data type&quot; minOccurs=&quot;nonNegativeInteger&quot; maxOccurs=&quot;nonNegativeInteger|unbounded&quot;/> </li></ul></ul></ul>
  7. 7. Example <ul><li><PRODUCTDATA> </li></ul><ul><li><PRODUCT PRODUCTID=&quot;P001&quot; CATEGORY=&quot;BOOKS&quot;> </li></ul><ul><li><PRODUCTNAME>Gone With the Wind</PRODUCTNAME> </li></ul><ul><li><DESCRIPTION> The backdrop of this book is the American Civil War</DESCRIPTION> </li></ul><ul><li><PRICE>25.00</PRICE> </li></ul><ul><li> <QUANTITY>35</QUANTITY> </li></ul><ul><li></PRODUCT> </li></ul><ul><li></PRODUCTDATA> </li></ul><ul><li><xsd:element name=&quot;PRODUCTNAME&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li><xsd:element name=&quot;DESCRIPTION&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li><xsd:element name=&quot;PRICE&quot; type=&quot;xsd:positiveInteger&quot;/> </li></ul><ul><li><xsd:element name=&quot;QUANTITY&quot; type=&quot;xsd:nonNegativeInteger&quot;/> </li></ul>
  8. 8. Elements in XML Schemas (cont.) <ul><ul><li>Complex Element </li></ul></ul><ul><ul><ul><li>A complex element contains other elements, attributes, and mixed content. </li></ul></ul></ul><ul><ul><ul><li>The syntax to define a complex data type is as follows: </li></ul></ul></ul><ul><ul><ul><li><xsd:complexType name=&quot;data type name&quot;> </li></ul></ul></ul><ul><ul><ul><li>Content model declaration </li></ul></ul></ul><ul><ul><ul><li></xsd:complexType> </li></ul></ul></ul>
  9. 9. Elements in XML Schemas (cont.) <ul><li>Defining a New Simple Data Type Based on an Existing Simple Data Type </li></ul><ul><li><xsd:simpleType name=&quot;phoneno&quot;> </li></ul><ul><li><xsd:restriction base=&quot;xsd:string&quot;> </li></ul><ul><li><xsd:length value=&quot;8&quot;/> </li></ul><ul><li><xsd:pattern value=&quot;d{3}-d{4}&quot;/> </li></ul><ul><li></xsd:restriction> </li></ul><ul><li></xsd:simpleType> </li></ul>
  10. 10. Elements in XML Schemas (cont.) <ul><li><xsd:simpleType name=&quot;num&quot;> </li></ul><ul><li><xsd:restriction base=&quot;xsd:positiveInteger&quot;> </li></ul><ul><li><xsd:maxInclusive value=&quot;400&quot;/> </li></ul><ul><li><xsd:minInclusive value=&quot;10&quot;/> </li></ul><ul><li></xsd:restriction> </li></ul><ul><li></xsd:simpleType> </li></ul>
  11. 11. Elements in XML Schemas (cont.) <ul><ul><li>The element to be declared as a complex type must be associated with a complex data type. </li></ul></ul><ul><ul><li>A complex type element can be associated with a complex data type using the syntax: </li></ul></ul><ul><ul><li><xsd:element name=“ElementName&quot; type=“ComplexDataType&quot;/> </li></ul></ul><ul><ul><li>In the preceding syntax, ElementName is the name of the element and ComplexDataType is the complex data type declared earlier. </li></ul></ul>
  12. 12. Elements in XML Schemas (cont.) <ul><li><xsd:complexType name=&quot;prdt&quot;> </li></ul><ul><li><xsd:sequence> </li></ul><ul><li><xsd:element name=&quot;PRODUCTNAME&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li><xsd:element name=&quot;DESCRIPTION&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li><xsd:element name=&quot;PRICE&quot; type=&quot;xsd:positiveInteger&quot;/> </li></ul><ul><li><xsd:element name=&quot;QUANTITY&quot; type=&quot;xsd:nonNegativeInteger&quot;/> </li></ul><ul><li></xsd:sequence> </li></ul><ul><li></xsd:complexType> </li></ul>
  13. 13. Review <ul><li>Simple element declaration </li></ul><ul><li>Complex element declaration </li></ul><ul><li>Simple type declaration </li></ul><ul><li>Complex type declaration </li></ul>
  14. 14. Declaring Attributes in a Schema <ul><li>Attribute declarations can be defined in two ways: </li></ul><ul><ul><li>Simple Type Definitions </li></ul></ul><ul><ul><li>Global Attribute Declarations </li></ul></ul><ul><li>The syntax for declaring an attribute in XSD is as follows: </li></ul><ul><ul><li><attribute name=&quot;attributename&quot; ref=&quot;attributename&quot; type=&quot;datatypename&quot; use=&quot;value&quot; value=&quot;value&quot;> </attribute> </li></ul></ul><ul><ul><li>Consists of various attributes: </li></ul></ul><ul><ul><ul><li>name: Used to specify the name of a user-defined attribute. </li></ul></ul></ul><ul><ul><ul><li>ref: Used to refer to a user-defined attribute declared either in the same or in any other XSD document. </li></ul></ul></ul>
  15. 15. Declaring Attributes in a Schema (cont.) <ul><ul><li>Consists of various attributes (cont.): </li></ul></ul><ul><ul><ul><li>type: Takes a value that specifies the data type of the user-defined attribute. </li></ul></ul></ul><ul><ul><ul><li>use: Specifies the way in which an attribute can be used in an XML document. </li></ul></ul></ul><ul><ul><ul><ul><li>optional </li></ul></ul></ul></ul><ul><ul><ul><ul><li>default </li></ul></ul></ul></ul><ul><ul><ul><ul><li>required </li></ul></ul></ul></ul><ul><ul><ul><ul><li>fixed </li></ul></ul></ul></ul>
  16. 16. Declaring Attributes in a Schema (cont.) <ul><li>Global Attributes </li></ul><ul><ul><li>Are declared outside all element declarations. </li></ul></ul><ul><ul><li>Facilitate attribute reusability. </li></ul></ul><ul><ul><li>For such attributes, the schema element is the parent element. For example </li></ul></ul><ul><ul><li><xsd:schema> </li></ul></ul><ul><ul><ul><li><xsd:attribute name=&quot;NAME&quot; type=&quot;xsd:string&quot;/ </li></ul></ul></ul><ul><ul><ul><li></xsd:schema> </li></ul></ul></ul>
  17. 17. Declaring Attributes in a Schema (cont.) <ul><li>In order to restrict values that can be assigned to an attribute: </li></ul><ul><ul><li>Declare the attribute and associate it with a user-defined simple data type. </li></ul></ul><ul><ul><li>Create a simple data type by using the XSD simpleType element. </li></ul></ul><ul><ul><li>Use the XSD restriction element within the simpleType element to restrict the values that can be assigned to the elements or attributes that use the simple data type. </li></ul></ul>
  18. 18. Exercises <ul><li>Create a Schema for music.xml </li></ul>
  19. 19. Create groups of elements and attributes <ul><li>Feature of creating grouped elements and attributes facilitates the following tasks: </li></ul><ul><ul><li>Creating a reusable group of elements and attributes. </li></ul></ul><ul><ul><li>Selecting a single element from a group. </li></ul></ul><ul><ul><li>Specifying the sequence of elements. </li></ul></ul><ul><li>XSD provides the following elements to group user-defined elements and attributes: </li></ul><ul><ul><li>sequence </li></ul></ul><ul><ul><li>group </li></ul></ul><ul><ul><li>choice </li></ul></ul><ul><ul><li>all </li></ul></ul><ul><ul><li>attributeGroup </li></ul></ul>
  20. 20. Create groups of elements and attributes (cont.) <ul><li>Sequence </li></ul><ul><ul><li>Ensures that the elements declared within the opening and closing tags appear in a specific order. </li></ul></ul><ul><li><xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> </li></ul><ul><li><xsd:element name=&quot;EMPLOYEE&quot; type=&quot;emptype&quot;/> </li></ul><ul><li><xsd:complexType name=&quot;emptype&quot;> </li></ul><ul><li><xsd:sequence> </li></ul><ul><li><xsd:element name=&quot;FIRSTNAME&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li><xsd:element name=&quot;LASTNAME&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li><xsd:element name=&quot;DESIG&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li><xsd:element name=&quot;DEPARTMENT&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li></xsd:sequence> </li></ul><ul><li></xsd:complexType> </li></ul><ul><li></xsd:schema> </li></ul>
  21. 21. Create groups of elements and attributes (cont.) <ul><li>The group Element </li></ul><ul><ul><li>Grouping of elements is beneficial when you want a set of related elements to be referred using a common name. </li></ul></ul><ul><ul><li>The syntax for declaring a group element is as follows: </li></ul></ul><ul><ul><ul><li><group maxOccurs=&quot;nonNegetiveInteger | unbounded“ minOccurs=&quot;nonNegetiveInteger&quot; name=&quot;NCName&quot; ref=&quot;QName&quot;> </group> </li></ul></ul></ul>
  22. 22. Create groups of elements and attributes (cont.) <ul><li>Example </li></ul><ul><li><xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> </li></ul><ul><li><xsd:group name=&quot;empname&quot;> </li></ul><ul><li><xsd:sequence> </li></ul><ul><li><xsd:element name=&quot;FIRSTNAME&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li><xsd:element name=&quot;LASTNAME&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li></xsd:sequence> </li></ul><ul><li></xsd:group> </li></ul><ul><li><xsd:element name=&quot;EMPLOYEE&quot; type=&quot;emptype&quot;/> </li></ul><ul><li><xsd:complexType name=&quot;emptype&quot;> </li></ul><ul><li><xsd:sequence> </li></ul><ul><li><xsd:group ref=&quot;empname&quot;/> </li></ul><ul><li><xsd:element name=&quot;ADDRESS&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li></xsd:sequence> </li></ul><ul><li></xsd:complexType> </li></ul><ul><li></xsd:schema> </li></ul>
  23. 23. Create groups of elements and attributes (cont.) <ul><li>The choice Element </li></ul><ul><ul><li>Enables the selection of a single option from multiple options. </li></ul></ul><ul><ul><li>Allows only one of the elements contained in the group to be present within the parent element. </li></ul></ul><ul><ul><li>The syntax for declaring a choice element is as follows: </li></ul></ul><ul><ul><ul><li><choice id=&quot;ID&quot; maxOccurs=&quot;nonNegativeInteger | unbounded&quot; minOccurs=&quot;nonNegativeInteger&quot;> </choice> </li></ul></ul></ul>
  24. 24. Create groups of elements and attributes (cont.) <ul><li><xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> </li></ul><ul><li><xsd:element name=&quot;CUSTOMER&quot; type=&quot;custtype&quot;/> </li></ul><ul><li><xsd:complexType name=&quot;custtype&quot;> </li></ul><ul><li><xsd:sequence> </li></ul><ul><li><xsd:group ref=&quot;custname&quot;/> </li></ul><ul><li><xsd:element name=&quot;ADDRESS&quot; type=&quot;addtype&quot;/> </li></ul><ul><li></xsd:sequence> </li></ul><ul><li></xsd:complexType> </li></ul><ul><li><xsd:complexType name=&quot;addtype&quot;> </li></ul><ul><li><xsd:choice> </li></ul><ul><li><xsd:element name=&quot;RESIDENCE&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li><xsd:element name=&quot;OFFICE&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li></xsd:choice> </li></ul><ul><li></xsd:complexType> </li></ul><ul><li><xsd:group name=&quot;custname&quot;> </li></ul><ul><li><xsd:sequence> </li></ul><ul><li><xsd:element name=&quot;FIRSTNAME&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li><xsd:element name=&quot;LASTNAME&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li></xsd:sequence> </li></ul><ul><li></xsd:group> </li></ul><ul><li></xsd:schema> </li></ul>
  25. 25. Create groups of elements and attributes (cont.) <ul><li>The all Element </li></ul><ul><ul><li>Enables to use the child elements in any order. </li></ul></ul><ul><ul><li>The syntax for using the all element: </li></ul></ul><ul><ul><ul><li><all maxOccurs=&quot;positiveInteger&quot; minOccurs=&quot; 0 | 1 &quot;> </all> </li></ul></ul></ul>
  26. 26. Create groups of elements and attributes (cont.) <ul><li><xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> </li></ul><ul><li><xsd:element name=&quot;EMPLOYEE&quot; type=&quot;emptype&quot; /> </li></ul><ul><li><xsd:complexType name=&quot;emptype&quot;> </li></ul><ul><li><xsd:all> </li></ul><ul><li><xsd:element name=&quot;FIRSTNAME&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li><xsd:element name=&quot;LASTNAME&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li><xsd:element name=&quot;DESIG&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li><xsd:element name=&quot;DEPARTMENT&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li></xsd:all> </li></ul><ul><li></xsd:complexType> </li></ul><ul><li></xsd:schema> </li></ul>
  27. 27. Create groups of elements and attributes (cont.) <ul><li>The attributeGroup Element </li></ul><ul><ul><li>Enables to group attributes that can be reused with different elements. </li></ul></ul><ul><ul><li>The syntax of the attributeGroup element is as follows: </li></ul></ul><ul><ul><ul><li><attributeGroup> </li></ul></ul></ul><ul><ul><ul><li>attribute1 </li></ul></ul></ul><ul><ul><ul><li>attribute2 </li></ul></ul></ul><ul><ul><ul><li> : </li></ul></ul></ul><ul><ul><ul><li></attributeGroup> </li></ul></ul></ul>
  28. 28. Create groups of elements and attributes (cont.) <ul><li><xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> </li></ul><ul><li><xsd:element name=&quot;EMPLOYEE&quot; type=&quot;emptype&quot;/> </li></ul><ul><li><xsd:complexType name=&quot;emptype&quot;> </li></ul><ul><li><xsd:group ref=&quot;empname&quot;/> </li></ul><ul><li><xsd:attributeGroup ref=&quot;depdesig&quot;/> </li></ul><ul><li></xsd:complexType> </li></ul><ul><li><xsd:group name=&quot;empname&quot;> </li></ul><ul><li><xsd:sequence> </li></ul><ul><li><xsd:element name=&quot;FIRSTNAME&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li><xsd:element name=&quot;LASTNAME&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li></xsd:sequence> </li></ul><ul><li></xsd:group> </li></ul><ul><li><xsd:attributeGroup name=&quot;depdesig&quot;> </li></ul><ul><li><xsd:attribute name=&quot;DEPARTMENT&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li><xsd:attribute name=&quot;DESIGNATION&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li></xsd:attributeGroup> </li></ul><ul><li></xsd:schema> </li></ul>
  29. 29. <ul><li>To be continued… </li></ul>
  30. 30. Reference <ul><li>XML How to program </li></ul><ul><li>http://www.w3.org </li></ul><ul><li>Teach Yourself XML in 21 Days, 3 rd Edition </li></ul><ul><li>Learning XML, 2 nd Edition </li></ul><ul><li>XML tutorial http://www.w3schools.com/w3c/ </li></ul>
  31. 31. Q&A <ul><li>Feel free to post questions at http://yht4ever.blogspot.com . </li></ul><ul><li>or email to: [email_address] or [email_address] </li></ul>
  32. 32. http://yht4ever.blogspot.com Thank You !

×