XML Notes
XML Notes
XML Notes
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 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.
• 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.
• XML is a software- and hardware-independent tool for carrying information.
• XML is a W3C Recommendation
XML became a W3C Recommendation on February 10, 1998.
• XML is Everywhere
1
• XML Simplifies Data Sharing
In the real world, computer systems and databases contain data in incompatible formats.
XML data is stored in plain text format. This provides a software- and hardware-independent
way of storing data.
XML data is stored in text format. This makes it easier to expand or upgrade to new operating
systems, new applications, or new browsers, without losing data.
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>
<subchild>.....</subchild>
</child>
</root>
2
XML elements can have attributes in name/value pairs just like in HTML.
3
XML DTD (Document Type Definition)
Introduction
A Document Type Definition (DTD) defines the legal building blocks of an XML document.
It defines the document structure with a list of legal elements and attributes.
If the DTD is declared inside the XML file, it should be wrapped in a DOCTYPE definition
with the following syntax:
-----------
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
4
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Uses of DTD
With a DTD, each of your XML files can carry a description of its own format.
With a DTD, independent groups of people can agree to use a standard DTD for
interchanging data.
Your application can use a standard DTD to verify that the data you receive from the outside
world is valid.
XML Validation
5
<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 also conforms to the
rules of a Document Type Definition (DTD):
The DOCTYPE declaration in the example above is a reference to an external DTD file.
The XML DOM defines the objects and properties of all XML elements, and
the methods (interface) to access them.
DOM Nodes
Example
6
<?xml version="1.0" ?>
<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>
</bookstore>
The root node in the XML above is named <bookstore>. All other nodes in the document are
contained within <bookstore>.
The first <book> node holds four nodes: <title>, <author>, <year>, and <price>, which contains
one text node each, "Everyday Italian", "Giada De Laurentiis", "2005", and "30.00".
The XML DOM views an XML document as a tree-structure. The tree structure is called a node-
tree.
All nodes can be accessed through the tree. Their contents can be modified or deleted, and new
elements can be created.
The node tree shows the set of nodes, and the connections between them. The tree starts at the
root node and branches out to the text nodes at the lowest level of the tree:
7
Node Parents, Children, and Siblings
The nodes in the node tree have a hierarchical relationship to each other.
The terms parent, child, and sibling are used to describe the relationships. Parent nodes have
children. Children on the same level are called siblings (brothers or sisters).
XML Schema
An XML schema describes the structure of an XML document. XML Schema is an XML-
based alternative to DTD.
The XML Schema language is also referred to as XML Schema Definition (XSD).
The purpose of an XML Schema is to define the legal building blocks of an XML document,
just like a DTD.
An 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>
</xs:schema>
Example
The above example is an XML Schema definition file called "note.xsd" that defines the
elements of the XML document below ("note.xml"):
The note element is a complex type because it contains other elements. The other elements
(to, from, heading, body) are simple types because they do not contain other elements.
9
Binding a Single-Valued Element to Data (Singular data
binding)
The procedure for binding a single-valued element to data is the same regardless of the
element. Elements can be bound to data at design time using the DATASRC and DATAFLD
attributes.
Given a text box, for example, a page author can bind that element to data as follows:
The dataSrc attribute in this example specifies the ID, prefixed by a hash mark (#), of a DSO
embedded on the page. The hash mark is required. The dataFld attribute identifies the field
in the data provided by the DSO to which the text box should be bound.
<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>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
</CATALOG>
10
<html>
<body>
<xml id="emp" src="salary.xml"></xml>
<table border="1" datasrc="#emp">
<tr>
<td><span datafld="EMPID"></span></td>
<td><span datafld="NAME"></span></td>
<td><span datafld="DEPT"></span></td>
<td><span datafld="DESG"></span></td>
<td><span datafld="SALARY"></span></td>
</tr>
</table>
</body>
</html>
11