XML is a language to describe the data
I have practiced here
<root> <child attributes="name"> <subchild1></subchild1> <subchild2></subchild2> <subchild3></subchild3> <subchild4></subchild4> </child> </root>
Just like the example here:
<bookstore> <book category="COOKING"> <title>Hamilton</title> <author>Zaki</author> <year>2015</year> <price>30</price> </book> <book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>JK Rowling.</author> <year>2005</year> <price>10</price> </book> <book category="PROGRAMMING"> <title lang="fr">Learning C++</title> <author>Zaki Live</author> <year>2015</year> <price>20</price> </book> </bookstore>
Root element is a must have element…
Element vs Attribute:
The way can be followed
<person gender="Male"> <firstname>Zaki</firstname> <lastname>Live</lastname> </person>
or like this
<person> <gender>Male<gender> <firstname>Zaki</firstname> <lastname>Live</lastname> </person>
It’s better to avoid attributes in XML ..It’s better for HTML but in XML it better to use as elements or subchild what you say….It’s not stricted like html that where to use Attribute and where to use as element
Sometimes in XML we have to use attributes for metadata identify the xml
<messages> <note id="405"> <to>ahmed@zakilive.com</to> <from>demo@zakilive.com</from> <heading>Hello Zaki !</heading> <body>Hiii</body> </note> <note id="406"> <to>zaki@zakilive.com</to> <from>demo@zakilive.com</from> <heading>Hello Zaki Live !</heading> <body>How are you ?</body> </note> </messages>
XML namespaces: Various method can be followed
Same Table name can conflict in XML:
like this
<table> <tr> <td>Apple</td> <td>Banana</td> </tr> </table>
with this
<table> <name>Egg White</name> <quantity>5 dozens</quantity> <price>500 Taka</price> </table>
two are diffent meaning but name is same so it can be conflict
solution is to using prefix before the name:
just like this
<a:table> <a:tr> <a:td>Apple</a:td> <a:td>Banana</a:td> </a:tr> </a:table>
<b:table> <b:name>Egg White</b:name> <b:quantity>5 dozens</b:qunatity> <b:price>500 Taka<b:price> </b:table>
So it will never be conflict đ
XML Namespcaes – xmlns attribute
<root> <a:table xmlns:a="http://www.zakilive.com/html"> <a:tr> <a:td>Apple</a:td> <a:td>Banana</a:td> </a:tr> </a:table> <b:table xmlns:b="http://www.zakilive.com/murgirdim"> <b:name>Egg White</b:name> <b:quantity>5 dozens</b:qunatity> <b:price>500 Taka<b:price> </b:table> </root>
it can also write like this:
<root xmlns:a="http://www.zakilive.com/html" xmlns:b="http://www.zakilive.com/murgirdim"> <a:table> <a:tr> <a:td>Apple</a:td> <a:td>Banana</a:td> </a:tr> </a:table> <b:table> <b:name>Egg White</b:name> <b:quantity>5 dozens</b:qunatity> <b:price>500 Taka<b:price> </b:table> </root>
Default Namespaces:
xmlns=”zakiliveURL”
The XML that is carrying HTML table
<table xmlns="http://www.zakilive.com/html"> <tr> <td>Apple</td> <td>Banana</td> </tr> </table>
The XML that is carrying egg white:
<table xmlns="http://www.zakilive.com/murgirdim"> <name>Egg White</name> <quantity>5 dozens</quantity> <price>500 Taka</price> </table>
Namespcaes in real life uses: here XSLT used XSLT is stands for XML Transformaion which transform XML documents into other formats like XHTML
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transofrm"> <xsl:template match="/"> <html> <body> <h2>My CD collection</h2> <table border="1"> <tr> <th style="text-align:left">Title</th> <th style="text-align:left">Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"></td> <td><xsl:value-of select="artist"></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>