XML was designed to describe data and to focus on what data is.
HTML was designed to display data and to focus on how data looks.
<?xml version="1.0" encoding="ISO-8859-1"?>
The first line in the document - the XML declaration - defines the XML version and the character encoding used in the document. In this case the document conforms to the 1.0 specification of XML and uses the ISO-8859-1 (Latin-1/West European) character set.
<?xml version="1.0" encoding="ISO-8859-1"?> -
the XML declaration<note> -
describes the root element of the document (like it was saying: "this document is a note")<to>Tove</to> -
next 4 lines describe 4 child elements of the root<from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> -
defines the end of the root element
<root> <child> <subchild>.....</subchild> </child> </root>
XSL - eXtensible Stylesheet Language
- is far more sophisticated than CSS. One way to use XSL is to transform XML into HTML before it is displayed by the browser
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="simple.xsl"?> <breakfast_menu> <food> <name>Belgian Waffles</name> <price>$5.95</price> <description> two of our famous Belgian Waffles </description> <calories>650</calories> </food> </breakfast_menu>
If you want to learn more about XSL, please visit our XSL tutorial.
With Internet Explorer, the unofficial <xml> tag can be used to create an XML data island. An XML data island is XML data embedded into an HTML page.
In the next example, we will embed an XML file called "cd_catalog.xml" into an HTML file.
The HTML file looks like this:
<html> <body> <xml id="cdcat" src="cd_catalog.xml"></xml> <table border="1" datasrc="#cdcat"> <tr> <td><span datafld="ARTIST"></span></td> <td><span datafld="TITLE"></span></td> </tr> </table> </body> </html>
The datasrc attribute of the <table> tag binds the HTML table element to the XML data island. The datasrc attribute refers to the id attribute of the data island.
<td> tags cannot be bound to data, so we are using <span> tags. The <span> tag allows the datafld attribute to refer to the XML element to be displayed. In this case, it is datafld="ARTIST" for the <ARTIST> element and datafld="TITLE" for the <TITLE> element in the XML file. As the XML is read, additional rows are created for each <CD> element.
A real-life example of how XML can be used to carry information.
XMLNews is a specification for exchanging news and other information.
Using such a standard makes it easier for both news producers and news consumers to produce, receive, and archive any kind of news information across different hardware, software, and programming languages.
An example XMLNews document:
<?xml version="1.0" encoding="ISO-8859-1"?> <nitf> <head> <title>Colombia Earthquake</title> </head> <body> <headline> <hl1>143 Dead in Colombia Earthquake</hl1> </headline> <byline> <bytag>By Jared Kotler, Associated Press Writer</bytag> </byline> <dateline> <location>Bogota, Colombia</location> <date>Monday January 25 1999 7:28 ET</date> </dateline> </body> </nitf>
To manipulate an XML document, you need an XML parser. The parser loads the document into your computer's memory. Once the document is loaded, its data can be manipulated using the DOM. The DOM treats the XML document as a tree.
To learn more about the XML DOM, please read our XML DOM tutorial.
There are some differences between Microsoft's XML parser and the XML parser used in Mozilla browsers. In this tutorial we will show you how to create cross browser scripts that will work in both Internet Explorer and Mozilla browsers.Read more about parsing in XML_Parser