Thursday, March 1, 2012

Understanding XML

XML is hardware and software independent tool to store and transport data.
XML stands for eXtensible markup language.
XML is a W3c consortium recommendation.
XML is a markup language much like HTML.
XML designed to store and carry data not to display data.
XML designed to be self-descriptive.
XML tags are not predefined, you must define your own tags.

Example
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Lucky</to>
<from>Mukesh</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

FACTS about XML

1. XML is designed to store and transport data and HTML designed for displaying data.
2. XML doesn't do anything its just designed for structure, store and transport data.

3. XML document forms a tree structure.
<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>
4. XML is not a replacement to HTML.
5. Generaly used to store, share and carry data.
6. It simplifies data sharing, because of its simple text format different incompitable applications can easily read it.
7. It simplifies data transport b/w different incompatible systems over internet, because of its simple text format.
8. It simplifies plateform changes without losing data, because of simple text format.
9. New languages invented by using XML like XHTML, RSS for news feeds, WSDL for describing available web services, XBRL etc.
10. XML document forms a tree that started from 'the root' and branches to 'the leaves'
11. In above ex. The first line is the XML declaration. It defines the XML version (1.0) and the encoding used (ISO-8859-1 = Latin-1/West European character set). The next line describes the root element of the document (like saying: "this document is a note"). Having 4 child elements.
12. XML document must have a root element.
<root>
<child>
<subchild>.....</subchild>
</child>
</root>

13. XML doesn't have predefined tags(ex. <p>, <h1>,<br>) like HTML, you have to invent your own tags(ex. <to>,<from>).
14. Tags are case sensitive.
15. Elements must have closing tags.
16. Elements must be nested properly.
<b><i>This text is bold and italic</b></i> Wrong
<b><i>This text is bold and italic</i></b> Right

17. XML attribute values must be quoted.
<title lang="en">Harry Potter</title> Right
<title lang= en >Harry Potter</title> Wrong

18. Some characters have special meanings in XML, so we use entity references in place of them. "<" and "&" are strictly illigal in XML.

&lt;
<
less than
&gt;
>
greater than
&amp;
&
ampersand
&apos;
'
apostrophe
&quot;
quotation mark

19. Comments in XML <!-- This is a comment -->.
20. XML preserves white spaces in document, while HTML trucate multiple white spaces into single white space.

XML Elements
21. An XML element is everything from (including) the element's start tag to (including) the element's end tag.
An element can contain text, attributes, other elements or all of given.
22. Elements are extensible. Like you can add more child elements in note.
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Lucky</to>
<from>Mukesh</from>
<subject>Making reminder</subject> <!--Newly added-->
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

XML Attributes
23. An attribute is an additional information piece with element.
Ex. <file type="gif">computer.gif</file>  type is an attribute.
24. Attribute Vs Element
<person sex="female"> Sex is attribute here.
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
<person>
<sex>female</sex> Sex is element here.
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>

25. Avoid attributes because
* Attribute can't hold multiple values while we can have multiple elements with values.
* Attributes can't have tree structure while elements can have.
* Attributes are not easily expandable for future changes.

26. An element may occur multiple times within same parent element with different IDs.
<messages>
<note id="211">
......
</note>
<note id="212">
......
</note>
</messages>

27. A "Well formed XML document" -
* 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

28. A "Valid XML document" - If a XML document is well formed then it is a Valid XML document.
29. To describe structure of XML document we create another document, that could be a DTD(Document Type Definition) or a XSD(XML Schema Definition). To understand XSD refer another post "Understanding XSD".
30. All major browsers displays XML document as it is. Because XML tags are user defined tags browsers are not aware about them. Ex. if a tag encountered <table> then how it will be treated a HTML table or a dining table.
31. You can add display information to XML document with CSS. Using CSS with XML document is not common method, W3c recommends XSLT(Read other post "understanding XSLT").

Example
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/css" href="cd_catalog.css"?> <!-- Reference of CSS file-->
<CATALOG>
  <CD>
   <ARTIST>Bob Dylan</ARTIST>
   <COMPANY>Columbia</COMPANY>
   <PRICE>10.90</PRICE>
</CD>
<CD>
   <ARTIST>Bonnie Tyler</ARTIST>
   <COUNTRY>UK</COUNTRY>
   <PRICE>9.90</PRICE>  
</CD>
.
.
.
</CATALOG>

CSS file -
CATALOG
{
background-color: #ffffff;
width: 100%;
}
CD
{
display: block;
margin-bottom: 30pt;
margin-left: 0;
}
ARTIST
{
color: #0000FF;
font-size: 20pt;
}
PRICE,COMPANY
{
display: block;
color: #000000;
margin-left: 20pt;
}

No comments:

Post a Comment

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...