0% found this document useful (0 votes)
6 views43 pages

Element of Commercial Portal Unit II

XML, or eXtensible Markup Language, is a versatile markup language designed for storing and transporting data in a self-descriptive manner, independent of hardware and software. It allows users to define their own tags and is widely used for data sharing and transport between incompatible systems. XML simplifies data management by separating content from presentation, making it easier to upgrade systems without data loss and ensuring accessibility across various platforms and devices.

Uploaded by

cocsit21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views43 pages

Element of Commercial Portal Unit II

XML, or eXtensible Markup Language, is a versatile markup language designed for storing and transporting data in a self-descriptive manner, independent of hardware and software. It allows users to define their own tags and is widely used for data sharing and transport between incompatible systems. XML simplifies data management by separating content from presentation, making it easier to upgrade systems without data loss and ensuring accessibility across various platforms and devices.

Uploaded by

cocsit21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Concept of XML

XML is a software- and hardware-independent tool for storing and transporting data.

What is XML?

 XML stands for eXtensible Markup Language


 XML is a markup language much like HTML
 XML was designed to store and transport data
 XML was designed to be self-descriptive
 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 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.

Why xml
Platform Independent and Language Independent: The main benefit of xml is that you can use it
to take data from a program like Microsoft SQL, convert it into XML then share that XML with
other programs and platforms. You can communicate between two platforms which are generally
very difficult.

The main thing which makes XML truly powerful is its international acceptance. Many
corporation use XML interfaces for databases, programming, office application mobile phones
and more. It is due to its platform independent feature.

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:

<?xml version="1.0" encoding="UTF-8"?>


<note>
<to>Tove</to>
<from>Jani</from>

Mr. Kadarkar B.M. Unit I- Element of Commercial portal 1


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

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

From: Jani

Reminder
Don't forget me this weekend!

The Difference Between XML and HTML

XML and HTML were designed with different goals:

 XML was designed to carry data - with focus on what data is


 HTML was designed to display data - with focus on how data looks
 XML tags are not predefined like HTML tags are

XML Does Not Use Predefined Tags

The XML language has no predefined 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.

HTML works with predefined tags like <p>, <h1>, <table>, etc.

With XML, the author must define both the tags and the document structure.

XML Simplifies Things

 XML simplifies data sharing

Mr. Kadarkar B.M. Unit I- Element of Commercial portal 2


 XML simplifies data transport
 XML simplifies platform changes
 XML simplifies data availability

Many computer systems contain data in incompatible formats. Exchanging data between
incompatible systems (or upgraded systems) is a time-consuming task for web developers. Large
amounts of data must be converted, and incompatible data is often lost.

XML stores data in plain text format. This provides a software- and hardware-independent
way of storing, transporting, and sharing data.

XML also makes it easier to expand or upgrade to new operating systems, new applications, or
new browsers, without losing data.

With XML, data can be available to all kinds of "reading machines" like people, computers,
voice machines, news feeds, etc.

XML Example 1
<?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>

Output

Note
To: Tove

From: Jani

Heading: Reminder

Body: Don't forget me this weekend!

XML Example 2
<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>

Mr. Kadarkar B.M. Unit I- Element of Commercial portal 3


Two of our famous Belgian Waffles with plenty of real maple syrup
</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>
Light Belgian waffles covered with strawberries and whipped cream
</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>
Belgian waffles covered with assorted fresh berries and whipped cream
</description>
<calories>900</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>
Thick slices made from our homemade sourdough bread
</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>
Two eggs, bacon or sausage, toast, and our ever-popular hash browns
</description>
<calories>950</calories>
</food>
</breakfast_menu>

Features and Advantages of XML


XML is widely used in the era of web development. It is also used to simplify data storage and
data sharing.

Mr. Kadarkar B.M. Unit I- Element of Commercial portal 4


The main features or advantages of XML are given below.

1) XML separates data from HTML


If you need to display dynamic data in your HTML document, it will take a lot of work to edit
the HTML each time the data changes.

With XML, data can be stored in separate XML files. This way you can focus on using
HTML/CSS for display and layout, and be sure that changes in the underlying data will not
require any changes to the HTML.

With a few lines of JavaScript code, you can read an external XML file and update the data
content of your web page.

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

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.

Mr. Kadarkar B.M. Unit I- Element of Commercial portal 5


