16. Advantages (Features) of XML - 1 XML simplifies data sharing Since XML data is stored in plain text format, data can be easily shared among different hardware and software platforms. XML separates data from HTML To display dynamic data in HTML, the code must be rewritten each time the data changes. With XML, data can be stored in separate files so that whenever the data changes it is automatically displayed correctly. We have to design the HTML for layout only once.
17.
18. With XML, data can be easily exchanged between different platforms.
23. Applications of XML File Converters Many applications have been written to convert existing documents into the XML standard. An example is a PDF to XML converter. Cell Phones - XML data is sent to some cell phones. This data is then formatted to display text or images, and even to play sounds! VoiceXML - Converts XML documents into an audio format so that you can listen to an XML document.
24. XML Document – Example 1 <?xml version="1.0" encoding="ISO-8859-1"?> <class_list> <student> <name>Anamika</name> <grade>A+</grade> </student> <student> <name>Veena</name> <grade>B+</grade> </student> </class_list>
38. XML – Example 2 <bookstore> <book category = "COOKING"> <title lang = "en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price></book> <book category = "CHILDREN"> <title lang = "en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price></book> <book category = "WEB"> <title lang = "en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price></book> </bookstore>
39. Important Definitions XML Element An element is a start tag, content, and an end tag. E.g., <greeting>”Hello World</greeting> XML Attribute An attribute provides additional information about elements E.g., <note priority = “high”>
40. Important Definitions Child elements – XML elements may have child elements <employee id = “100”> <name> <first>Anita</first> <initial>D</initial> <last>Singh</last> </name> </employee> Parent Element Name Children of parent element
41. XML Element An XML element is everything from the element's start tag to the element's end tag. An element can contain other elements, simple text or a mixture of both. Elements can also have attributes.
42. XML Syntax Rules All XML elements must have a closing tag XML tags are case sensitive. The tag <Book> is different from the tag <book> Opening and closing tags must be written with the same case <Message>This is incorrect</message><message>This is correct</message>
43.
44.
45. XML Syntax Rules XML Entity References Some characters have a special meaning in XML. E.g., If you place a character like "<" inside an XML element, it will generate an error because the parser interprets it as the start of a new element. <message>if salary < 1000 then </message> To avoid this error, replace the "<" character with an entity reference: <message>if salary < 1000 then</message>
46. XML Syntax Rules XML Entity References There are 5 predefined entity references in XML:
54. Different Types of XML Markups 5 Types of Markup in XML Elements Entities Comments Processing Instructions Ignored Sections
55. Element Markup Element Markup It is composed of 3 parts: start tag, the content, and the end tag. Example: <name>Neetu</name> The start tag and the end tag can be treated as wrappers The element name that appears in the start tag must be exactly the same as the name that appears in the end tag. Example: <Name>Neetu</name>
56. Different Types of XML Markups Attribute Markup Attributes are used to attach information to the information contained in an element. General syntax for attributes is: <elementname property = ‘value’> Or <elementname property = “value”> Attribute value must be enclosed within quotation marks Use either single quotes or double quotes but don’t mix them.
57. Attribute Markup If we specify the attributes for the same element more than once, the specifications are merged. <?xml version = “1.0”?> <myparas> <para num = “first”>This is Para 1 </para> <para num = ‘second’ color = “red”>This is Para 2</para> <myparas>
58. Attribute Markup When the XML processor encounters line 3, it will record the fact that para element has the num attribute When it encounters the 4th line it will record the fact that para element has the color attribute
59. Reserved Attribute The xml:lang attribute is reserved to identify the human language in which the element was written The value of attribute is one of the following: en English fr French de German
60.
61. Similar to attributes in HTML e.g., <IMG SRC=“sky.jpg”> In this SRC is the attribute
63. XML elements can have attributes in name/value pairs just like in HTML.
64. In XML the attribute value must always be quoted.<note date = 01/01/2010> <---------- This is invalid<to>Priya</to><from>Deeali</from> </note> <note date = “01/01/2010”> --------- Now OK since enclosed in double quotes <note date = ‘01/01/2010’> --------- This is also OK since enclosed in single quotes
65. XML Attributes and Elements Consider the following example: <person gender = "female"> <firstname>Geeta</firstname> <lastname>Shah</lastname></person> Gender is an attribute Gender is an element <person> <gender>female</gender> <firstname>Geeta</firstname> <lastname>Shah</lastname></person>
74. The ID can then be used to identify the XML element <messages><note id="501"> <to>Tina</to> <from>Yasmin</from> <heading>Reminder</heading> <body>Happy Birthday!</body></note><note id="502"> <to>Yasmin</to> <from>Tina</from> <heading>Re: Reminder</heading> <body>Thank you, my dear</body></note></messages>
75.
76. What does Extensible mean in XML? Now suppose the author of the XML document added some extra information to it: <note><date>2008-01-10</date><to>Anita</to><from>Veena</from><heading>Reminder</heading><body>You have an exam tomorrow</body> </note>
77. What does Extensible mean in XML? This application will not crash because it will still find the <to>, <from> and <body> elements in the XML document and produce the same output.
78. XML Validation What is a “well formed” XML document? XML with correct syntax is "Well Formed" XML. A "Well Formed" XML document has correct XML syntax. XML documents must have a root element XML elements must have a closing tag XML tags are case sensitive XML elements must be properly nested XML attribute values must be quoted
79.
80.
81.
82.
83.
84. These examples are invalid since they are both examples forgetting the semi-colon following the character entity. Bad Examples… <h1>Jack & Jill</h1> <equation>5 < 2</equation>
87. Element names may contain letters, numbers, hyphens, periods and underscores inclusively.BAD EXAMPLES <bad*characters> <illegal space> <99number-start> GOOD EXAMPLES <example-one> <_example2> <Example.Three>
88.
89.
90.
91. Viewing XML Files - 2 The XML document will be displayed with color-coded root and child elements. A plus (+) or minus sign (-) to the left of the elements can be clicked to expand or collapse the element structure. To view the raw XML source (without the + and - signs), select "View Page Source" or "View Source" from the browser menu.
92. Viewing XML Files - 3 Why XML documents display like this? XML documents do not carry information about how to display the data. Since XML tags are created by the user of the XML document, browsers do not know if a tag like <table> describes an HTML table or a dining table. Without any information about how to display the data, most browsers will just display the XML document as it is.
93. Using CSS to display XML Files CSS (Cascading Style Sheets) can be used to format a XML document. Consider this XML document: