XML Tutorial
XML Tutorial
(BASIC)
XML stands for EXtensible Markup Language.
XML was designed to transport and store data.
In this tutorial you will learn about XML, and the difference between XML and HTML.
XML is important to know, and very easy to learn.
Introduction to XML
XML was designed to transport and store data.
HTML was designed to display data.
What You Should Already Know
Before you continue you should have a basic understanding of the following:
HTML
JavaScript
If you want to study these subjects first, find the tutorials on our Home page.
What is XML?
XML stands for EXtensible Markup Language
XML is a markup language much like HTML
XML was designed to carry data, not to display data
XML tags are not predefined. You must define your own tags
XML is designed to be self-descriptive
XML is a W3C Recommendation
The Difference Between XML and HTML
XML is not a replacement for HTML.
XML and HTML were designed with different goals:
XML was designed to transport and store data, with focus on what data is.
HTML was designed to display data, with focus on how data looks.
HTML is about displaying information, while XML is about carrying information.
XML Does not DO Anything
Maybe it is a little hard to understand, but XML does not DO anything. XML was created to structure, store,
and transport information.
The note above is quite self descriptive. It has sender and receiver information, it also has a heading and a
message body.
But still, this XML document does not DO anything. It is just pure information wrapped in tags. Someone
must write a piece of software to send, receive or display it.
XML is Just Plain Text
XML is nothing special. It is just plain text. Software that can handle plain text can also handle XML.
However, XML-aware applications can handle the XML tags specially. The functional meaning of the tags
depends on the nature of the application.
With XML You Invent Your Own Tags
The tags in the example above (like <to> and <from>) are not defined in any XML standard. These tags are
"invented" by the author of the XML document.
That is because the XML language has no predefined tags.
The tags used in HTML (and the structure of HTML) are predefined. HTML documents can only use tags
defined in the HTML standard (like <p>, <h1>, etc.).
XML allows the author to define his own tags and his own document structure.
XML is Not a Replacement for HTML
XML is a complement to HTML.
It is important to understand that XML is not a replacement for HTML. In most web applications, XML is used
to transport data, while HTML is used to format and display the data.
My best description of XML is this:
XML is a software and hardware independent tool for carrying information.
XML is a W3C Recommendation
1
XML became a W3C Recommendation 10. February 1998.
XML is Everywhere
We have been participating in XML development since its creation. It has been amazing to see how quickly
the XML standard has developed, and how quickly a large number of software vendors have adopted the
standard.
XML is now as important for the Web as HTML was to the foundation of the Web.
XML is everywhere. It is the most common tool for data transmissions between all sorts of applications, and
is becoming more and more popular in the area of storing and describing information.
XML data is stored in plain text format. This provides a software- and hardware-independent way of
storing data.
This makes it much easier to create data that different applications can share.
XML Tree
XML documents form a tree structure that starts at "the root" and branches to "the leaves".
An Example XML Document
2
XML documents use a self-describing and simple syntax:<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
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"):
<note>
The next 4 lines describe 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>
And finally the last line defines the end of the root element:</note>
You can assume, from this example, that the XML document contains a note to Tove from Jani.
Don't you agree that XML is pretty self-descriptive?
XML Documents Form a Tree Structure
XML documents must contain a root element. This element is "the parent" of all other elements.
The elements in an XML document form a document tree. The tree starts at the root and branches to the
lowest level of the tree.
All elements can have sub elements (child elements):<root>
<child>
<subchild>.....</subchild>
</child>
</root>
The terms parent, child, and sibling are used to describe the relationships between elements. Parent
elements have children. Children on the same level are called siblings (brothers or sisters).
All elements can have text content and attributes (just like in HTML).
Example:
3
<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>
The root element in the example is <bookstore>. All <book> elements in the document are contained within
<bookstore>.
The <book> element has 4 children: <title>,< author>, <year>, <price>.
<message>This is correct</message>
Note: "Opening and closing tags" are often referred to as "Start and end tags". Use whatever you prefer. It is
exactly the same thing.
</i></b>In the example above, "Properly nested" simply means that since the <i> element is opened inside
the <b> element, it must be closed inside the <b> element.
XML Documents Must Have a Root Element
XML documents must contain one element that is the parent of all other elements. This element is called the
root element.<root>
<child>
<subchild>.....</subchild>
</child></root>
4
<to>Tove</to>
<from>Jani</from>
</note>
The error in the first document is that the date attribute in the note element is not quoted.
Entity References
Some characters have a special meaning in XML.
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.
This will generate an XML error:<message>if salary < 1000 then</message>
To avoid this error, replace the "<" character with an entity reference:<message>if salary < 1000
then</message>
Comments in XML
The syntax for writing comments in XML is similar to that of HTML.
<!-- This is a comment -->
With XML, White Space is Preserved
HTML reduces multiple white space characters to a single white space:
HTML: Hello my name is Tove
Output: Hello my name is Tove.
In Windows applications, a new line is normally stored as a pair of characters: carriage return (CR) and line
feed (LF). The character pair bears some resemblance to the typewriter actions of setting a new line. In Unix
applications, a new line is normally stored as a LF character. Macintosh applications use only a CR
character to store a new line.
XML Elements
An element can contain other elements, simple text or a mixture of both. Elements can also have
attributes.<bookstore>
<book category="CHILDREN">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
5
In the example above, <bookstore> and <book> have element contents, because they contain other
elements. <author> has text content because it contains text.
In the example above only <book> has an attribute (category="CHILDREN").
XML Naming Rules
Let's imagine that we created an application that extracted the <to>, <from>, and <body> elements from the
XML document to produce this output:MESSAGE
To: Tove
From: Jani
Don't forget me this weekend!
Imagine that the author of the XML document added some extra information to it:
<note>
<date>2008-01-10</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body></note>
Should the application break or crash?No. The application should still be able to find the <to>, <from>, and
<body> elements in the XML document and produce the same output.
One of the beauties of XML, is that it can often be extended without breaking applications.
XML Attributes
XML elements can have attributes in the start tag, just like HTML.
Attributes provide additional information about elements.
XML Attributes
From HTML you will remember this: <img src="computer.gif">. The "src" attribute provides additional
information about the <img> element.
In HTML (and in XML) attributes provide additional information about elements:<img src="computer.gif">
<a href="demo.asp">
6
Attributes often provide information that is not a part of the data. In the example below, the file type is
irrelevant to the data, but important to the software that wants to manipulate the element:<file
type="gif">computer.gif</file>
In the first example sex is an attribute. In the last, sex is an element. Both examples provide the same
information.
There are no rules about when to use attributes and when to use elements. Attributes are handy in HTML. In
XML my advice is to avoid them. Use elements instead.
My Favorite Way
The following three XML documents contain exactly the same information:
7
attributes are not easily expandable (for future changes)
Attributes are difficult to read and maintain. Use elements for data. Use attributes for information that is not
relevant to the data.
The ID above is just an identifier, to identify the different notes. It is not a part of the note itself.
What I'm trying to say here is that metadata (data about data) should be stored as attributes, and that data
itself should be stored as elements.
XML Validation
XML with correct syntax is "Well Formed" XML.
XML validated against a DTD is "Valid" XML.
Well Formed XML Documents
A "Well Formed" XML document has correct XML syntax.
The syntax rules were described in the previous chapters:
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<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
The DOCTYPE declaration in the example above, is a reference to an external DTD file. The content of the
file is shown in the paragraph below.
8
XML DTD
The purpose of a DTD is to define the structure of an XML document. It defines the structure with a list of
legal elements:<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
If you want to study DTD, you will find our DTD tutorial on our homepage.
XML Schema
W3C supports an XML based alternative to DTD called XML Schema:<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence></xs:complexType></xs:element>
XML Validator
Errors in XML documents will stop your XML applications.
The W3C XML specification states that a program should stop processing an XML document if it finds an
error. The reason is that XML software should be small, fast, and compatible.
HTML browsers will display documents with errors (like missing end tags). HTML browsers are big and
incompatible because they have a lot of unnecessary code to deal with (and display) HTML errors.
With XML, errors are not allowed.
9
Sample XML file: The CD catalog
Empire Burlesque Bob Dylan USA Columbia 10.90 1985 Hide your heart Bonnie Tyler UK CBS Records
9.90 1988 Greatest Hits Dolly Parton USA RCA 9.90 1982 Still got the blues Gary Moore UK Virgin records
10.20 1990 Eros Eros Ramazzotti EU BMG 9.90 1997 One night only Bee Gees UK Polydor 10.90 1998
Sylvias Mother Dr.Hook UK CBS 8.10 1973 Maggie May Rod Stewart UK Pickwick 8.50 1990 Romanza
Andrea Bocelli EU Polydor 10.80 1996 When a man loves a woman Percy Sledge USA Atlantic 8.70 1987
Black angel Savage Rose EU Mega 10.90 1995 1999 Grammy Nominees Many USA Grammy 10.20 1999
For the good times Kenny Rogers UK Mucik Master 8.70 1995 Big Willie style Will Smith USA Columbia
9.90 1997 Tupelo Honey Van Morrison UK Polydor 8.20 1971 Soulsville Jorn Hoel Norway WEA 7.90 1996
The very best of Cat Stevens UK Island 8.90 1990 Stop Sam Brown UK A and M 8.90 1988 Bridge of Spies
T'Pau UK Siren 7.90 1987 Private Dancer Tina Turner UK Capitol 8.90 1983 Midt om natten Kim Larsen EU
Medley 7.80 1983 Pavarotti Gala Concert Luciano Pavarotti UK DECCA 9.90 1991 The dock of the bay Otis
Redding USA Atlantic 7.90 1987 Picture book Simply Red EU Elektra 7.20 1985 Red The Communards UK
London 7.80 1987 Unchain my heart Joe Cocker USA EMI 8.20 1987
Below is a fraction of the XML file. The second line links the XML file to the CSS file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/css" href="cd_catalog.css"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
10
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
.
.
.
.
</CATALOG>
Formatting XML with CSS is not the most common method.
W3C recommend using XSLT instead. See the next chapter.
Below is a fraction of the XML file. The second line links the XML file to the XSLT file:<?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 XSLT, find our XSLT tutorial on our homepage.
Transforming XML with XSLT on the Server
In the example above, the XSLT transformation is done by the browser, when the browser reads the XML
file.
Different browsers may produce different result when transforming XML with XSLT. To reduce this problem
the XSLT transformation can be done on the server.
Note that the result of the output is exactly the same, either the transformation is done by the web server or
by the web browser.
XML JAVASCRIPT
XML Parser
Most browsers have a built-in XML parser to read and manipulate XML.
The parser converts XML into a JavaScript accessible object.
Parsing XML
All modern browsers have a built-in XML parser that can be used to read and manipulate XML.
The parser reads XML into memory and converts it into an XML DOM object that can be
accessed with JavaScript.
11
You will learn more about the XML DOM in the next chapter of this tutorial.
There are some differences between Microsoft's XML parser and the parsers used in other
browsers. The Microsoft parser supports loading of both XML files and XML strings (text), while
other browsers use separate parsers. However, all parsers contain functions to traverse XML
trees, access, insert, and delete nodes (elements) and their attributes.
In this tutorial we will show you how to create scripts that will work in both Internet Explorer and
other browsers.
Note: When we talk about parsing XML, we often use the term "Nodes" about XML elements.
Loading XML with Microsoft's XML Parser
Example explained:
The first line of the script above creates an empty Microsoft XML document object.
The second line turns off asynchronized loading, to make sure that the parser will not continue
execution of the script before the document is fully loaded.
The third line tells the parser to load an XML document called "note.xml".
The following JavaScript fragment loads a string called txt into the parser:var xmlDoc=new
ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(txt);
Note: The loadXML() method is used for loading strings (text), load() is used for loading files.
Example explained:
The first line of the script above creates an empty XML document object.
The second line turns off asynchronized loading, to make sure that the parser will not continue
execution of the script before the document is fully loaded.
The third line tells the parser to load an XML document called "note.xml".
The following JavaScript fragment loads a string called txt into the parser:var parser=new
DOMParser();
var doc=parser.parseFromString(txt,"text/xml");
Example explained:
The first line of the script above creates an empty XML document object.
The second line tells the parser to load a string called txt.
Note: Internet Explorer uses the loadXML() method to parse an XML string, while other browsers
uses the DOMParser object.
12
If you want to use the example above on one of your web pages, the XML files you load must be
located on your own server. Otherwise the xmlDoc.load() method, will generate the error "Access
is denied".
XML DOM
The DOM (Document Object Model) defines a standard way for accessing and manipulating
documents.
The XML DOM
The XML DOM (XML Document Object Model) defines a standard way for accessing and
manipulating XML documents.
The DOM views XML documents as a tree-structure. All elements can be accessed through the
DOM tree. Their content (text and attributes) can be modified or deleted, and new elements can
be created. The elements, their text, and their attributes are all known as nodes.
In the examples below we use the following DOM reference to get the text from the <to> element:
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue
xmlDoc - the XML document created by the parser.
getElementsByTagName("to")[0] - the first <to> element
childNodes[0] - the first child of the <to> element (the text node)
nodeValue - the value of the node (the text itself)
13
xmlDoc.load("note.xml");
document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
}
</script>
</head>
<body onload="parseXML()">
<h1>W3Schools Internal Note</h1>
<p><b>To:</b> <span id="to"></span><br />
<b>From:</b> <span id="from"></span><br />
<b>Message:</b> <span id="message"></span>
</p></body></html>
Output:
Important Note
To extract the text "Jani" from the XML, the syntax is:getElementsByTagName("from")
[0].childNodes[0].nodeValue
In the XML example there is only one <from> tag, but you still have to specify the array index [0],
because the XML parser method getElementsByTagName() returns an array of all <from> nodes.
14
catch(e)
{
alert(e.message);
return;
}
}
document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
}
</script></head>
<body onload="parseXML()">
<h1>W3Schools Internal Note</h1>
<p><b>To:</b> <span id="to"></span><br />
<b>From:</b> <span id="from"></span><br />
<b>Message:</b> <span id="message"></span>
</p>
</body></html>
XML to HTML
This chapter explains how to display XML data as HTML.
Display data as html table:
Example:
<html><body>
<script type="text/javascript">
var xmlDoc=null;
if (window.ActiveXObject)
{// code for IE
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");}
else if (document.implementation.createDocument)
{// code for Mozilla, Firefox, Opera, etc.
xmlDoc=document.implementation.createDocument("","",null);}
else{
alert('Your browser cannot handle this script');}
if (xmlDoc!=null) {
xmlDoc.async=false;
xmlDoc.load("cd_catalog.xml");
var x=xmlDoc.getElementsByTagName("CD");
document.write("<table border='1'>");
document.write("<thead>");
document.write("<tr><th>Artist</th><th>Title</th></tr>");
document.write("</thead>");
document.write("<tfoot>");
document.write("<tr><th colspan='2'>This is my CD collection</th></tr>");
document.write("</tfoot>");
for (var i=0;i<x.length;i++)
{
document.write("<tr>");
document.write("<td>");
document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
document.write("</td>");
document.write("<td>");
document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
15
document.write("</td>");
document.write("</tr>");}
document.write("</table>");}
</script>
</body></html>
SAMPLE OUTPUT:
Artist Title
This is my CD collection
16
In the last chapter, we explained how to parse XML and access the DOM with JavaScript.
In this example, we loop through an XML file (cd_catalog.xml), and display each CD element as
an HTML table row:
<html>
<body>
<script type="text/javascript">
var xmlDoc=null;
if (window.ActiveXObject)
{// code for IE
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
else if (document.implementation.createDocument)
{// code for Mozilla, Firefox, Opera, etc.
xmlDoc=document.implementation.createDocument("","",null);
}
else
{
alert('Your browser cannot handle this script');}
if (xmlDoc!=null){
xmlDoc.async=false;
xmlDoc.load("cd_catalog.xml");
document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("CD");
for (i=0;i<x.length;i++){
document.write("<tr>");
document.write("<td>");
document.write(
x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
document.write("</td>");
document.write("<td>");
document.write(
x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</td>");
document.write("</tr>");}
document.write("</table>");}
</script></body></html>
Example explained
We check the browser, and load the XML using the correct parser
We create an HTML table with <table border="1">
We use getElementsByTagName() to get all XML CD nodes
For each CD node, we display data from ARTIST and TITLE as table data.
We end the table with </table>
For more information about using JavaScript and the XML DOM, visit our XML DOM tutorial.
Access Across Domains
For security reasons, modern browsers does not allow access across domains.
This means, that both the web page and the XML file it tries to load, must be located on the same
server.
The examples on W3Schools all open XML files located on the W3Schools domain.
If you want to use the example above on one of your web pages, the XML files you load must be
located on your own server. Otherwise the xmlDoc.load() method, will generate the error "Access
is denied".
17
The XMLHttpRequest object provides a way to communicate with a server after a web page has
loaded.
What is the XMLHttpRequest Object?
The XMLHttpRequest object is the developer’s dream, because you can:
Update a web page with new data without reloading the page
Request data from a server after the page has loaded
Receive data from a server after the page has loaded
Send data to a server in the background
The XMLHttpRequest object is supported in all modern browsers.
18
True means that the script continues to run after the send() method, without waiting for a
response from the server.
The onreadystatechange event complicates the code. But it is the safest way if you want to
prevent the code from stopping if you don't get a response from the server.
By setting the parameter to "false", your can avoid the extra onreadystatechange code. Use this if
it's not important to execute the rest of the code if the request fails.
XML Application
This chapter demonstrates a small XML application built with HTML and JavaScript
The XML Example Document
Look at the following XML document ("cd_catalog.xml"), that represents a CD catalog:
<?xml version="1.0" encoding="ISO-8859-1"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR> </CD>
Load the XML Document
To load the XML document (cd_catalog.xml), we use the same code as we used in the XML
Parser chapter:var xmlDoc;
if (window.ActiveXObject)
{// code for IE
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
else if (document.implementation.createDocument)
{// code for Firefox, Mozilla, Opera, etc.
xmlDoc=document.implementation.createDocument("","",null);
}
else
{
alert('Your browser cannot handle this script');
}
xmlDoc.async=false;
xmlDoc.load("cd_catalog.xml");
After the execution of this code, xmlDoc is an XML DOM object, accessible by JavaScript.
19
document.write("</td>");
document.write("<td>");
document.write(
x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</td>");
document.write("</tr>");
}
document.write("</table>");
For each CD element in the XML document, a table row is created. Each table row contains two
table data cells with ARTIST and TITLE data from the current CD element.
function display()
{
artist=
(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
title=
(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
year=
(x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue);
txt="Artist: "+artist+"<br />Title: "+title+"<br />Year: "+year;
document.getElementById("show").innerHTML=txt;
}
The body of the HTML document contains an onload eventattribute that will call the display()
function when the page has loaded. It also contains a <div id='show'> element to receive the XML
data.<body onload="display()">
<div id='show'></div>
</body>
With the example above, you will only see data from the first CD element in the XML document.
To navigate to the next line of data, you have to add some more code.
Add a Navigation Script
To add navigation to the example above, create two functions called next() and
previous():function next(){
if (i<x.length-1)
{
i++;
display();
}}
function previous()
{
if (i>0)
{
i--;
display();
}}
20
All Together Now
With a little creativity you can create a full application.
If you use what you have learned on this page, and a little imagination, you can easily develop
this into a full application.
The next() function makes sure that nothing is displayed if you already are at the last CD element,
and the previous () function makes sure that nothing is displayed if you already are at the first CD
element.
The next() and previous() functions are called by clicking next/previous buttons:<input
type="button" onclick="previous()" value="previous" />
<input type="button" onclick="next()" value="next" />
XML ADVANCED
XML Namespaces
XML Namespaces provide a method to avoid element name conflicts.
Name Conflicts
In XML, element names are defined by the developer. This often results in a conflict when trying to mix XML
documents from different XML applications.
This XML carries HTML table information:
<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
In the example above, there will be no conflict because the two <table> elements have different names.
XML Namespaces - The xmlns Attribute
When using prefixes in XML, a so-called namespace for the prefix must be defined.
The namespace is defined by the xmlns attribute in the start tag of an element.
The namespace declaration has the following syntax. xmlns:prefix="URI".
<root>
<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
21
</h:table>
<f:table xmlns:f="http://www.w3schools.com/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
In the example above, the xmlns attribute in the <table> tag give the h: and f: prefixes a qualified
namespace.
When a namespace is defined for an element, all child elements with the same prefix are associated with
the same namespace.
Namespaces can be declared in the elements where they are used or in the XML root element:<root
xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3schools.com/furniture">
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table></root>
Note: The namespace URI is not used by the parser to look up information.
The purpose is to give the namespace a unique name. However, often companies use the namespace as a
pointer to a web page containing namespace information.
Try to go to http://www.w3.org/TR/html4/.
Default Namespaces
Defining a default namespace for an element saves us from using prefixes in all the child elements. It has
the following syntax:xmlns="namespaceURI"
This XML carries HTML table information:
<table xmlns="http://www.w3.org/TR/html4/">
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
22
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr>
<th align="left">Title</th>
<th 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>
XML CDATA
All text in an XML document will be parsed by the parser.
But text inside a CDATA section will be ignored by the parser.
Parsed Character Data (PCDATA) is a term used about text data that will be parsed by the XML parser.
CDATA - (Unparsed) Character Data
The term CDATA is used about text data that should not be parsed by the XML parser.
Characters like "<" and "&" are illegal in XML elements.
"<" will generate an error because the parser interprets it as the start of a new element.
"&" will generate an error because the parser interprets it as the start of an character entity.
Some text, like JavaScript code, contains a lot of "<" or "&" characters. To avoid errors script code can be
defined as CDATA.
Everything inside a CDATA section is ignored by the parser.
A CDATA section starts with "<![CDATA[" and ends with "]]>":<script>
<![CDATA[
function matchwo(a,b)
{
if (a < b && a < 0) then
{
return 1;
}
else
{
return 0;
}}
]]></script>
In the example above, everything inside the CDATA section is ignored by the parser.
23
Notes on CDATA sections:
A CDATA section cannot contain the string "]]>". Nested CDATA sections are not allowed.
The "]]>" that marks the end of the CDATA section cannot contain spaces or line breaks.
XML Encoding
XML documents can contain non ASCII characters, like Norwegian æ ø å , or French ê è é.
To avoid errors, specify the XML encoding, or save XML files as Unicode.
Windows Notepad
Windows Notepad save files as single-byte ANSI (ASCII) by default.
If you select "Save as...", you can specify double-byte Unicode (UTF-16).
Save the XML file below as Unicode (note that the document does not contain any encoding attribute):
<?xml version="1.0"?>
<note>
<from>Jani</from>
<to>Tove</to>
<message>Norwegian: æøå. French: êèé</message></note>
The file above, note_encode_none_u.xml will NOT generate an error. But if you specify a single-byte
encoding it will.
The following encoding (open it), will give an error message:<?xml version="1.0" encoding="windows-
1252"?>
The following encoding (open it), will give an error message:<?xml version="1.0" encoding="ISO-8859-1"?>
The following encoding (open it), will give an error message:<?xml version="1.0" encoding="UTF-8"?>
The following encoding (open it), will NOT give an error:<?xml version="1.0" encoding="UTF-16"?>
Conclusion
Always use the encoding attribute
Use an editor that supports encoding
Make sure you know what encoding the editor uses
Use the same encoding in your encoding attribute
Start Windows Notepad and write the following lines: <?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<from>Jani</from>
<to>Tove</to>
<message>Remember me this weekend</message>
</note>
Save the file on your web server with a proper name like "note.xml".
Generating XML with ASP
XML can be generated on a server without any installed XML software.
24
To generate an XML response from the server - simply write the following code and save it as an ASP file on
the web server:<%
response.ContentType="text/xml"
response.Write("<?xml version='1.0' encoding='ISO-8859-1'?>")
response.Write("<note>")
response.Write("<from>Jani</from>")
response.Write("<to>Tove</to>")
response.Write("<message>Remember me this weekend</message>")
response.Write("</note>")
%>
Note that the content type of the response must be set to "text/xml".
See how the ASP file will be returned from the server.
25
'Load XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("simple.xsl"))
'Transform file
Response.Write(xml.transformNode(xsl))
%>
Example explained
The first block of code creates an instance of the Microsoft XML parser (XMLDOM), and loads the
XML file into memory.
The second block of code creates another instance of the parser and loads the XSL file into
memory.
The last line of code transforms the XML document using the XSL document, and sends the
result as XHTML to your browser. Nice!
See how it works.
Saving XML To a File Using ASP
This ASP example creates a simple XML document and saves it on the server:<%
text="<note>"
text=text & "<to>Tove</to>"
text=text & "<from>Jani</from>"
text=text & "<heading>Reminder</heading>"
text=text & "<body>Don't forget me this weekend!</body>"
text=text & "</note>"
set xmlDoc=Server.CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.loadXML(text)
xmlDoc.Save("test.xml")
%>
26
Change the Value of an Element
The following code changes the text value of the first <title>
element:x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Cooking";
Create an Element
The createElement() method creates a new element node.
The createTextNode() method creates a new text node.
The appendChild() method adds a child node to a node (after the last child).
To create a new element with text content, it is necessary to create both an element node and a
text node.
The following code creates an element (<edition>), and adds it to the first <book>
element:newel=xmlDoc.createElement("edition");
newtext=xmlDoc.createTextNode("First");
newel.appendChild(newtext);
x=xmlDoc.getElementsByTagName("book");
x[0].appendChild(newel);
Example explained:
Create an <edition> element
Create a text node with value = "First"
Append the text node to the <edition> element
Append the <edition> element to the first <book> element
Remove an Element
The removeChild() method removes a specified node (or element).
The following code fragment will remove the first node in the first <book>
element:x=xmlDoc.getElementsByTagName("book")[0];
x.removeChild(x.childNodes[0]);
Note: The result of the example above may be different depending on what browser you use.
Firefox treats new lines as empty text nodes, Internet Explorer don't. You can read more about
this and how to avoid it in the XML DOM tutorial.
XML Don't
Here are some technologies you should try to avoid when using XML.
Internet Explorer - XML Data Islands
What is it? An XML data island is XML data embedded into an HTML page.
Why avoid it? XML Data Islands only works with Internet Explorer browsers.
What to use instead? You should use JavaScript and XML DOM to parse and display XML in
HTML.
XML Data Island Example
This example uses the XML document "cd_catalog.xml".
Bind the XML document to an <xml> tag in the HTML document. The id attribute defines an id for
the data island, and the src attribute points to the XML file:<html>
<body>
<xml id="cdcat" src="cd_catalog.xml"></xml>
<table border="1" datasrc="#cdcat">
27
<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 to the XML data island.
The <span> tags allow the datafld attribute to refer to the XML element to be displayed. In this
case, "ARTIST" and "TITLE". As the XML is read, additional rows are created for each <CD>
element.
28
and manipulate that element.</p>
</span></body></html>
29
A format for syndicating news and the content of news-like sites.
WAP (Wireless Application Protocol)
A XML based language for displaying content on wireless clients, like mobile phones.
SMIL (Synchronized Multimedia Integration Language)
A language for describing audiovisual presentations.
SVG (Scalable Vector Graphics)
Defines graphics in XML format.
30
<wind_dir>West</wind_dir>
<wind_degrees>280</wind_degrees>
<wind_mph>18.4</wind_mph>
<wind_gust_mph>29</wind_gust_mph>
<pressure_mb>1023.6</pressure_mb>
<pressure_in>30.23</pressure_in>
<dewpoint_f>-11</dewpoint_f>
<dewpoint_c>-24</dewpoint_c>
<windchill_f>-7</windchill_f>
<windchill_c>-22</windchill_c>
<visibility_mi>10.00</visibility_mi>
<icon_url_base>
http://weather.gov/weather/images/fcicons/
</icon_url_base>
<icon_url_name>nfew.jpg</icon_url_name>
<two_day_history_url>
http://www.weather.gov/data/obhistory/KJFK.html
</two_day_history_url>
<disclaimer_url>
http://weather.gov/disclaimer.html
</disclaimer_url>
<copyright_url>
http://weather.gov/disclaimer.html
</copyright_url></current_observation>
XML Editors
If you are serious about XML, you will benefit from using a professional XML Editor.
XML is Text-based
However, when you start working with XML, you will soon find that it is better to edit XML
documents using a professional XML editor.
But, if you use Notepad for XML editing, you will soon run into problems.
Notepad does not know that you are writing XML, so it will not be able to assist you.
Why an XML Editor?
31
XML Editors
Professional XML editors will help you to write error-free XML documents, validate your XML
against a DTD or a schema, and force you to stick to a valid XML structure.
An XML editor should be able to:
Add closing tags to your opening tags automatically
Force you to write valid XML
Verify your XML against a DTD
Verify your XML against a Schema
Color code your XML syntax
Altova® XMLSpy®
At W3Schools we have been using XMLSpy for many years. XMLSpy is our favorite XML editor.
These are some of the features we especially like:
Easy to use
Automatic tag completion
Context-sensitive entry helpers
Automatic well-formedness checking
Syntax coloring and pretty printing
Built in DTD and/or XML Schema-based validation
Easy switching between text view and grid view
Built in graphical XML Schema editor
Powerful conversion utilities
Database import and export
Built in templates for many XML document types
Built in XPath 1.0/2.0 analyzer
XSLT 1.0/2.0 editor, profiler, and debugger
XQuery editor, profiler, and debugger
SOAP client and debugger
Graphical WSDL editor
Powerful project management capabilities
Code generation in Java, C++, and C#
Support for Office 2007 / OOXML
XML Summary
XML can be used to exchange, share, and store data.
XML documents form a tree structure that starts at "the root" and branches to "the leaves".
XML has very simple syntax rules. XML with correct syntax is "Well Formed". Valid XML also
validates against a DTD.
XSLT is used to transform XML into other formats like HTML.
All modern browsers have a build-in XML parser that can read and manipulate XML.
The DOM (Document Object Model) defines a standard way for accessing XML.
The XMLHttpRequest object provides a way to communicate with a server after a web page has
loaded.
XML Namespaces provide a method to avoid element name conflicts.
Text inside a CDATA section is ignored by the parser.
Our XML examples also represent a summary of this XML tutorial.
What to Study Next?
Our recommendation is to learn about the XML DOM and XSLT.
If you want to learn more about validating XML, we recommend DTD and XML Schema.
Below is a short description of each subject.
XML DOM (Document Object Model)
The XML DOM defines a standard way for accessing and manipulating XML documents.
The XML DOM is platform and language independent and can be used by any programming
language like Java, JavaScript, and VBScript.
32
XSLT (XML Stylesheet Language Transformations)
XSLT is the style sheet language for XML files.
With XSLT you can transform XML documents into other formats, like XHTML.
XML DTD (Document Type Definition)
The purpose of a DTD is to define what elements, attributes and entities is legal in an XML
document.
With DTD, each of your XML files can carry a description of its own format with it.
DTD can be used to verify that the data you receive, and your own data, is valid.
XML Schema
XML Schema is an XML based alternative to DTD.
Unlike DTD, XML Schemas has support for datatypes, and XML Schema use XML Syntax.
By: DataIntegratedEntity22592
Source: http://w3schools.com/xml/xml_whatis.asp
33