6) XML can be used to create new internet languages
A lot of new Internet languages are created with XML.

Here are some examples:

o XHTML
o WSDL for describing available web services
o WAP and WML as markup languages for handheld devices
o RSS languages for news feeds
o RDF and OWL for describing resources and ontology
o SMIL for describing multimedia for the web

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:

<?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>
Mr. Kadarkar B.M. Unit I- Element of Commercial portal 6
<body>Don't forget me this weekend!</body>

And finally the last line defines the end of the root element.

</note>

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

XML Element
An XML element is everything from (including) the element's start tag to (including) the
element's end tag.

<price>29.99</price>

An element can contain:

 text
 attributes

Mr. Kadarkar B.M. Unit I- Element of Commercial portal 7


 other elements
 or a mix of the above

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

In the example above:

<title>, <author>, <year>, and <price> have text content because they contain text (like 29.99).

<bookstore> and <book> have element contents, because they contain elements.

<book> has an attribute (category="children").

Empty XML Elements


An element with no content is said to be empty.

In XML, you can indicate an empty element like this:

<element></element>

You can also use a so called self-closing tag:

<element />

The two forms produce identical results in XML software (Readers, Parsers, Browsers).

Empty elements can have attributes.

XML Naming Rules


XML elements must follow these naming rules:

Mr. Kadarkar B.M. Unit I- Element of Commercial portal 8


 Element names are case-sensitive
 Element names must start with a letter or underscore
 Element names cannot start with the letters xml (or XML, or Xml, etc)
 Element names can contain letters, digits, hyphens, underscores, and periods
 Element names cannot contain spaces

Any name can be used, no words are reserved (except xml).

Best Naming Practices


Create descriptive names, like this: <person>, <firstname>, <lastname>.

Create short and simple names, like this: <book_title> not like this: <the_title_of_the_book>.

Avoid "-". If you name something "first-name", some software may think you want to subtract
"name" from "first".

Avoid ".". If you name something "first.name", some software may think that "name" is a
property of the object "first".

Avoid ":". Colons are reserved for namespaces (more later).

Non-English letters like éòá are perfectly legal in XML, but watch out for problems if your
software doesn't support them!

Naming Conventions
Some commonly used naming conventions for XML elements:

Style Example Description

Lower <firstname> All letters lower case


case

Upper <FIRSTNAME> All letters upper case


case

Snake <first_name> Underscore separates words (commonly used in SQL


case databases)

Pascal <FirstName> Uppercase first letter in each word (commonly used by C


case programmers)

Camel <firstName> Uppercase first letter in each word except the first
case (commonly used in JavaScript)

Mr. Kadarkar B.M. Unit I- Element of Commercial portal 9


Tip! Choose your naming style, and be consistent about it!

XML documents often have a corresponding database. A common practice is to use the naming
rules of the database for the XML elements.

XML Attributes
XML elements can have attributes, just like HTML.

Attributes are designed to contain data related to a specific element.

XML Attributes Must be Quoted


Attribute values must always be quoted. Either single or double quotes can be used.

For a person's gender, the <person> element can be written like this:

<person gender="female">

or like this:

<person gender='female'>

If the attribute value itself contains double quotes you can use single quotes, like in this example:

<gangster name='George "Shotgun" Ziegler'>

or you can use character entities:

<gangster name="George &quot;Shotgun&quot; Ziegler">

XML Elements vs. Attributes


Take a look at these two examples:

<person gender="female">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>

<person>
<gender>female</gender>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


10
In the first example, gender is an attribute. In the last example, gender is an element. Both
examples provide the same information.

There are no rules about when to use attributes or when to use elements in XML.

Avoid XML Attributes?


Some things to consider when using attributes are:

 attributes cannot contain multiple values (elements can)


 attributes cannot contain tree structures (elements can)
 attributes are not easily expandable (for future changes)

Don't end up like this:

<note day="10" month="01" year="2008"


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

XML Attributes for Metadata


Sometimes ID references are assigned to elements. These IDs can be used to identify XML
elements in much the same way as the id attribute in HTML. This example demonstrates this:

<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 attributes above are for identifying 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
the data itself should be stored as elements.

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


11
XML with CSS

XML stands for Extensible Markup Language. It is also a markup language designed especially
for web documents. It defines a set of rules for encoding documents in a format that is both
human-readable and machine-readable. It allows developers to create custom tags. XML also
enables the definition, transmission, validation, and interpretation of data between applications.

