Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

XML Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Introduction to XML

XML was designed to transport and store data.


HTML was designed to display data.

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

XML Document Example


<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

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 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.

• 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.
• 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 Simplifies Data Transport


• XML Simplifies Platform Changes

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 Syntax Rules

• All XML Elements Must Have a Closing Tag


In HTML, some elements do not have to have a closing tag:
In XML, it is illegal to omit the closing tag. All elements must have a closing tag

• XML Tags are Case Sensitive


XML tags are case sensitive. The tag <Letter> is different from the tag <letter>.
Opening and closing tags must be written with the same case:
<Message>This is incorrect</message>
<message>This is correct</message>

• XML Elements Must be Properly Nested

In HTML, you might see improperly nested elements:

<b><i>This text is bold and italic</b></i>

In XML, all elements must be properly nested within each other:

<b><i>This text is bold and italic</i></b>

• 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>

<subchild>.....</subchild>
</child>
</root>

• XML Attribute Values Must be Quoted

2
XML elements can have attributes in name/value pairs just like in HTML.

In XML, the attribute values must always be quoted.

XML Naming Rules


XML elements must follow these naming rules:

• Names can contain letters, numbers, and other characters


• Names cannot start with a number or punctuation character
• Names cannot start with the letters xml (or XML, or Xml, etc)
• Names cannot contain spaces

Any name can be used, no words are reserved.

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.

A DTD can be declared inline (internal) inside an XML document, or as an external


reference.

Internal DTD Declaration

If the DTD is declared inside the XML file, it should be wrapped in a DOCTYPE definition
with the following syntax:

<! DOCTYPE root-element [element-declarations]>

Example XML document with an internal DTD:

<?xml version="1.0" ?>


<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>

External DTD Declaration

If the DTD is declared in an external file, it should be wrapped in a DOCTYPE definition


with the following syntax:

<!DOCTYPE root-element SYSTEM "filename">

-----------

<?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>

And this is the file "note.dtd" which contains the DTD:

<!ELEMENT note (to,from,heading,body)>


<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

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

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 are:

• 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" ?>


<note>
<to>Tove</to>

5
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

Valid XML Documents

A "Valid" XML document is a "Well Formed" XML document, which also conforms to the
rules of a Document Type Definition (DTD):

<?xml version="1.0" encoding="ISO-8859-1"?>


<!DOCTYPE note SYSTEM "Note.dtd">
<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.

XML DOM (DOCUMENT OBJECT MODEL)


The XML DOM defines a standard for accessing and manipulating XML elements.

The DOM is a W3C (World Wide Web Consortium) standard.

The XML DOM is:

• A standard object model for XML


• A standard programming interface for XML
• Platform- and language-independent

The XML DOM defines the objects and properties of all XML elements, and
the methods (interface) to access them.

In the DOM, everything in an XML document is a NODE.

DOM Nodes

• The entire document is a DOCUMENT node


• Every XML element (or tag) is an ELEMENT node
• The text in the XML elements are TEXT nodes
• Every attribute is an ATTRIBUTE node
• Comments are COMMENT 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 root node <bookstore> holds two <book> nodes.

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 Node Tree

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).

• In a node tree, the top node is called the root


• Every node, except the root, has exactly one parent node
• A node can have any number of children
• A leaf is a node with no children
• Siblings are nodes with the same parent

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:

• defines elements and attributes that can appear in a document


• defines which elements are child elements, the number and order of child elements
• defines whether an element is empty or can include text
• defines data types for elements and attributes
• defines default and fixed values for elements and attributes

XML Schema Example


<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<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>

XML Schemas are the Successors of DTDs


8
XML Schemas will be used in most Web applications as a replacement for DTDs. Here are
some reasons:

• XML Schemas are extensible to future additions


• XML Schemas are richer and more powerful than DTDs
• XML Schemas are written in XML
• XML Schemas support data types
• XML Schemas support namespaces
• XML Schema is a W3C Recommendation

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.

Connecting a Schema in an XML document


<?xml version="1.0"?>
<note xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

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.

Binding a Single-Valued Element to Data at Design Time

Given a text box, for example, a page author can bind that element to data as follows:

<INPUT TYPE=”TEXT” DATASRC="#dsoComposers" DATAFLD="compsr_last">

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.

Binding a TABLE to Data (Tabular data binding)


Because the table element is a tabular data consumer, it relies on the elements that it contains
to bind to the individual fields in the data set provided by the DSO. The contained elements
serve as a template, and they are repeated once for each record in the data set. The table
specifies the dataSrc attribute. The contained elements specify the dataFld attribute and
inherit the dataSrc from the table.

<?xml version="1.0" ?>

<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

You might also like