Session 2


Published on

XML by Example Session 2

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Session 2

  1. 1. Session 2 DTDs and XML Schema Create by ChungLD faculty XML by Example / Bachkhoa – Aptech Computer Education /41
  2. 2. Objectives <ul><li>Document Type Definition </li></ul><ul><li>Working with DTDs </li></ul><ul><li>Valid XML documents </li></ul><ul><li>Declarations </li></ul><ul><li>XML Schema </li></ul><ul><li>Exploring XML Schemas </li></ul><ul><li>Working with complex types </li></ul><ul><li>Working with simple types </li></ul>Create by Chungld faculty /41
  3. 3. Definition of a DTD <ul><li>A Document Type Definition(DTD) is a non XML Document made up of elements, attributes and entities declarations. </li></ul><ul><li>The DTD helps XML parsers to validate the XML document. It also helps authors to set default values for element attributes. The document’s tree structure can be determined based on the DTD. </li></ul>Create by Chungld faculty /41
  4. 4. Structure of DTD Create by Chungld faculty /41 Element Declarations Attribute Declarations Entity Declarations
  5. 5. Internal DTD Declaration <ul><li><!DOCTYPE root-element [element-declarations]> </li></ul>Create by Chungld faculty /41 Internal DTDs
  6. 6. External DTD Declaration <ul><li><!DOCTYPE root-element SYSTEM &quot;filename&quot;> </li></ul>Create by Chungld faculty /41 External DTDs
  7. 7. Declaring Element <ul><li>In a DTD, XML elements are declared with an element declaration with the following syntax: </li></ul><ul><li><!ELEMENT element-name category> or <!ELEMENT element-name (element-content)> </li></ul>Create by Chungld faculty /41
  8. 8. Example <ul><ul><li><!ELEMENT Mobile (Company, Model, Price,Accessories)> </li></ul></ul><ul><ul><li><!ELEMENT Company (#PCDATA)> </li></ul></ul><ul><ul><li><!ELEMENT Model (#PCDATA)> </li></ul></ul><ul><ul><li><!ELEMENT Price (#PCDATA)> </li></ul></ul><ul><ul><li><!ELEMENT Accessories (#PCDATA)> </li></ul></ul>Create by Chungld faculty /41
  9. 9. Declaring Elements Create by Chungld faculty /41
  10. 10. More on declaring elements Create by Chungld faculty /41
  11. 11. Declaring attributes <ul><li><!ATTLIST element-name attribute-name attribute – type default – value> </li></ul><ul><li>Attribute –type </li></ul><ul><ul><li>CDATA </li></ul></ul><ul><ul><li>ID </li></ul></ul><ul><ul><li>IDREF </li></ul></ul><ul><ul><li>ENTITY </li></ul></ul>Create by Chungld faculty /41 The attribute-value in a DTD declaration can have values as show in the table:
  12. 12. Default Example <ul><li>DTD: <!ELEMENT square EMPTY> <!ATTLIST square width CDATA &quot;0&quot;> </li></ul><ul><li>Valid XML: <square width=&quot;100&quot; /> </li></ul>Create by Chungld faculty /41
  13. 13. #REQUIRED Example <ul><li>Syntax </li></ul><ul><li><!ATTLIST element-name attribute-name attribute-type #REQUIRED> </li></ul><ul><li>DTD: <!ATTLIST person number CDATA #REQUIRED> </li></ul><ul><li>Valid XML: <person number=&quot;5677&quot; /> </li></ul><ul><li>Invalid XML: <person /> </li></ul>Create by Chungld faculty /41
  14. 14. #IMPLIED Example <ul><li>Syntax </li></ul><ul><li><!ATTLIST element-name attribute-name attribute-type #FIXED &quot;value&quot;> </li></ul><ul><li>#IMPLIED </li></ul><ul><li>DTD: <!ATTLIST contact fax CDATA #IMPLIED> Valid XML: <contact fax=&quot;555-667788&quot; /> Valid XML: <contact /> </li></ul>Create by Chungld faculty /41
  15. 15. #FIXED example <ul><li>Syntax </li></ul><ul><li><!ATTLIST element-name attribute-name (en1|en2|..) default-value> </li></ul><ul><li>DTD: <!ATTLIST sender company CDATA #FIXED &quot;Microsoft&quot;> Valid XML: <sender company=&quot;Microsoft&quot; /> Invalid XML: <sender company=&quot;W3Schools&quot; /> </li></ul>Create by Chungld faculty /41
  16. 16. Enumerated Attribute Values <ul><li>Syntax </li></ul><ul><li><!ATTLIST element-name attribute-name (en1|en2|..) default-value> </li></ul><ul><li>DTD: <!ATTLIST payment type (check|cash) &quot;cash&quot;> XML example: <payment type=&quot;check&quot; /> or <payment type=&quot;cash&quot; /> </li></ul>Create by Chungld faculty /41
  17. 17. Avoid using attributes? <ul><li>Some of the problems with attributes are: </li></ul><ul><ul><li>attributes cannot contain multiple values (child elements can) </li></ul></ul><ul><ul><li>attributes are not easily expandable (for future changes) </li></ul></ul><ul><ul><li>attributes cannot describe structures (child elements can) </li></ul></ul><ul><ul><li>attributes are more difficult to manipulate by program code </li></ul></ul><ul><ul><li>attribute values are not easy to test against a DTD </li></ul></ul>Create by Chungld faculty /41
  18. 18. DTD - Entities <ul><li>An entity, in XML, is a placeholder that consists of a name and a value. It is declared once and then repeatedly used through out the document in place of its value. Entities are variables used to define shortcuts to standard text or special characters. </li></ul><ul><li>Entites c assified of: </li></ul><ul><ul><li>Entity references are references to entities </li></ul></ul><ul><ul><li>Entities can be declared internal or external </li></ul></ul>Create by Chungld faculty /41
  19. 19. Internal Entity Declaration <ul><li>Syntax </li></ul><ul><ul><li><!ENTITY entity-name &quot;entity-value&quot;> </li></ul></ul><ul><li>DTD Example: <!ENTITY writer &quot;Donald Duck.&quot;> <!ENTITY copyright &quot;Copyright W3Schools.&quot;> XML example: <author>&writer;&copyright;</author> </li></ul>Create by Chungld faculty /41
  20. 20. External Entity Declaration <ul><li>DTD Example: <!ENTITY writer SYSTEM “C:/entities.dtd&quot;> <!ENTITY copyright SYSTEM “C:/entities.dtd&quot;> XML example: <author>&writer;&copyright;</author> </li></ul>Create by Chungld faculty /34
  21. 21. DTD Example Create by Chungld faculty /41 Click and see
  22. 22. XML Schema Create by Chungld faculty /41
  23. 23. What is an XML Schema? <ul><li>The objective of an XML Schema is to define the valid structure of an XML document, similar to a DTD. </li></ul><ul><li>An XML Schema defines: </li></ul><ul><ul><li>defines elements that can appear in a document </li></ul></ul><ul><ul><li>defines attributes that can appear in a document </li></ul></ul><ul><ul><li>defines which elements are child elements </li></ul></ul><ul><ul><li>defines the order of child elements </li></ul></ul><ul><ul><li>defines the number of child elements </li></ul></ul><ul><ul><li>defines whether an element is empty or can include text </li></ul></ul><ul><ul><li>defines data types for elements and attributes </li></ul></ul><ul><ul><li>defines default and fixed values for elements and attributes </li></ul></ul>Create by Chungld faculty /41
  24. 24. Need for Schema <ul><li>XML schema allow web applications to exchange XML data more robustly using a range of new features: </li></ul><ul><ul><li>Schemas support data types </li></ul></ul><ul><ul><li>Schemas are portable and efficient </li></ul></ul><ul><ul><li>Schemas secure data communication </li></ul></ul><ul><ul><li>Schemas are extensible </li></ul></ul><ul><ul><li>Schemas catch higher-level mistakes </li></ul></ul><ul><ul><li>Schemas support Namespace </li></ul></ul>Create by Chungld faculty /41
  25. 25. Comparing DTDs with Schemas <ul><li>DTDs are used to define content models, nesting of elements in a valid order, and provide limited support to data types and attributes. The drawbacks of DTDs are: </li></ul><ul><ul><li>DTDs are written in a non-XML syntax </li></ul></ul><ul><ul><li>DTDs are not extensible </li></ul></ul><ul><ul><li>DTDs do not support namespace </li></ul></ul><ul><ul><li>DTDs offer limited data typing </li></ul></ul>Create by Chungld faculty /41 DTD Schema <!ELEMENT> declaration xsd:element element <!ATTLIST> declaration xsd:attribute element <!ENTITY> declaration n/a #PCDATA content xsd:string type n/a other data types
  26. 26. Advantages of XML Schemas over DTD <ul><li>The XML schema offers a range of new features: </li></ul><ul><ul><li>Richer data types </li></ul></ul><ul><ul><li>Archetypes </li></ul></ul><ul><ul><li>Attribute grouping </li></ul></ul><ul><ul><li>Definable archetypes </li></ul></ul>Create by Chungld faculty /41 Click and see
  27. 27. How to write an XML Schema <ul><li>Step 1: create XML file </li></ul><ul><li>Step 2: create XSD file for XML file </li></ul>Create by Chungld faculty /41 Click and see
  28. 28. XML Schemas Support Data Types <ul><li>XML Schema describes a number of built-in data types, which can be used to specify and validate the intended data type of the content. </li></ul><ul><ul><li>Built-in data types are available to all XML Schema authors, and should be implemented by a conforming processor. </li></ul></ul><ul><ul><li>User-derived datatypes are defined in individual schema instances, and are particular to that schema(although it is possible to import these definitions into other definitions). </li></ul></ul>Create by Chungld faculty /41
  29. 29. XML Schemas Support Data Types… <ul><li>The built – in datatypes specified by XML: </li></ul><ul><ul><li>string (xs:string) </li></ul></ul><ul><ul><li>boolean (xs:boolean) </li></ul></ul><ul><ul><li>Numeric (xs:integer | xs:float | xs:double) </li></ul></ul><ul><ul><li>dataTime (xs:date) </li></ul></ul><ul><ul><li>binary (xs:hexBinary) </li></ul></ul><ul><ul><li>anyURI (xs:anyURI) </li></ul></ul>Create by Chungld faculty /41
  30. 30. Schema Vocabulary <ul><li>Schema delcaration: Every XML Schema starts with the root element <schema>. The code indicates that the elements and data types used in schema are derived from the namespace and prefixed with xsd </li></ul>Create by Chungld faculty /41 <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?> <xsd:schema xmlns:xsd=&quot;; targetNamespace=&quot;http:// aptech .com&quot; xmlns=&quot;http:// aptech .com&quot; elementFormDefault=&quot;qualified&quot;>
  31. 31. Schema Vocabulary… <ul><li>Using Schema declaration : It indicates the default namespace declaration. This declaration informs the schema-validator that all the elements used in this XML document are declared in the default( ) namespace </li></ul>Create by Chungld faculty /41 <mail xmlns=&quot;; xmlns:xsi=&quot;; xsi:schemaLocation=&quot; mail.xsd&quot;>
  32. 32. XSD Simple Elements <ul><li>A simple element is an XML element that can contain only text. It cannot contain any other elements or attributes. </li></ul><ul><li>Syntax: </li></ul><ul><li><xs:element name=&quot;xxx&quot; type=&quot;yyy&quot;/> </li></ul><ul><ul><li>XML: </li></ul></ul><ul><li><lastname>Refsnes</lastname> <age>36</age> <dateborn>1970-03-27</dateborn> </li></ul><ul><li> XSD: </li></ul><ul><li><xs:element name=&quot;lastname&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;age&quot; type=&quot;xs:integer&quot;/> <xs:element name=&quot;dateborn&quot; type=&quot;xs:date&quot;/> </li></ul>Create by Chungld faculty /41
  33. 33. Default and Fixed Values for Simple Elements <ul><li>Syntax: </li></ul><ul><li><xs:element name=&quot;color&quot; type=&quot;xs:string&quot; default=&quot;red&quot;/> </li></ul><ul><li>Example: </li></ul><ul><li><xs:element name=&quot;color&quot; type=&quot;xs:string&quot; fixed=&quot;red&quot;/> </li></ul>Create by Chungld faculty /41
  34. 34. XSD Attributes <ul><li>All attributes are declared as simple types. </li></ul><ul><li>Simple elements cannot have attributes. If an element has attributes, it is considered to be of a complex type. But the attribute itself is always declared as a simple type. </li></ul><ul><li>The syntax for defining an attribute is: </li></ul><ul><li><xs:attribute name=&quot;xxx&quot; type=&quot;yyy&quot;/> </li></ul><ul><li> XML: </li></ul><ul><ul><li><lastname lang=&quot;EN&quot;>Smith</lastname> </li></ul></ul><ul><ul><li>XSD: </li></ul></ul><ul><ul><li><xs:attribute name=&quot;lang&quot; type=&quot;xs:string&quot;/> </li></ul></ul>Create by Chungld faculty /41
  35. 35. Default and Fixed Values for Attributes <ul><li>Syntax: </li></ul><ul><li><xs:attribute name=&quot;lang&quot; type=&quot;xs:string&quot; default=&quot;EN&quot;/> </li></ul><ul><li>XSD: </li></ul><ul><li><xs:attribute name=&quot;lang&quot; type=&quot;xs:string&quot; fixed=&quot;EN&quot;/> </li></ul>Create by Chungld faculty /41
  36. 36. Optional and Required Attributes <ul><li><xs:attribute name=&quot;lang&quot; type=&quot;xs:string&quot; use=&quot;required&quot;/> </li></ul>Create by Chungld faculty /41
  37. 37. User-defined simple types <ul><li>XML-schema supports a library of built – in datatypes. However, during the course of writing complex schema documents, a developer may need a type of data that is not defined in the schema recommendation. Thus, the Restrictions must defines </li></ul>Create by Chungld faculty /41
  38. 38. Restrictions on Values <ul><li>The following example defines an element called &quot;age&quot; with a restriction. The value of age cannot be lower than 0 or greater than 120: </li></ul><ul><ul><li><xs:element name=&quot;age&quot;>   <xs:simpleType>     <xs:restriction base=&quot;xs:integer&quot;>       <xs:minInclusive value=&quot;0&quot;/>       <xs:maxInclusive value=&quot;120&quot;/>     </xs:restriction>   </xs:simpleType> </xs:element> </li></ul></ul>Create by Chungld faculty /41
  39. 39. Restrictions on a Set of Values <ul><li>To limit the content of an XML element to a set of acceptable values, we would use the enumeration constraint. </li></ul><ul><ul><li><xs:element name=“Province&quot;>   <xs:simpleType>     <xs:restriction base=&quot;xs:string&quot;>       <xs:enumeration value=“Ha Nam&quot;/>       <xs:enumeration value=“Ha Noi&quot;/>       <xs:enumeration value=“TPHCM&quot;/>     </xs:restriction>   </xs:simpleType> </xs:element> </li></ul></ul>Create by Chungld faculty /41
  40. 40. Restriction by pattern <ul><li><xsd:element name=&quot;zipcode&quot;> </li></ul><ul><li><xsd:simpleType> </li></ul><ul><li><xsd:restriction base=&quot;xsd:string&quot;> <xsd:pattern value=&quot;d{5}(-d{4})?&quot;/> </xsd:restriction> </li></ul><ul><li></xsd:simpleType> </li></ul><ul><li></xsd:element> </li></ul><ul><ul><li>value attribute contains a regular expression </li></ul></ul><ul><ul><li>d means any digit </li></ul></ul><ul><ul><li>() used for grouping </li></ul></ul><ul><ul><li>x{5} means exactly 5 x's (in a row) </li></ul></ul><ul><ul><li>x? indicates zero or one x </li></ul></ul><ul><ul><li>zipcode examples: 90720-1314 and 22043 </li></ul></ul>Create by Chungld faculty /41
  41. 41. Restrictions on Length <ul><li>To limit the length of a value in an element, we would use the length, maxLength, and minLength constraints. </li></ul><ul><ul><li><xs:element name=&quot;password&quot;>   <xs:simpleType>     <xs:restriction base=&quot;xs:string&quot;>       <xs:length value=&quot;8&quot;/>     </xs:restriction>   </xs:simpleType> </xs:element> </li></ul></ul>Create by Chungld faculty /41
  42. 42. Restrictions on Whitespace Characters <ul><li>To specify how whitespace characters should be handled, we would use the whiteSpace constraint. </li></ul><ul><li>Example following means that the XML processor WILL NOT remove any white space characters: </li></ul><ul><ul><li><xs:element name=&quot;address&quot;>   <xs:simpleType>     <xs:restriction base=&quot;xs:string&quot;>       <xs:whiteSpace value=&quot;preserve&quot;/>     </xs:restriction>   </xs:simpleType> </xs:element> </li></ul></ul>Create by Chungld faculty /41
  43. 43. Restrictions on Whitespace Characters… <ul><li>Example following means that the XML processor WILL REPLACE all white space characters (line feeds, tabs, spaces, and carriage returns) with spaces: </li></ul><ul><ul><li><xs:element name=&quot;address&quot;>   <xs:simpleType>     <xs:restriction base=&quot;xs:string&quot;>       <xs:whiteSpace value=&quot;replace&quot;/>     </xs:restriction>   </xs:simpleType> </xs:element> </li></ul></ul>Create by Chungld faculty /41
  44. 44. Restrictions on Whitespace Characters… <ul><li>Example following means that the XML processor WILL REMOVE all white space characters (line feeds, tabs, spaces, carriage returns are replaced with spaces, leading and trailing spaces are removed, and multiple spaces are reduced to a single space): </li></ul><ul><ul><li><xs:element name=&quot;address&quot;>   <xs:simpleType>     <xs:restriction base=&quot;xs:string&quot;>       <xs:whiteSpace value=&quot;collapse&quot;/>     </xs:restriction>   </xs:simpleType> </xs:element> </li></ul></ul>Create by Chungld faculty /41
  45. 45. Working with complex types <ul><li>A complex element is an XML element that contains other elements and/or attributes. </li></ul><ul><li>There are four kinds of complex elements: </li></ul><ul><ul><li>empty elements </li></ul></ul><ul><ul><li>elements that contain only other elements </li></ul></ul><ul><ul><li>elements that contain only text </li></ul></ul><ul><ul><li>elements that contain both other elements and text </li></ul></ul>Create by Chungld faculty /41
  46. 46. How to Define a Complex Element <ul><li>XML: </li></ul><ul><li><employee>   <firstname>John</firstname>   <lastname>Smith</lastname> </employee> </li></ul><ul><li>XSD: </li></ul><ul><li><xs:element name=&quot;employee&quot;>   <xs:complexType>     <xs:sequence>       <xs:element name=&quot;firstname&quot; type=&quot;xs:string&quot;/>       <xs:element name=&quot;lastname&quot; type=&quot;xs:string&quot;/>     </xs:sequence>   </xs:complexType> </xs:element> </li></ul>Create by Chungld faculty /41
  47. 47. Complex Empty Elements <ul><li>XML: </li></ul><ul><li><product prodid=&quot;1345&quot; /> </li></ul><ul><li>XSD: </li></ul><ul><li><xs:element name=&quot;product&quot;>   <xs:complexType> </li></ul><ul><li>      <xs:attribute name=&quot;prodid&quot; type=&quot;xs:positiveInteger&quot;/>   </xs:complexType> </xs:element> </li></ul>Create by Chungld faculty /41
  48. 48. Complex Types Containing Elements Only <ul><li>XML: </li></ul><ul><li><employee>   <firstname>John</firstname>   <lastname>Smith</lastname> </employee> </li></ul><ul><li>XSD: </li></ul><ul><li><xs:element name=&quot;employee&quot;>   <xs:complexType>     <xs:sequence>       <xs:element name=&quot;firstname&quot; type=&quot;xs:string&quot;/>       <xs:element name=&quot;lastname&quot; type=&quot;xs:string&quot;/>     </xs:sequence>   </xs:complexType> </xs:element> </li></ul>Create by Chungld faculty /41
  49. 49. Complex Text-Only Elements <ul><li>XML: </li></ul><ul><li><shoesize country=&quot;france&quot;>35</shoesize> </li></ul><ul><li>XSD: </li></ul><ul><li><xs:element name=&quot;shoesize&quot;>   <xs:complexType>     <xs:simpleContent>       <xs:extension base=&quot;xs:integer&quot;>         <xs:attribute name=&quot;country&quot; type=&quot;xs:string&quot; />       </xs:extension>     </xs:simpleContent>   </xs:complexType> </xs:element> </li></ul>Create by Chungld faculty /41
  50. 50. Complex Types with Mixed Content <ul><li>An XML element, &quot;letter&quot;, that contains both text and other elements: </li></ul><ul><ul><li>XML: </li></ul></ul><ul><ul><li><letter>   Dear Mr.<name>John Smith</name>.   Your order <orderid>1032</orderid>   will be shipped on <shipdate>2001-07-13</shipdate>. </letter> </li></ul></ul><ul><ul><li>XSD: </li></ul></ul><ul><ul><li><xs:element name=&quot;letter&quot;>   <xs:complexType mixed=&quot;true&quot;>     <xs:sequence>       <xs:element name=&quot;name&quot; type=&quot;xs:string&quot;/>       <xs:element name=&quot;orderid&quot; type=&quot;xs:positiveInteger&quot;/>       <xs:element name=&quot;shipdate&quot; type=&quot;xs:date&quot;/>     </xs:sequence>   </xs:complexType> </xs:element> </li></ul></ul>Create by Chungld faculty /41
  51. 51. Defining number of element occurrences <ul><li>minOccurs and maxOccurs attributes control number of occurrences of an element, sequence or choice </li></ul><ul><ul><li>minOccurs must be a non-negative integer </li></ul></ul><ul><ul><li>maxOccurs must be a non-negative integer or unbounded </li></ul></ul><ul><ul><li>default value for each of minOccurs and maxOccurs is 1 </li></ul></ul>Create by Chungld faculty /41
  52. 52. minOccurs and maxOccurs Create by Chungld faculty /41 Click and see
  53. 53. Grouping Constructs… <ul><li>The XML Schema Language provides three grouping constructs that specify whether and how ordering of individual elements is important: </li></ul><ul><ul><li>xsd:sequence </li></ul></ul><ul><ul><ul><li>requires elements to occur in order given </li></ul></ul></ul><ul><ul><ul><li>analogous to , in DTDs </li></ul></ul></ul><ul><ul><li>xsd:choice </li></ul></ul><ul><ul><ul><li>allows one of the given elements to occur </li></ul></ul></ul><ul><ul><ul><li>analogous to | in DTDs </li></ul></ul></ul><ul><ul><li>xsd:all </li></ul></ul><ul><ul><ul><li>allows elements to occur in any order </li></ul></ul></ul><ul><ul><ul><li>analogous to & in SGML DTDs </li></ul></ul></ul>Create by Chungld faculty /41
  54. 54. Summary and workshop Create by Chungld faculty /41