How to display XML using CSS

We can use CSS properties to style the content present in the XML document. Following are the
steps to display XML using CSS −

 Step-1 − Create a .xml file and add your code to it.


 Step-2 − Create a .css file and add stylings for the tags specified in the .xml file.

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


12
 Step-3 − Link the .css file to the .xml file using the following code block. This should be
included in the .xml document.

<?xml-stylesheet type="text/css" href="name_of_css_file.css"?>

 Step-4 − Place both files in the same folder.


 Step-5 − Open the .xml file in any browser to view the CSS styling applied to .xml
elements.

Example

In the following example, we are creating an XML file that contains information about cricket
player statistics and displaying the XML file using CSS.

<?xml version="1.0"?>

<style>

Cricket {

display: block;

margin-bottom: 30px;

name {

font-weight: bold;

color: seagreen;

hundreds, fifties {

color: lightslategray;

</style>

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


13
<Sports>

<Cricket>

<name>Virat Kohli</name>

<hundreds>75</hundreds>

<fifties>130</fifties>

</Cricket>

<Cricket>

<name>Joe Root</name>

<hundreds>46</hundreds>

<fifties>99</fifties>

</Cricket>

<Cricket>

<name>Steve Smith</name>

<hundreds>44</hundreds>

<fifties>70</fifties>

</Cricket>

<Cricket>

<name>Faf du Plessis</name>

<hundreds>23</hundreds>

<fifties>66</fifties>

</Cricket>

</Sports>

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


14
Execute the "data.xml" file in any browser to view the CSS styling applied to elements present in
the XML file.

Output
Virat Kohli 75 130

Joe Root 46 99

Steve Smith 44 70

Faf du Plessis 23 66

Example

Following is another example to display XML file using CSS −

Save the following file as "data.xml" –

data.xml

<?xml version="1.0" encoding="UTF-8"?>

<?xml-stylesheet type="text/css" href="style.css"?>

<root>

<person>

<name>Maya</name>

<age>30</age>

<gender>Female</gender>

</person>

<person>

<name>Ram</name>

<age>25</age>

<gender>Male</gender>

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


15
</person>

<person>

<name>Varun</name>

<age>25</age>

<gender>Male</gender>

</person>

<person>

<name>Sarah</name>

<age>25</age>

<gender>Female</gender>

</person>

</root>

CSS File

Save the following file as "style.css" −

root {

display: block;

font-family: Arial, sans-serif;

font-size: 14px;

margin-bottom: 20px;

person {
Mr. Kadarkar B.M. Unit I- Element of Commercial portal
16
display: block;

width: 10%;

display: block;

margin-bottom: 20px;

border: 1px solid black;

padding: 10px;

border-radius: 5px;

name {

font-weight: bold;

color: #333;

age {

color: #999;

gender{

color: brown;

font-weight: bolder;

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


17
XML with DSO

XML Data Source Object

XML Data Source Object is a Microsoft Active X object built into the web browser so we can
use ActiveX control to extract data from the XML code embedded directly in the HTML file and
the external XML file using data binding into HTML web pages.

Data islands

 XML code is embedded in an HTML document to create data islands.


 The file should be saved with .html or .htm extension.

Data islands are created with two methods:

1. Explicit method: Embedding data directly.

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


18
Syntax:
<XML ID=“xmlID”>
<!-- XML data-->
</XML>

2. Implicit method: Embedding XML data with reference to external XML file.

Syntax:
<XML ID=“xmlID” SRC=“filename.xml”></XML>

The DSO object is implicitly created when we use a XML data island.

 To extract data from external data file use the HTML tags.
<IMG>, <LABLE>, <TABLE>

 Two attributes are very important along with html tags.


a) DATASRC: Specifies the source of data.
b) DATAFLD: Specifies the field from where the data is to be displayed.

Example : Write a program to display content in table using XML-DSO

<html>
<head>
</head>
<body>
<xml id="xml">
<table datasrc="#xml">
<thead>
<th>Name</th>
<th>gender</th>
</thead>
<tr>
<td><div datafld="name"></div></td>
<td><div datafld="gender"></div></td>
</tr>
</table>
<db>
<member>
<name>BOB</name>
<gender>male</gender>
Mr. Kadarkar B.M. Unit I- Element of Commercial portal
19
</member>
</db>
</xml>
</body>
</html>

Output:
Name gender

Bob Male

XML-DSO object is used to load external XML document.

The steps to do this are as follows:

Create and initialize XML-DSO object.

Syntax:
<OBJECT ID=“SomeID CLASSID=CLSID”></OBJECT>

 Load the external XML file in HTML page using the XMLDSO. We have to use Javascript in
the <HEAD> section of HTML page.
 Extract data from loaded XML file through HTML tags using DATASRC and DATAFLD.

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>
Mr. Kadarkar B.M. Unit I- Element of Commercial portal
20
This XML carries information about a table (a piece of furniture):

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

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

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 namespace for the prefix must be defined.

The namespace can be defined by an xmlns attribute in the start tag of an element.

The namespace declaration has the following syntax. xmlns:prefix="URI".

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


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

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

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


22
</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 DTD
An XML document with correct syntax is called "Well Formed".

An XML document validated against a DTD is both "Well Formed" and "Valid".

What is a DTD?
DTD stands for Document Type Definition.

A DTD defines the structure and the legal elements and attributes of an XML document.

When to Use a DTD?


With a DTD, independent groups of people can agree to use a standard DTD for interchanging
data.

With a DTD, you can verify that the data you receive from the outside world is valid.

You can also use a DTD to verify your own data.

When NOT to Use a DTD?


XML does not require a DTD.

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


23
When you are experimenting with XML, or when you are working with small XML files,
creating DTDs may be a waste of time.

If you develop applications, wait until the specification is stable before you add a DTD.
Otherwise, your software might stop working because of validation errors.

Valid XML Documents


A "Valid" XML document is "Well Formed", as well as it conforms to the rules of a DTD:

<?xml version="1.0" encoding="UTF-8"?>


<!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 above contains a reference to a DTD file. The content of the DTD
file is shown and explained below.

An Internal DTD Declaration


If the DTD is declared inside the XML file, it must be wrapped inside the <!DOCTYPE>
definition:

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>

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


24
In the XML file, select "view source" to view the DTD.

The DTD above is interpreted like this:

 !DOCTYPE note defines that the root element of this document is note
 !ELEMENT note defines that the note element must contain four 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"

An External DTD Declaration


If the DTD is declared in an external file, the <!DOCTYPE> definition must contain a reference
to the DTD file:

XML document with a reference to an external DTD


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

And here 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)>

DTD - XML Building Blocks


The main building blocks of both XML and HTML documents are elements.

The Building Blocks of XML Documents


Seen from a DTD point of view, all XML documents are made up by the following building
blocks:

 Elements
Mr. Kadarkar B.M. Unit I- Element of Commercial portal
25
 Attributes
 Entities
 PCDATA
 CDATA

Elements
Elements are the main building blocks of both XML and HTML documents.

Examples of HTML elements are "body" and "table". Examples of XML elements could be
"note" and "message". Elements can contain text, other elements, or be empty. Examples of
empty HTML elements are "hr", "br" and "img".

Examples:

<body>some text</body>

<message>some text</message>

Attributes
Attributes provide extra information about elements.

Attributes are always placed inside the opening tag of an element. Attributes always come in
name/value pairs. The following "img" element has additional information about a source file:

<img src="computer.gif" />

The name of the element is "img". The name of the attribute is "src". The value of the attribute is
"computer.gif". Since the element itself is empty it is closed by a " /".

Entities
Some characters have a special meaning in XML, like the less than sign (<) that defines the start
of an XML tag.

Most of you know the HTML entity: "&nbsp;". This "no-breaking-space" entity is used in
HTML to insert an extra space in a document. Entities are expanded when a document is parsed
by an XML parser.

The following entities are predefined in XML:

Entity References Character

&lt; <

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


26
&gt; >

&amp; &

&quot; "

&apos; '

PCDATA
PCDATA means parsed character data.

Think of character data as the text found between the start tag and the end tag of an XML
element.

PCDATA is text that WILL be parsed by a parser. The text will be examined by the parser
for entities and markup.

Tags inside the text will be treated as markup and entities will be expanded.

However, parsed character data should not contain any &, <, or > characters; these need to be
represented by the &amp; &lt; and &gt; entities, respectively.

CDATA
CDATA means character data.

CDATA is text that will NOT be parsed by a parser. Tags inside the text will NOT be treated
as markup and entities will not be expanded.

XML Schema
An XML Schema describes the structure of an XML document.

The XML Schema language is also referred to as XML Schema Definition (XSD).

XSD Example
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
<xs:complexType>

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


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

The purpose of an XML Schema is to define the legal building blocks of an XML document:

 the elements and attributes that can appear in a document


 the number of (and order of) child elements
 data types for elements and attributes
 default and fixed values for elements and attributes

Why Learn XML Schema?


In the XML world, hundreds of standardized XML formats are in daily use.

Many of these XML standards are defined by XML Schemas.

XML Schema is an XML-based (and more powerful) alternative to DTD.

XML Schemas Support Data Types


One of the greatest strength of XML Schemas is the support for data types.

 It is easier to describe allowable document content


 It is easier to validate the correctness of data
 It is easier to define data facets (restrictions on data)
 It is easier to define data patterns (data formats)
 It is easier to convert data between different data types

XML Schemas use XML Syntax


Another great strength about XML Schemas is that they are written in XML.

 You don't have to learn a new language


 You can use your XML editor to edit your Schema files
 You can use your XML parser to parse your Schema files
 You can manipulate your Schema with the XML DOM
 You can transform your Schema with XSLT

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


28
XML Schemas are extensible, because they are written in XML.

With an extensible Schema definition you can:

 Reuse your Schema in other Schemas


 Create your own data types derived from the standard types
 Reference multiple schemas in the same document

Well-Formed is Not Enough


A well-formed XML document is a document that conforms to the XML syntax rules, like:

 it must begin with the XML declaration


 it must have one unique root element
 start-tags must have matching end-tags
 elements are case sensitive
 all elements must be closed
 all elements must be properly nested
 all attribute values must be quoted
 entities must be used for special characters

Even if documents are well-formed they can still contain errors, and those errors can have
serious consequences.

Think of the following situation: you order 5 gross of laser printers, instead of 5 laser printers.
With XML Schemas, most of these errors can be caught by your validating software.

Writing simple sheet using XSLT


XSLT Introduction
 XSL (eXtensible Stylesheet Language) is a styling language for XML.
 XSLT stands for XSL Transformations
 XSLT is the most important part of XSL
 XSLT transforms an XML document into another XML document
 XSLT uses XPath to navigate in XML documents

Online XSLT Editor


With our online editor, you can edit XML and XSLT code, and click on a button to view the
result.

XSLT Example
<?xml version="1.0"?>

<xsl:stylesheet version="1.0"

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


29
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>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>

SAX Parser

Using the SAX, DOM, etc., that are used as parsers, we can parse the XML file in Android. The

SAX parser can be used to parse the XML file only and not to create the XML file.

Advantage of SAX Parser over DOM:


Compared to DOM, the SAX parser consumes less memory.

Example:
activity_main.xml:

In the activity_main.xml file, we will drag a Textview from the palette.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


30
android:id="@+id/user_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp" />
</LinearLayout>

List_row.xml:(File: list_row.xml)

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="17dp" />
<TextView
android:id="@+id/designation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_marginTop="7dp"
android:textColor="#343434"
android:textSize="14dp" />
<TextView
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/designation"
android:layout_alignBottom="@+id/designation"
android:layout_alignParentRight="true"
android:textColor="#343434"
android:textSize="14dp" />
</RelativeLayout>

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


31
XML Parsers
An XML parser is a software library or package that provides interfaces for client applications to
work with an XML document. The XML Parser is designed to read the XML and create a way
for programs to use XML.

XML parser validates the document and check that the document is well formatted.

Let's understand the working of XML parser by the figure given below:

Types of XML Parsers


These are the two main types of XML Parsers:

1. DOM
2. SAX

DOM (Document Object Model)


A DOM document is an object which contains all the information of an XML document. It is
composed like a tree structure. The DOM Parser implements a DOM API. This API is very
simple to use.

Features of DOM Parser


A DOM Parser creates an internal structure in memory which is a DOM document object and the
client applications get information of the original XML document by invoking methods on this
document object.

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


32
DOM Parser has a tree based structure.

Advantages
1) It supports both read and write operations and the API is very simple to use.

2) It is preferred when random access to widely separated parts of a document is required.

Disadvantages
1) It is memory inefficient. (consumes more memory because the whole XML document needs
to loaded into memory).

2) It is comparatively slower than other parsers.

SAX (Simple API for XML)


A SAX Parser implements SAX API. This API is an event based API and less intuitive.

Features of SAX Parser


It does not create any internal structure.

Clients does not know what methods to call, they just overrides the methods of the API and place
his own code inside method.

It is an event based parser, it works like an event handler in Java.

Advantages
1) It is simple and memory efficient.

