Before you continue you should have some basic understanding of the following:
If you want to study these subjects, Go To W3Schools.
XML is not a replacement for HTML. XML and HTML were designed with different goals:
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.
HTML is about displaying information, XML is about describing information.
The tags used to markup HTML documents and the structure of HTML documents are predefined. The author of HTML documents can only use tags that are defined in the HTML standard.
XML allows the author to define his own tags and his own document structure.
It is important to understand that XML is not a replacement for HTML. In the future development of the Web it is most likely that XML will be used to structure and describe the Web data, while HTML will be used to format and display the same data.
We have been participating in XML development since its creation. It has been amazing to see how quickly the XML standard has been developed, and how quickly a large number of software vendors have adopted the standard.
We strongly believe that XML will be as important to the future of the Web as HTML has been to the foundation of the Web. XML is the future for all data transmission and data manipulation over the Web.
HTML pages are used to display data. Data is often stored inside HTML pages. With XML this data can now be stored in a separate XML file. This way you can concentrate on using HTML for formatting and display, and be sure that changes in the underlying data will not force changes to any of your HTML code.
XML data can also be stored inside HTML pages as “Data Islands”. You can still concentrate on using HTML for formatting and displaying the data.
In the real world, computer systems and databases contain data in incompatible formats. One of the
most time consuming challenges for developers has been to exchange data between such systems over the Internet. Converting the data to XML can greatly reduce this complexity and create data that can be read by different types of applications.
XML can also be used to store data in files or in databases. Applications can be written to store and retrieve information from the store, and generic applications can be used to display the data.
<?xml version="1.0"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
The first line in the document: The XML declaration should always be included. It defines the XML version of the document. In this case the document conforms to the 1.0 specification of XML:
<?xml version="1.0"?>
The next line defines the first element of the document (the root element):
<note>
The next lines defines 4 child elements of the root (to, from, heading, and
body):
<to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body>
The last line defines the end of the root element:
</note>
In HTML some elements do not have to have a closing tag. The following code is legal in HTML:
<p>This is a paragraph <p>This is another paragraph
In XML all elements must have a closing tag like this:
<p>This is a paragraph</p> <p>This is another paragraph</p>
XML tags are case sensitive. The tag <Letter> is different from the tag <letter>.
Opening and closing tags must therefore be written with the same case:
<Message>This is incorrect</message>
<message>This is correct</message>
In HTML some elements can be improperly nested within each other like this:
<b><i>This text is bold and italic</b></i>
In XML all elements must be properly nested within each other like this
<b><i>This text is bold and italic</i></b>
All XML documents must contain a single tag pair to define the root element. All other elements must be nested within the root element. All elements can have sub (children) elements. Sub elements must be in pairs and correctly nested within their parent element:
<root>
<child>
<subchild>
</subchild>
</child>
</root>
XML elements can have attributes in name/value pairs just like in HTML. In XML the attribute value must always be quoted. Study the two XML documents below. The first one is incorrect, the second is correct:
<?xml version="1.0"?> <note date=12/11/99> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
<?xml version="1.0"?> <note date="12/11/99"> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
XML attributes are normally used to describe XML elements, or to provide additional information about elements. From HTML you can remember this construct: <IMG SRC=”computer.gif”>. In this HTML example SRC is an attribute to the IMG element. The SRC attribute provides additional information about the element.
Attributes are always contained within the start tag of an element. Here are some examples:
HTML examples: <img src="computer.gif"> <a href="demo.asp"> XML examples: <file type="gif"> <person id="3344">
Usually, or most common, attributes are used to provide information that is not a part of the content of the XML document. Did you understand that? Here is another way to express that: Often attribute data is more important to the XML parser than to the reader. Did you understand it now? Anyway, in the example above, the person id is a counter value that is irrelevant to the reader, but important to software that wants to manipulate the person element.
Take a look at these examples:
Using an Attribute for sex: <person sex="female"> <firstname>Anna</firstname> <lastname>Smith</lastname> </person> Using an Element for sex: <person> <sex>female</sex> <firstname>Anna</firstname> <lastname>Smith</lastname> </person>
In the first example sex is an attribute. In the last example sex is an element. Both examples provides the same information to the reader.
There are no fixed rules about when to use attributes to describe data, and when to use elements. My experience is however; that attributes are handy in HTML, but in XML you should try to avoid them, as long as the same information can be expressed using elements.
Here is another example, demonstrating how elements can be used instead of attributes. The following three XML documents contain exactly the same information. A date attribute is used in the first, a date element is used in the second, and an expanded date element is used in the third:
<?xml version="1.0"?> <note date="12/11/99"> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> <?xml version="1.0"?> <note> <date>12/11/99</date> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> <?xml version="1.0"?> <note> <date> <day>12</day> <month>11</month> <year>99</year> </date> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
Why should you avoid using attributes? Should you just take my word for it? These are some of the problems using attributes:
If you start using attributes as containers for XML data, you might end up with documents that are both difficult to maintain and to manipulate. What I’m trying to say is that you should use elements to describe your data. Use attributes only to provide information that is not relevant to the reader. Please don’t end up like this:
<?xml version="1.0"?> <note day="12" month="11" year="99" to="Tove" from="Jani" heading="Reminder" body="Don't forget me this weekend!"> </note>
This don’t look much like XML. Got the point?
Rules always have exceptions. My rule about not using attributes has one too:
Sometimes I assign ID references to elements in my XML documents. These ID references can be used to access XML element in much the same way as the NAME or ID attributes in HTML. This example demonstrates this:
<?xml version="1.0"?>
<messages>
<note ID="501">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<note ID="502">
<to>Jani</to>
<from>Tove</from>
<heading>Re: Reminder</heading>
<body>I will not!</body>
</note>
</messages>
The ID in these examples is just a counter, or a unique identifier, to identify the different notes in the XML file.
Many applications support XML in a number of ways. In this Web we focus on the XML support in Internet Explorer 5.0. Some visitors have complained about this, but we don’t do it because IE5 is the only performer in the XML field. We do it because it is the only practical way to demonstrate XML to a large
audience over the Web.
So – while we are waiting for Netscape – most of our software examples will work only with IE5. If you want to learn XML the easy way – with lots of examples for you to try out – you will have to live with that.
Netscape has promised full XML support in its new Navigator 5 browser.
We hope that this will include standard support for the W3C XML, just as it does in Internet Explorer 5.
Based on previous experience we can only hope that Navigator and Explorer will be fully compatible in the future XML field.
Your option at the moment – if you want to work with Netscape and XML – is to work with XML on your server and transform your XML to HTML before it is sent to the browser. You can read more about transforming XML to HTML in the chapters about XSL.
Internet Explorer 5 fully supports the international standards for both XML 1.0 and the XML Document Object Model (DOM). These standards are set by the World Wide Web Consortium (W3C).
You can download IE5 from http://www.microsoft.com/windows/ie/
Internet Explorer 5.0 has the following XML support:
Examples of these features are given in the next chapters.
You can use IE5 to view an XML document just as you view any HTML page. There are several ways to open an XML document. You can click on a link, type the URL into the address bar, double-click on an XML document in a folder, and so on.
If you
point IE5 to an XML document, IE5 will display the document with its root element and child elements expanded. A plus (+) or minus sign
(-)
to the left
of the XML elements can be clicked to expand or collapse the element structure, and if you only want to view the raw XML source, you can select “View Source” from the browser menu.
If you click on the following filename: note.xml, IE5 will open the file in an explorer like view.
If an erroneous XML file is opened with IE5, IE5 will report the error in the file.
If you click on the following filename: note_error.xml, IE5 will display an error message.
To help you get the feeling of different types of XML data, we have collected the following XML data files for you:
To display XML data inside an HTML page you can use JavaScript to import data from an XML file. To see how XML and HTML complement each other this way; first look at the XML document (note.xml), then open the HTML document (note.htm) that contains a JavaScript which reads an XML file and displays the information inside the HTML page.
To see how it works,
To demonstrate how XML files can be formatted with CSS we have compiled the following XML files:
Take a look at this pure XML file: The CD Catalog
Then look at this style sheet: The CSS file
Finally, view: The CD Catalog formatted with the CSS file
Even if it looks right to use CSS this way, we strongly believe that formatting with XSL will be the standard way to format XML in future (or as soon as the main browsers support it).
Will we write our homepages in XML in the future?
No, we don’t think so. But we could not resist giving it a try : A
homepage written in XML
This might be the answer: Take
a XHTML tutorial
Follow this link to see how the content of an XML file can be
displayed in the browser.
Follow this link to see how the same XML file is displayed by using an XSL style sheet.
Follow this link to see how the same data can be prepared by the server and returned to the browser as an HTML file.
To learn more about XSL go to the XSL chapter.
A “Well Formed” XML document is a document that conforms to the XML syntax rules that we described in the previous chapter.
The following is a “Well Formed” XML document:
<?xml version="1.0"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
A “Valid” XML document is a “Well Formed” XML document which conforms to the rules of a Document Type Definition (DTD).
The following is the same document as above but with an added reference to a DTD:
<?xml version="1.0"?> <!DOCTYPE note SYSTEM "InternalNote.dtd"> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
You can read more about DTD in the chapter XML DTD.