Module 5 Notes
Module 5 Notes
MODULE 5
XML
By
Prof. Likhith S R
Assistant. Professor
Dept. of CSE(DS), NCET
Module - V
XML: Introduction, Syntax, Document Type Definitions, Namespaces, XML Schemas,
Displaying Raw XML Documents, Displaying XML Documents with CSS.
What is xml
• Xml (eXtensible Markup Language) is a mark up language.
• XML is designed to store and transport data.
• Xml was released in late 90’s. it was created to provide an easy to use and store self
describing data.
• XML became a W3C Recommendation on February 10, 1998.
• XML is not a replacement for HTML.
• XML is designed to be self-descriptive.
• XML is designed to carry data, not to display data.
• XML tags are not predefined. You must define your own tags.
• XML is platform independent and language independent.
XML Does Not DO Anything
• Maybe it is a little hard to understand, but XML does not DO anything.
• This note is a note to Tove from Jani, stored as XML:
The XML above is quite self-descriptive:
• It has sender information
• It has receiver information
• It has a heading
• It has a message body
But still, the XML above does not DO anything. XML is just information wrapped in tags.
Someone must write a piece of software to send, receive, store, or display it:
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
5) HTML has its own predefined tags. You can define tags according to your
need.
• 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.
• This makes it much easier to create data that can be shared by different applications.
3) XML simplifies data transport
• One of the most time-consuming challenges for developers is to exchange data
between incompatible systems over the Internet.
• Exchanging data as XML greatly reduces this complexity, since the data can be read
by different incompatible applications.
4) XML simplifies Platform change
• Upgrading to new systems (hardware or software platforms), is always time
consuming. Large amounts of data must be converted and incompatible data is often
lost.
• 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.
5) XML increases data availability
• Different applications can access your data, not only in HTML pages, but also from
XML data sources.
• With XML, your data can be available to all kinds of "reading machines" (Handheld
computers, voice machines, news feeds, etc), and make it more available for blind
people, or people with other disabilities.
6) XML can be used to create new internet languages
A lot of new Internet languages are created with XML.
Here are some examples:
• XHTML
• WSDL for describing available web services
• WAP and WML as markup languages for handheld devices
• RSS languages for news feeds
• RDF and OWL for describing resources and ontology
• SMIL for describing multimedia for the web
Parameters of
XML XHTML
Comparison
1. XML is considered to be the most useful and popular Markup language, whereas
XHTML is less popular or less important compared to XML on basis of the usage.
2. XML refers to a simple text-based format that is used for representing structured
information like data, transactions, configuration, documents, invoices, books, and
so on. On the other hand, XHTML refers to a cross between XML and HTML that is
used for transiting from HTML to XML.
3. XML was first published in 1998 but XHTML was initially released in 2000.
4. XML has a hierarchical tree-shaped structure which is called XML tree. XHTML is
developed based on three main components- declaration, head, and body.
5. XML is composed of Unicode. XHTML comprises three versions- XHTML
Transitional, XHTML 1.0 Frameset, and XHTML 1.0 Strict.
XML Syntax and Rules
• The syntax rules of XML are very simple and logical. The rules are easy to learn,
and easy to use.
• XML Documents Must Have a Root Element
• XML documents must contain one root element that is the parent of all other
elements:
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
The XML Prolog
• This line is called the XML prolog: <?xml version="1.0" encoding="UTF-8"?>
• The XML prolog is optional. If it exists, it must come first in the document.
• XML documents can contain international characters, like Norwegian øæå or French
êèé.
• To avoid errors, you should specify the encoding used, or save your XML files as
UTF-8.
• UTF-8 is the default character encoding for XML documents.
<p>This is a paragraph.</p>
<br />
<message>This is correct</message>
• "Opening and closing tags" are often referred to as "Start and end tags". Use
whatever you prefer. It is exactly the same thing.
XML Elements Must be Properly Nested
• In HTML, you might see improperly nested elements:
• 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 Tree Structure
• An XML document has a self descriptive structure.
• It forms a tree structure which is referred as an XML tree.
• The tree structure makes easy to describe an XML document.
• A tree structure contains root element (as parent), child element and so on.
• It is very easy to traverse all succeeding branches and sub-branches and leaf nodes
starting from the root.
Dept. of CSE(Data Science), NCET 7 2021-22
Web Programming(20CSI43) Module-5
<?xml version="1.0"?>
<college>
<student>
<firstname>Tamanna</firstname>
<lastname>Bhatia</lastname>
<contact>09990449935</contact>
<email>tammanabhatia@abc.com</e mail>
<address>
<city>Ghaziabad</city>
<state>Uttar Pradesh</state>
<pin>201007</pin>
</address>
</student>
</college>
• In the above example, first line is the XML declaration. It defines the XML version
1.0. Next line shows the root element (college) of the document. Inside that there is
one more element (student). Student element contains five branches named
<firstname>, <lastname>, <contact>, <Email> and <address>.
• <address> branch contains 3 sub-branches named <city>, <state> and <pin>.
• The purpose of a DTD is to define the structure and the legal elements and attributes
of an XML document:
The DTD above is interpreted like this:
• !DOCTYPE note - Defines that the root element of the document is note
• !ELEMENT note - Defines that the note element must contain the elements: "to,
from, heading, body"
• !ELEMENT to - Defines the to element to be of type "#PCDATA"
• !ELEMENT from - Defines the from element to be of type "#PCDATA"
• !ELEMENT heading - Defines the heading element to be of type "#PCDATA"
• !ELEMENT body - Defines the body element to be of type "#PCDATA"
Note.dtd:
<!DOCTYPE note
[
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
Ex2:
<?xml version = "1.0" encoding = "UTF-8" standalone = "no" ?>
<!DOCTYPE address SYSTEM "address.dtd">
<address>
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
</address>
DTD
<!ELEMENT address (name,company,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
XML Schema
• An XML Schema describes the structure of an XML document, just like a DTD.
• An XML document with correct syntax is called "Well Formed".
• An XML document validated against an XML Schema is both "Well Formed" and
"Valid".
• XML Schema is an XML-based alternative to DTD:
<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>
String data types are used for values that contains character strings.
The string data type can contain characters, line feeds, carriage returns, and tab
characters.
<customer>John Smith</customer>
The integer data type is used to specify a numeric value without a fractional component.
<price>999</price>
<price>999.50</price>
Boolean
The xsd:boolean data type accepts two values, true and false, but depending on whether it
is on input or output data, these values representatio ns can change.
On input (on queries), boolean values can be:
XML Namespaces
• XML Namespaces provide a method to avoid element 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>
<table>
<name>African Coffee Table</name>
<width>80</width>
<length>120</length>
</table>
• If these XML fragments were added together, there would be a name conflict. Both
contain a <table> element, but the elements have different content and meaning.
• A user or an XML application will not know how to handle these differences.
Solving the Name Conflict Using a Prefix
• Name conflicts in XML can easily be avoided using a name prefix.
• This XML carries information about an HTML table, and a piece of furniture
• In the example above, there will be no conflict because the two <table> elements
have different names.
<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>
<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table xmlns:f="https://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 first <table> element gives the h: prefix a qualified
namespace.
• The xmlns attribute in the second <table> element gives the f: prefix 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 also be declared in the XML root element:
<root xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="https://www.w3schools.com/furniture">
</root>
• Note: The namespace URI is not used by the parser to look up information.
• The purpose of using an URI is to give the namespace a unique name.
• However, companies often use the namespace as a pointer to a web page containing
namespace information.
XML Example
• XML documents create a hierarchical structure looks like a tree so it is known as
XML Tree that starts at "the root" and branches to "the leaves".
Example of XML Document
• XML documents uses a self-describing and simple syntax:
<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).
Ex1
<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>
<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 elements in the document are
contained within <bookstore>.
• The <book> element has 4 children: <title>,< author>, <year> and <price>.
Displaying XML
• Raw XML files can be viewed in all major browsers.
• Don't expect XML files to be displayed as HTML pages.
Viewing XML Files
<?xml version="1.0" encoding="UTF-8"?>
- <note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
• Most browsers will display an XML document with color-coded elements.
• Often a plus (+) or minus sign (-) to the left of the elements can be clicked to expand
or collapse the element structure.
• To view raw XML source, try to select "View Page Source" or "View Source" from
the browser menu.
Viewing an Invalid XML File
• If an erroneous XML file is opened, some browsers will report the error, and some
will display it, or display it incorrectly.
CSS in XML
Purpose of CSS in XML
• CSS (Cascading Style Sheets) can be used to add style and display information to an
XML document. It can format the whole XML document.
How to link XML file with CSS
• To link XML files with CSS, you should use the following syntax:
cssemployee.css
employee
{
background-color: pink;
}
firstname,lastname,email
{
font-size:25px;
display:block;
color: blue;
margin- left: 50px;
}
employee.dtd
<!ELEMENT employee (firstname,lastname,email)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT email (#PCDATA)>
employee.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="cssemployee.css"?>
<!DOCTYPE employee SYSTEM "employee.dtd">
<employee>
<firstname>vimal</firstname>
<lastname>jaiswal</lastname>
<email>vimal@javatpoint.com</email>
</employee>
Ex2
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="Rule.css"?>
<books>
<heading>Welcome To GeeksforGeeks </heading>
<book>
<title>Title -: Web Programming</title>
<author>Author -: Chrisbates</author>
<publisher>Publisher -: Wiley</publis her>
<edition>Edition -: 3</edition>
<price> Price -: 300</price>
</book>
<book>
<title>Title -: Internet world-wide-web</title>
<author>Author -: Ditel</author>
<publisher>Publisher -: Pearson</publisher>
<edition>Edition -: 3</edition>
<price>Price -: 400</price>
</book>
<book>
<title>Title -: Computer Networks</title>
<author>Author -: Foruouzan</author>
<publisher>Publisher -: Mc Graw Hill</publisher>
<edition>Edition -: 5</edition>
<price>Price -: 700</price>
</book>
<book>
<title>Title -: DBMS Concepts</title>
<author>Author -: Navath</author>
<publisher>Publisher -: Oxford</publisher>
<edition>Edition -: 5</edition>
<price>Price -: 600</price>
</book>
<book>
<title>Title -: Linux Programming</title>
<author>Author -: Subhitab Das</author>
<publisher>Publisher -: Oxford</publisher>
<edition>Edition -: 8</edition>
<price>Price -: 300</price>
</book>
</books>
Rule.css
books {
color: white;
background-color : gray;
width: 100%;
}
heading {
color: green;
font-size : 40px;
background-color : powderblue;
}
heading, title, author, publisher, edition, price {
display : block;
}
title {
font-size : 25px;
font-weight : bold;
}
• You need two files: The XML file, and a CSS file. In your XML document, you
need to add one line of code. This one line of code tells the processor to display the
XML using styles from the external style sheet.
1. Create an XML file with the following content and save it:
tutorials {
margin:10px;
background-color:#ccff00;
font-family:verdana,helvetica,sans-serif;
}
name {
display:block;
font-weight:bold;
}
url {
display:block;
color:#636363;
font-size:small;
font-style:italic;
}
Ex3
<car_catalog>
<car>
<year> 1997 </year>
<make> &c; </make>
<model> Impala </model>
<color> Light blue </color>
<engine>
<number_of_cylinders> 8 cylinder </number_of_cylinders>
<fuel_system> multi-port fuel injected </fuel_system>
</engine>
<number_of_doors> 4 door </number_of_doors>
<transmission_type> 4 speed automatic </transmission_type>
<accessories radio = "yes" air_conditioning = "yes" power_windows = "yes"
power_steering = "yes" power_brakes = "yes" />
</car>
<car_catalog>
DTD
<!ELEMENT car_catalog >
<!ELEMENT car (year, make, model, color, engine, number_of_doors, transmission_type,
accessories)>
<!ELEMENT make (#PCDATA)>
<!ELEMENT model (#PCDATA)>
<!ELEMENT year (#PCDATA)>
The same XML data can be used in many different presentation scenarios.
Because of this, with XML, there is a full separation between data and presentation.
In many HTML applications, XML is used to store or transport data, while HTML is
used to format and display the same data.
When displaying data in HTML, you should not have to edit the HTML file when
the data changes.
With a few lines of JavaScript code, you can read an XML file and update the data
content of any HTML page.