2) It is very fast and works for huge documents.

Disadvantages
1) It is event-based so its API is less intuitive.

2) Clients never know the full information because the data is broken into pieces.

What is XML DOM


DOM is an acronym stands for Document Object Model. It defines a standard way to access and
manipulate documents. The Document Object Model (DOM) is a programming API for HTML

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


33
and XML documents. It defines the logical structure of documents and the way a document is
accessed and manipulated.

As a W3C specification, one important objective for the Document Object Model is to provide a
standard programming interface that can be used in a wide variety of environments and
applications. The Document Object Model can be used with any programming language.

XML DOM defines a standard way to access and manipulate XML documents.

What does XML DOM


The XML DOM makes a tree-structure view for an XML document.

We can access all elements through the DOM tree.

We can modify or delete their content and also create new elements. The elements, their content
(text and attributes) are all known as nodes.

For example, consider this table, taken from an HTML document:

<TABLE>
<ROWS>
<TR>
<TD>A</TD>
<TD>B</TD>
</TR>
<TR>
<TD>C</TD>
<TD>D</TD>
</TR>
</ROWS>
</TABLE>

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


34
The Document Object Model represents this table like this:

XML DOM Example : Load XML File


Let's take an example to show how an XML document ("note.xml") is parsed into an XML DOM
object.

