1 / 48

CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad

CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad Virtual Campus, CIIT COMSATS Institute of Information Technology T2-Lecture-5. eXtensable Markup Language (XML) Part - III. For Lecture Material/Slides Thanks to: www.w3schools.com. Objectives.

kieran-witt
Download Presentation

CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad

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. CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad Virtual Campus, CIIT COMSATS Institute of Information Technology T2-Lecture-5

  2. eXtensable Markup Language (XML)Part - III For Lecture Material/Slides Thanks to: www.w3schools.com

  3. Objectives • Part 1: Review The basics of creating an XML document • Part 2: Imposing Structure on XML Documents using Document Type Definition DTD • Part 3: Strengthening the data-modeling capabilities of XML Using XML Schemas T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  4. Part 1: Review The basics of creating an XML document T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  5. Part 1: A review of XML • An Extensible Markup Language (XML) document describes the structure of data. • XML and HTML have a similar syntax … both derived from SGML • XML has no mechanism to specify the format for presenting data to the user • An XML document resides in its own file with an ‘.xml’ extension T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  6. Main Components of an XML Document • Elements: <hello> • Attributes: <item id=“33905”> • Entities: &lt; (<) T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  7. The Basic Rules • XML is case sensitive • All start tags must have end tags • Elements must be properly nested • XML declaration is the first statement • Every document must contain a root element • Attribute values must have quotation marks • Certain characters are reserved for parsing T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  8. XML is different from HTML • HTML is a Hyper Text Markup language • Designed for a specific application, namely, displaying, viewing, presenting and linking hypertext documents • XML describes structure (organization of data) andcontent(“semantics” the interpretation of data) • XML is a subset of SGML (Standard Generalized Markup Language) T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  9. An Address Book as an XML document <addresses> <person> <name> Ahmed Mumtaz</name> <tel> 9251-2233-44 </tel> <email> mumtaz@yahoo.com </email> </person> <person> <name> Malik Riaz khan</name> <tel> 9251-123-4450 </tel> <email>mriaz@yahoo.com</email> </person> </addresses> T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  10. Important Features of XML No fixed set of tags • User is allowed to introduce New tags Already defined set of tags can also be used Namespacesfacilitate uniform and coherent descriptions of data • For example, a namespace for address books determines to use of : <tel> or <phone> or <mobile> T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  11. Features of XML (cont’d) • XML supports internationalization through Unicode • Web services (e.g., e-commerce) require exchanging data between various applications that run on different platforms. • XML (with the support of namespaces) is the best option for data exchange on the Web. • XML is a data model • Similar to the semi-structured-data-model • XML has follow the concept of DTD and the more impressive XML Schema T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  12. XML family • Limited styling of XML can be done with CSS • Document Type Definitions (DTDs) impose structure on XML documents • XML Schemasstrengthen the data-modeling capabilities of XML • XPathis a language for accessing XML documents • XLink andXPointer support cross-references • XSLTis a language for transforming XML documents into other XML documents such as XHTML, for viewing XML files • XQueryis a language for querying XML documents T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  13. DTD : (Document Type Definition) Imposing Structure on XML Documents using

  14. XML defines structure of the document • Some XML files only contain text documents with tags that contain metadata and describe the structure Example: <book year= “2011"> <title> e-Commerce Business, Technology and Society </title> <author> <last>Laudon</last> <first>Kenneth</first> </author> <publisher>PEARSON</publisher> <price>78.99</price> </book> T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  15. Document Type Definitions • Document Type Definitions (DTDs) impose structure on XML documents • There is some relationship between a DTD and a schema, but it is not close hence the need for additional “typing” systems exists. • The DTD is a syntactic specification T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  16. Document Type Definitions • A description of legal, valid data further contributes to the interoperability and efficiency of using XML • A single DTD ensures a common format for each XML document that references it. • An application can use a standard DTD to verify that data that it receives from the outside world is valid. • An XML document that conforms to the rules within a DTD is said to be valid document. • If the XML document does not follow the rules contained within the DTD, a parser generates an error. T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  17. Motivation • A DTD adds syntactical requirements in addition to the well-formed requirement. • It helps in eliminating errors when creating or editing XML documents. • It clarifies the intended semantics. • It simplifies the processing of XML documents T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  18. An Example • In an address book, where can a phone number appear? Under <person>, under <name> or under both? • If we have to check for all possibilities, processing takes longer and it may not be clear to whom a phone belongs to? T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  19. Exactlyonename At most one greeting As many address lines as needed (in order) Mixed telephones and faxes As many as needed Example: An Address Book <person> <name>HomerSimpson</name> <greet>Dr. H. Simpson</greet> <addr>1234 Springwater Road</addr> <addr>Springfield USA, 98765</addr> <tel>(321) 786 2543</tel> <fax>(051) 786 2544</fax> <tel>(051) 786 2544</tel> <email>homer@math.springfield.edu</email> </person> T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  20. Specifying the Structure • name to specify a name element • greet? to specify an optional (0 or 1) greet elements • name, greet? to specify a name followed by an optional greet • addr* to specify 0 or more address lines • tel | fax a telor a fax element • (tel | fax)* 0 or more repeats of tel or fax • email* 0 or more email elements T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  21. Specifying the Structure (cont’d) • So the whole structure of a person entry is specified by name, greet?, addr*, (tel | fax)*, email* This is known as a regular expression T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  22. Element Type Definition • for each element type E, a declaration of the form: • <!ELEMENT E P> • where P is a regular expression, i.e., • P ::= EMPTY | ANY | #PCDATA | E’ | • P1, P2 | P1 | P2 | P? | P+ | P* • E’: element type • P1 , P2: concatenation • P1 | P2: disjunction • P?: optional • P+: one or more occurrences • P*: 0 or more occurrences T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  23. Summary of Regular Expressions A The tag (i.e., element) A occurs e1,e2 The expression e1 followed by e2 e* 0 or more occurrences of e e? Optional: 0 or 1 occurrences e+ 1 or more occurrences e1 | e2 either e1 or e2 (e) grouping T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  24. The Definition of an Element Consists of Exactly One of the Following • A regular expression (as defined earlier) • EMPTY means that the element has no content • ANY means that content can be any mixture of PCDATA and elements defined in the DTD • Mixed content which is defined as described on the next slide : (#PCDATA) T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  25. The Definition of Mixed Content • Mixed content is described by a repeatable OR group (#PCDATA | element-name | …)* Inside the group, no regular expressions ; just ement names #PCDATA must be first followed by 0 or more element names, separated by | * The group can be repeated 0 or more times T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  26. Some Example DTD Declarations Example 1: Elements with Data <!ELEMENT Month (#PCDATA)><!--DTD declaration of an element-> <!—Valid usage within XML file--> <Month>April</Month> <Month>This is a month</Month> <!—Invalid usage within XML file, Month can’t have children!--> <Month> <January>Jan</January> <March>March</March> </Month> T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  27. Some Example DTD Declarations Example 3: Elements with Children ;To specify that an element must have a single child element, include the element name within the parenthesis. <!ELEMENT House (Address)> <!—A house element has a single child address--> <House> <!—Valid usage within XML file--> <Address> 5 Park Road ChakShahzad, Islamabad </Address> </House> T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  28. Some Example DTD Declarations Example-3: An element can have multiple children. A DTD describes multiple children using a sequence, or a list of elements separated by commas. The XML file must contain one of each element in the specified order. <!--DTD declaration of an element--> <!ELEMENT address (person,street,city, zip)> <!ELEMENT person (#PCDATA)> <!ELEMENT street (#PCDATA)> <!ELEMENT city (#PCDATA)> <!ELEMENT zip (#PCDATA)> <!—Valid usage within XML file--> <address> <person> Tariq Rasheed </person> <street> 9thEveneue.</street> <city> Federal Area, Islamabad </city> <zip> 44000 </zip> </address> T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  29. Cautions concerning DTDs • All element declarations begin with <! and end with > <!ELEMENT Address > • The ELEMENT declaration is case sensitive • The programmer must declare all elements within an XML file • Elements declared with the #PCDATA content model can not have children • When describing sequences, (e1,e2) the XML document must contain exactly those elements in exactly that order. T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  30. The name of the DTD is addressbook “Internal” means that the DTD and the XML Document are in the same file An Address-Book XML Document with an Internal DTD <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE addressbook [ <!ELEMENT addressbook (person*)> <!ELEMENT person (name, greet?, address*, (fax | tel)*, email*)> <!ELEMENT name (#PCDATA)> <!ELEMENT greet (#PCDATA)> <!ELEMENT address (#PCDATA)> <!ELEMENT tel (#PCDATA)> <!ELEMENT fax (#PCDATA)> <!ELEMENT email (#PCDATA)> ]> The syntax of a DTD is not XML syntax T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  31. The Address-Book XML Document • <addressbook> • <person> • <name> Ahmed Mumtaz</name> • <greet> Prof. Mumtaz</greet> • <email> am@yahoo.com </email> • </person> • </addressbook> T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  32. XML documents are similar to database files Example: A Relational database for school: student: course: enroll: T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  33. XML Representation of school database • <school> • <student id=“001”> • <name> Joe </name> <gpa> 3.0 </gpa> • </student> • <student id=“002”> • <name> Mary </name> <gpa> 4.0 </gpa> • </student> • <course cno=“331”> • <title> DB </title> <credit> 3.0 </credit> • </course> • <course cno=“350”> • <title> Web </title> <credit> 3.0 </credit> • </course> T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  34. XML Representation of school database <enroll> <id> 001 </id> <cno> 331 </cno> </enroll> <enroll> <id> 001 </id> <cno> 350 </cno> </enroll> <enroll> <id> 002 </id> <cno> 331 </cno> </enroll> </school> T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  35. Well-Formed XML Documents • An XML document (with or without a DTD) is well-formed if • Tags are syntactically correct • Every tag has an end tag • Tags are properly nested • There is a root tag • A start tag does not have two occurrences of the same attribute An XML document must be well formed T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  36. Strengthen the data-modeling capabilities of XML Using XML Schemas

  37. What is an XML Schema? The purpose of an XML Schema is to define the legal building blocks of an XML document, just like a DTD. 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 order of child elements • 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 and fixed values for elements and attributes T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  38. XML Schemas are the Successors of DTDs It is expected that XML Schemas will be used in most Web applications as a replacement for DTDs. Here are some reasons: • XML Schemas are extensible to future additions • XML Schemas are richer and more powerful than DTDs • XML Schemas are written in XML • XML Schemas support data types • XML Schemas support namespaces T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  39. XML Schema XML Schema is an XML-based alternative to DTD: Example: <xs:element name="note"><xs:complexType>  <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:element> T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  40. XML Schema The Schema in previous example is interpreted like this: Explanation of the Example: • <xs:element name="note"> defines the element called "note" • <xs:complexType> the "note" element is a complex type • <xs:sequence> the complex type is a sequence of elements • <xs:element name="to" type="xs:string"> the element "to" is of type string (text) • <xs:element name="from" type="xs:string"> the element "from" is of type string • <xs:element name="heading" type="xs:string"> the element "heading" is of type string • <xs:element name="body" type="xs:string"> the element "body" is of type string Everything is wrapped in "Well Formed" XML. T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  41. Why Use an XML Schema? • With XML Schema, the XML files can carry a description of its own format. • With XML Schema, independent groups of people can agree on a standard for interchanging data. T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  42. XML Schemas use XML Syntax Another great strength about XML Schemas is that they are written in XML: • You don't have to learn a new language • You can use your XML editor to edit your Schema files • You can use your XML parser to parse your Schema files • You can manipulate your Schemas with the XML DOM • You can transform your Schemas with XSLT T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  43. XML Schemas Support Data Types • One of the greatest strength of XML Schemas is the support for data types: • It is easier to describe document content • It is easier to define restrictions on data • It is easier to validate the correctness of data • It is easier to convert data between different data types T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  44. XML Schemas Support Data Types • One of the greatest strength of XML Schemas is the support for data types: • It is easier to describe document content • It is easier to define restrictions on data • It is easier to validate the correctness of data • It is easier to convert data between different data types T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  45. XML Schemas Secure Data Communication • When sending data from a sender to a receiver, it is essential that both parts have the same "expectations" about the content. • With XML Schemas, the sender can describe the data in a way that the receiver will understand. • A date like: "03-11-2004" will, in some countries, be interpreted as 3.November and in other countries as 11.March. • However, an XML element with a data type like this: • <date type="date">2004-03-11</date> • ensures a mutual understanding of the content, because the XML data type "date" requires the format "YYYY-MM-DD". T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  46. XML Schemas are Extensible XML Schemas are extensible, because they are written in XML. With an extensible Schema definition you can: • Reuse your Schema in other Schemas • Create your own data types derived from the standard types • Reference multiple schemas in the same document T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  47. Well-Formed is not Enough A well-formed XML document is a document that conforms to the XML syntax rules, like: • it must begin with the XML declaration • it must have one unique root element • start-tags must have matching end-tags • elements are case sensitive • all elements must be closed • all elements must be properly nested • all attribute values must be quoted • entities must be used for special characters T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

  48. The End eXtensable Markup Language (XML)Part – III Thank You T2-Lecture-5 Ahmed Mumtaz Mustehsan www.w3schools.com

More Related