Session 1


Published on

XML by Example Session 1

Published in: Technology, News & Politics
  • Be the first to comment

  • Be the first to like this

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

No notes for slide

Session 1

  1. 1. Session 1 Introduction to XML and Namespace Create by ChungLD faculty XML by Example / Bachkhoa – Aptech Computer Education /34
  2. 2. Objectives <ul><li>Introduction to XML </li></ul><ul><li>Exploring XML </li></ul><ul><li>Working with XML </li></ul><ul><li>XML Syntax </li></ul><ul><li>XML Namespaces </li></ul><ul><li>Working with Namespaces Syntax </li></ul>Create by Chungld faculty /34
  3. 3. Overview Markup languages <ul><li>GML (Generalized Markup Language-1970) describes the document in terms of its format, structure and other properties. </li></ul><ul><li>SGML (Standard Generalized Markup Language-1980) ensures that the system can represent the data in its own way. </li></ul><ul><li>HTML (Hyper text Markup Language - 1989) used ASCII text, which allows the user to use any text editor </li></ul>Create by Chungld faculty /34
  4. 4. Evolution of XML <ul><li>XML is a W3C recommendation </li></ul><ul><li>The first version (1.0) introduced in 1998 </li></ul><ul><li>XML is a set of rules for defining semantic tags that break a document into parts and identify the different parts of the document. </li></ul><ul><li>XML was developed over HTML because of the basic differences between them given in the table below. </li></ul>Create by Chungld faculty /34
  5. 5. Comparison HTML and XML Create by Chungld faculty /34
  6. 6. Features of XML <ul><li>XML stands for Extensible Markup Language </li></ul><ul><li>XML is a markup language much like HTML </li></ul><ul><li>XML was designed to describe data </li></ul><ul><li>XML tags are not predefined. You must define your own tags </li></ul><ul><li>XML uses a Document Type Definition (DTD) or an XML Schema to describe the data </li></ul><ul><li>XML with a DTD or XML Schema is designed to be self - descriptive </li></ul>Create by Chungld faculty /34
  7. 7. Benefits of XML <ul><li>Data Independence </li></ul><ul><li>Easier to parse </li></ul><ul><li>Reducing Server Load </li></ul><ul><li>Easier to create </li></ul><ul><li>Web site Content </li></ul><ul><li>Remote Procedure Calls </li></ul><ul><li>e-Commerce </li></ul>Create by Chungld faculty /34
  8. 8. Example XML Document <ul><li><?xml version=“1.0” encoding=“iso-8859-1” ?> </li></ul><ul><li>< FlowerPlanet > </li></ul><ul><li>< Name > Rose </ Name > </li></ul><ul><li>< Price > $1 </ Price > </li></ul><ul><li>< Description > Red in color </ Description > </li></ul><ul><li>< Number > 700 </ Number > </li></ul><ul><li></ FlowerPlanet > </li></ul>Create by Chungld faculty /34
  9. 9. XML Document Structure Create by Chungld faculty /34
  10. 10. XML document life cycle <ul><li>XML parser creates, manipulates and updates the XML document. It will read the list of records in the XML document and the handler stores it in the form of a data structure. The handler then processes it and displays it in HTML </li></ul>Create by Chungld faculty /34
  11. 11. XML Editors <ul><li>The main functions that editors provide: </li></ul><ul><ul><li>Add opening and closing tags to the code </li></ul></ul><ul><ul><li>Check for validity of XML </li></ul></ul><ul><ul><li>Verify XML against a DTD/Schema </li></ul></ul><ul><ul><li>Perform series of transforms over a document </li></ul></ul><ul><ul><li>Color the XML syntax </li></ul></ul><ul><ul><li>Display the line numbers </li></ul></ul><ul><ul><li>Present the content and hide the code </li></ul></ul><ul><ul><li>Complete the word </li></ul></ul>Create by Chungld faculty /34
  12. 12. XML Parsers <ul><li>An XML parser/XML processor reads the document and verifies it for its well-formedness. After the document is verified, the processor converts the document into a tree of elements or a data structure. </li></ul><ul><li>Commonly used parser are: </li></ul><ul><ul><li>MSXML </li></ul></ul><ul><ul><li>Oracle XML Parser </li></ul></ul><ul><ul><li>Java API for XML </li></ul></ul>Create by Chungld faculty /34
  13. 13. XML Parsers… <ul><li>The two types of parsers are: </li></ul><ul><ul><li>Non validating parser: It checks the well formedness of the document. Read the document and checks for its conformity with XML standards. </li></ul></ul><ul><ul><li>Validating parser: It checks the validity of the document using DTD </li></ul></ul>Create by Chungld faculty /34
  14. 14. Browsers <ul><li>After the XML document is read, the parser passes the data structure to the client application. The application can be a web browser. Other programs like database, MIDI program or a spreadsheet program. </li></ul><ul><li>Commonly used web browsers are: </li></ul><ul><ul><li>Netscape </li></ul></ul><ul><ul><li>Mozilla </li></ul></ul><ul><ul><li>Internet Explorer </li></ul></ul><ul><ul><li>Firefox </li></ul></ul><ul><ul><li>Opera </li></ul></ul>Create by Chungld faculty /34
  15. 15. Building an XML document <ul><li>The steps to build an XML document are: </li></ul><ul><ul><li>Create an XML document in an editor. </li></ul></ul><ul><ul><li>Save the XML document </li></ul></ul><ul><ul><li>Load XML document in a browser </li></ul></ul>Create by Chungld faculty /34 Click and see
  16. 16. Exploring the XML document <ul><li>XML version declaration </li></ul><ul><li>Document Type Definition </li></ul><ul><li>Document instance in which the content is defined by the markup. </li></ul>Create by Chungld faculty /34 Click and see
  17. 17. Well-formed XML document <ul><li>Well-formness refers to the standards that are to be followed by the XML document. A document is well formed, if it fullfills the following rules: </li></ul>Create by Chungld faculty /34
  18. 18. XML Documents Must Have a Root Element Create by Chungld faculty /34 <root>   <child>     <subchild>.....</subchild>   </child> </root>
  19. 19. XML Tags are Case Sensitive Create by Chungld faculty /34 <ul><ul><li><Message>This is incorrect</message> <message>This is correct</message> </li></ul></ul>
  20. 20. All XML Elements Must Have a Closing Tag <ul><li>In HTML, you will often see elements that don't have a closing tag: </li></ul><ul><li>In XML, it is illegal to omit the closing tag. All elements must have a closing tag: </li></ul>Create by Chungld faculty /34 <p>This is a paragraph <p>This is another paragraph <p>This is a paragraph</p> <p>This is another paragraph</p>
  21. 21. XML Elements Must be Properly Nested <ul><li>In HTML, you might see improperly nested elements: </li></ul><ul><li>In XML, all elements must be properly nested within each other: </li></ul>Create by Chungld faculty /34 <b><i>This text is bold and italic</b></i> <b><i>This text is bold and italic</i></b>
  22. 22. XML Attribute Values Must be Quoted <ul><li>In XML the attribute value must always be quoted. Study the two XML documents below. The first one is incorrect, the second is correct: </li></ul>Create by Chungld faculty /34 <note date=12/11/2007>   <to>Tove</to>   <from>Jani</from> </note> <note date=“12/11/2007”>   <to>Tove</to>   <from>Jani</from> </note>
  23. 23. Entity References <ul><li>Some characters have a special meaning in XML. </li></ul><ul><li>If you place a character like &quot;<&quot; inside an XML element, it will generate an error because the parser interprets it as the start of a new element. </li></ul><ul><li>This will generate an XML error: </li></ul><ul><li><message>if salary < 1000 then</message> </li></ul><ul><li>To avoid this error, replace the &quot;<&quot; character with an entity reference : </li></ul><ul><li><message>if salary &lt; 1000 then</message> </li></ul>Create by Chungld faculty /34
  24. 24. Comments in XML <ul><li>Comments is same HMTL </li></ul><ul><li><!-- Comments --> </li></ul>Create by Chungld faculty /34 Click and see
  25. 25. Processing Instructions <ul><li>Processing instructions are information which is application specific. The instructions do not follow XML rules or internal syntax. With the help of a parser these instructions are passed to the application </li></ul><ul><li><?PITarget <instructions>?> </li></ul>Create by Chungld faculty /34
  26. 26. White-space is Preserved in XML <ul><li>HTML truncates multiple white-space characters to one single white-space: </li></ul><ul><li>HTML: Hello           my name is Tove Output: Hello my name is Tove. </li></ul><ul><li>With XML, the white-space in a document is not truncated. </li></ul>Create by Chungld faculty /34
  27. 27. Classification to character data <ul><li>An XML document is divided into markup and character data. Character data describes the document’s actual content with the white space. The text in character data is not processed by the parser and thus not treated as a regular text. </li></ul>Create by Chungld faculty /34 Character data PCDATA CDATA
  28. 28. PCDATA <ul><li>The data that is parsed by the parser is called as parsed character data (PCDATA). The PCDATA specifies that the element has parsed character data. It is used in the element declaration. </li></ul><ul><li>Example </li></ul><ul><ul><li><Semster>X<5&10</Semster> </li></ul></ul><ul><ul><li>A result it will generate an error as shown: </li></ul></ul>Create by Chungld faculty /34
  29. 29. CDATA <ul><li>The text inside a CDATA section is not parsed by the XML parser. A text is considered in a CDATA section if it contains ‘<‘ or ‘<&>’ character. The CDATA section cannot be nested. It also does not accept line breaks or spaces inside the <![CDATA[ 0 <X< =10   ]]> string. Comments are also not recognized. </li></ul>Create by Chungld faculty /34 Click and see
  30. 30. What is an XML Element? <ul><li>An XML element is everything from (including) the element's start tag to (including) the element's end tag. </li></ul><ul><li>An element can contain other elements, simple text or a mixture of both. Elements can also have attributes. </li></ul><ul><li>XML elements must follow these naming rules: </li></ul><ul><ul><li>Names can contain letters, numbers, and other characters </li></ul></ul><ul><ul><li>Names cannot start with a number or punctuation character </li></ul></ul><ul><ul><li>Names cannot start with the letters xml (or XML, or Xml, etc) </li></ul></ul><ul><ul><li>Names cannot contain spaces </li></ul></ul>Create by Chungld faculty /34
  31. 31. Example elements <ul><li><bookstore>   <book category=&quot;CHILDREN&quot;>     <title>Harry Potter</title>     <author>J K. Rowling</author>     <year>2005</year>     <price>29.99</price>   </book>   <book category=&quot;WEB&quot;>     <title>Learning XML</title>     <author>Erik T. Ray</author>     <year>2003</year>     <price>39.95</price>   </book> </bookstore> </li></ul>Create by Chungld faculty /34
  32. 32. XML Attributes <ul><li>XML elements can have attributes in the start tag, just like HTML. </li></ul><ul><li>Attributes provide additional information about elements. </li></ul><ul><li><elementName attName1=“value” attName2=“value”..> </li></ul>Create by Chungld faculty /34 <ul><ul><li><person sex=&quot;female&quot;>   <firstname>Anna</firstname>   <lastname>Smith</lastname> </person> </li></ul></ul>
  33. 33. XML Validation <ul><li>XML with correct syntax is &quot;Well Formed&quot; XML. </li></ul><ul><li>XML validated against a DTD is &quot;Valid&quot; XML or XML Schema </li></ul>Create by Chungld faculty /34
  34. 34. Namespaces <ul><li>Duplicate element names </li></ul><ul><li>Consequences of duplicate element names </li></ul>Create by Chungld faculty /34 <Cd> <Name>…</Name> </Cd> <Singer> <Name>…</Name> </Singer> <ul><li>How do I search for all CD title/names? </li></ul><ul><li>How do I differentiate between cd name and singer name? </li></ul>
  35. 35. Define Namespaces <ul><li>In XML, elements are distinguished by using namespaces. XML Namspaces provide a globally unique name for an element or attribute so that they do not conflict on another. </li></ul>Create by Chungld faculty /34
  36. 36. Prefixing element names <ul><li>Using prefixes in the element names provide a means for the document authors to prevent name collisions </li></ul><ul><li><CD:Title>Feel</CD:Title> </li></ul><ul><li>and </li></ul><ul><li><Book:Title>Returning to Earth</Book:Title> </li></ul><ul><li>But if the prefixes are not unique, the original problem of duplication would still exist. To solve this problem, each namespace prefix is added to a Uniform Resource Identifier or URI that uniquely identifies the namspace. </li></ul>Create by Chungld faculty /34
  37. 37. Syntax to add namespace URI <ul><li><elementName xmlns:namespacePrefix=“URI”> </li></ul><ul><li>You can place attributes in a namespace </li></ul><ul><li>prefix:localname=“value” </li></ul>Create by Chungld faculty /34 Click and see
  38. 38. Default namspace <ul><li>A default namespace using the xmlns attribute with a URI as its value. Once this default namespace is declared, child elements that are part of this namespace do not need a namespace prefix. </li></ul>Create by Chungld faculty /34 Click and see
  39. 39. Override default namespaces <ul><li>The default namespace applies to the element on which it was defined and all descendants of that element. If one of the descendants has another default namespace defined on it, this new namespace definition overrides the previous one and becomes the default for that element and all its descendants. </li></ul>Create by Chungld faculty /34 Click and see
  40. 40. Refer Website <ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul>Create by Chungld faculty /34
  41. 41. Summary and workshop Create by Chungld faculty /34