This example parses an XML document (note.xml) into an XML DOM object and extracts information from it with
JavaScript.

Let's see the XML file that contains message.

note.xml

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


<note>
<to>[email protected]</to>
<from>[email protected]</from>
<body>Hello XML DOM</body>

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


35
</note>

Let's see the HTML file that extracts the data of XML document using DOM.

xmldom.html

<!DOCTYPE html>
<html>
<body>
<h1>Important Note</h1>
<div>
<b>To:</b> <span id="to"></span><br>
<b>From:</b> <span id="from"></span><br>
<b>Message:</b> <span id="message"></span>
</div>
<script>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","note.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
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>
</body>
</html>

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


36
XML Soap
 SOAP stands for Simple Object Access Protocol
 SOAP is an application communication protocol
 SOAP is a format for sending and receiving messages
 SOAP is platform independent
 SOAP is based on XML
 SOAP is a W3C recommendation

Why SOAP?
It is important for web applications to be able to communicate over the Internet.

The best way to communicate between applications is over HTTP, because HTTP is supported
by all Internet browsers and servers. SOAP was created to accomplish this.

SOAP provides a way to communicate between applications running on different operating


systems, with different technologies and programming languages.

SOAP Building Blocks


A SOAP message is an ordinary XML document containing the following elements:

 An Envelope element that identifies the XML document as a SOAP message


 A Header element that contains header information
 A Body element that contains call and response information
 A Fault element containing errors and status information

Syntax Rules
Here are some important syntax rules:

 A SOAP message MUST be encoded using XML


 A SOAP message MUST use the SOAP Envelope namespace
 A SOAP message must NOT contain a DTD reference
 A SOAP message must NOT contain XML Processing Instructions

Skeleton SOAP Message


<?xml version="1.0"?>

<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


37
<soap:Header>
...
</soap:Header>

<soap:Body>
...
<soap:Fault>
...
</soap:Fault>
</soap:Body>

</soap:Envelope>

The SOAP Envelope Element


The required SOAP Envelope element is the root element of a SOAP message. This element
defines the XML document as a SOAP message.

Example
<?xml version="1.0"?>

<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
...
Message information goes here
...
</soap:Envelope>

The xmlns:soap Namespace


Notice the xmlns:soap namespace in the example above. It should always have the value of:
"http://www.w3.org/2003/05/soap-envelope/".

The namespace defines the Envelope as a SOAP Envelope.

If a different namespace is used, the application generates an error and discards the message.

The encodingStyle Attribute


The encodingStyle attribute is used to define the data types used in the document. This attribute
may appear on any SOAP element, and applies to the element's contents and all child elements.

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


38
A SOAP message has no default encoding.

Syntax
soap:encodingStyle="URI"

Example
<?xml version="1.0"?>

<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
...
Message information goes here
...
</soap:Envelope>

The SOAP Header Element


The optional SOAP Header element contains application-specific information (like
authentication, payment, etc) about the SOAP message.

If the Header element is present, it must be the first child element of the Envelope element.

Note: All immediate child elements of the Header element must be namespace-qualified.

<?xml version="1.0"?>

<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">

<soap:Header>
<m:Trans xmlns:m="https://www.w3schools.com/transaction/"
soap:mustUnderstand="1">234
</m:Trans>
</soap:Header>
...
...
</soap:Envelope>

The example above contains a header with a "Trans" element, a "mustUnderstand" attribute with
a value of 1, and a value of 234.

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


39
SOAP defines three attributes in the default namespace. These attributes are: mustUnderstand,
actor, and encodingStyle.

The attributes defined in the SOAP Header defines how a recipient should process the SOAP
message.

The mustUnderstand Attribute


The SOAP mustUnderstand attribute can be used to indicate whether a header entry is mandatory
or optional for the recipient to process.

If you add mustUnderstand="1" to a child element of the Header element it indicates that the
receiver processing the Header must recognize the element. If the receiver does not recognize the
element it will fail when processing the Header.

Syntax
soap:mustUnderstand="0|1"

Example
<?xml version="1.0"?>

<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">

<soap:Header>
<m:Trans xmlns:m="https://www.w3schools.com/transaction/"
soap:mustUnderstand="1">234
</m:Trans>
</soap:Header>
...
...
</soap:Envelope>

The actor Attribute


A SOAP message may travel from a sender to a receiver by passing different endpoints along the
message path. However, not all parts of a SOAP message may be intended for the ultimate
endpoint, instead, it may be intended for one or more of the endpoints on the message path.

The SOAP actor attribute is used to address the Header element to a specific endpoint.

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


40
Syntax
soap:actor="URI"

Example
<?xml version="1.0"?>

<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">

<soap:Header>
<m:Trans xmlns:m="https://www.w3schools.com/transaction/"
soap:actor="https://www.w3schools.com/code/">234
</m:Trans>
</soap:Header>
...
...
</soap:Envelope>

The encodingStyle Attribute


The encodingStyle attribute is used to define the data types used in the document. This attribute
may appear on any SOAP element, and it will apply to that element's contents and all child
elements.

A SOAP message has no default encoding.

Syntax
soap:encodingStyle="URI"

The SOAP Body Element


The required SOAP Body element contains the actual SOAP message intended for the ultimate
endpoint of the message.

Immediate child elements of the SOAP Body element may be namespace-qualified.

Example
<?xml version="1.0"?>

<soap:Envelope

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


41
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">

<soap:Body>
<m:GetPrice xmlns:m="https://www.w3schools.com/prices">
<m:Item>Apples</m:Item>
</m:GetPrice>
</soap:Body>

</soap:Envelope>

The example above requests the price of apples. Note that the m:GetPrice and the Item elements
above are application-specific elements. They are not a part of the SOAP namespace.

A SOAP response could look something like this:

<?xml version="1.0"?>

<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">

<soap:Body>
<m:GetPriceResponse xmlns:m="https://www.w3schools.com/prices">
<m:Price>1.90</m:Price>
</m:GetPriceResponse>
</soap:Body>

</soap:Envelope>

The SOAP Fault Element


The optional SOAP Fault element is used to indicate error messages.

The SOAP Fault element holds errors and status information for a SOAP message.

If a Fault element is present, it must appear as a child element of the Body element. A Fault
element can only appear once in a SOAP message.

The SOAP Fault element has the following sub elements:

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


42
Sub Element Description

<faultcode> A code for identifying the fault

<faultstring> A human readable explanation of the fault

<faultactor> Information about who caused the fault to happen

<detail> Holds application specific error information related to the Body


element

SOAP Fault Codes

The faultcode values defined below must be used in the faultcode element when describing
faults:

Error Description

VersionMismatch Found an invalid namespace for the SOAP Envelope element

MustUnderstand An immediate child element of the Header element, with the


mustUnderstand attribute set to "1", was not understood

Client The message was incorrectly formed or contained incorrect


information

Server There was a problem with the server so the message could not
proceed

Mr. Kadarkar B.M. Unit I- Element of Commercial portal


43

You might also like