You are on page 1of 9

III BSC VI SEM Web Technologies

UNIT – V
XML: defining data for web applications
Basic XML, document type definition, presenting XML, document object model. Web Services
…………………………………………………………………………………………………………………………
1. Basics of XML
XML stands for Extensible Markup Language. It is a text-based markup language derived from Standard Generalized Markup Language
(SGML).
XML tags identify the data and are used to store and organize the data, rather than specifying how to display it like HTML tags, which are
used to display the data. 
 XML is extensible − XML allows you to create your own self-descriptive tags, or language, that suits your application.
 XML carries the data, does not present it − XML allows you to store the data irrespective of how it will be presented.
 XML is a public standard − XML was developed by an organization called the World Wide Web Consortium (W3C) and is
available as an open standard.

Differences between XML and HTML


XML and HTML were designed with different goals:
 XML is designed to carry data emphasizing on what type of data it is.
 HTML is designed to display data emphasizing on how data looks.
 XML tags are not predefined like HTML tags.
 HTML is a markup language whereas XML provides a framework for defining markup languages.
 HTML is about displaying data; hence it is static whereas XML is about carrying information, which makes it dynamic

Advantages of XML
 XML is readable and understandable, even by novices, and no more difficult to code than HTML.
 XML is completely compatible with Java™ and 100% portable. Any application that can process XML can use your
information, regardless of platform.
 XML is extendable. Create your own tags, or use tags created by others, that use the natural language of your domain, that
have the attributes you need, and that makes sense to you and your users.
a) The basic structure of XML Document
An XML document starts with its name space included in <! Doctype> followed by a root element and its sub elements. All tags are case
sensitive and properly enclosed.
An XML document consists of three parts, in the order given:
I. An XML declaration (which is technically optional, but recommended in most normal cases)
II. A document type declaration that refers to a DTD (which is optional, but required if you want validation)
III. A body or document instance (which is required)
Collectively, the XML declaration and the document type declaration are called the XML prolog.

I.XML Declaration
XML declaration is case sensitive and must begin with “<?xml>” written in lower case.
The minimal XML declaration is:
<?xml version="1.0" ?> or <?xml version="1.0" encoding="UTF-8" ?>

II. Document Type Declaration


The document type declaration follows the XML declaration. The purpose of this declaration is to announce the root element (sometimes
called the document element) and to provide the location of the DTD.
The general syntax is:
<!DOCTYPE element DTD identifier
[ declaration 1
declaration 2 ] >
Types of DTD:
i. Internal: Elements are declared within the xml file. Syntax:<!DOCTYPE root-element [element-declaration]>
ii. External: Elements are declared outside the xml file. Syntax: <!DOCTYPE root-element SYSTEM “file-name”>

III. Document Body


Document Elements are the building blocks of XML. These divide the document into a hierarchy of sections, each serving a
specific purpose. You can separate a document into multiple sections so that they can be rendered differently, or used by a search engine.

Example:
<?xml version="1.0" ?>
Document Prolog

Page 1
III BSC VI SEM Web Technologies

<!DOCTYPE element DTD identifier


[ declaration 1
declaration 2 ] >

<contact-info>
<name>ABCD</name>
<company>XYZ</company> Document Elements
<phone>123456789</phone>
</contact-info>

b) XML Elements and attributes


ELEMENTS: XML elements can be defined as building blocks of an XML. Elements can behave as containers to hold text, elements,
attributes, media objects or all of these.
Each XML document contains one or more elements, the scope of which are either delimited by start and end tags, or for empty elements,
by an empty-element tag.
Syntax
<element-name attribute1 attribute2>
....content
</element-name>
XML elements must follow these naming rules:
 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
Attributes: Attributes are part of XML elements. An element can have multiple unique attributes. Attribute gives more information about
XML elements.
Element Attribute Rules:
 An attribute name must not appear more than once in the same start-tag or empty-element tag.
 An attribute must be declared in the Document Type Definition (DTD) using an Attribute-List Declaration.
 Attribute values must not contain direct or indirect entity references to external entities.
 The replacement text of any entity referred to directly or indirectly in an attribute value must not contain a less than sign (<)
c) Namespace in XML
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>
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>

Page 2
III BSC VI SEM Web Technologies

<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>
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".
<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.
2. Document Type Definition (DTD)
The XML Document Type Declaration, commonly known as DTD, is a way to describe XML language precisely. DTDs check vocabulary and
validity of the structure of XML documents against grammatical rules of appropriate XML language.
An XML DTD can be either specified inside the document, or it can be kept in a separate document and then liked separately.
Various building blocks of XML DTD
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.
Tags
Tags are used to markup elements.
A starting tag like <element_name> mark up the beginning of an element, and an ending tag like
</element_name> mark up the end of an element.
Examples:
A body element: <body>body text in between</body>.
A message element: <message>some message in between</message>
Attributes
 Attributes provide extra information about elements.
 Attributes are placed inside the start tag of an element. Attributes come in name/value pairs. The following "img" element has
additional information about a source file:
<img src="computer.gif" />
#PCDATA
 #PCDATA means parsed character data.
 #PCDATA is text that will be parsed by a parser. Tags inside the text will be treated as markup and entities will be expanded.
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.
#REQUIRED: The attribute value must be included in the element
#FIXED value: The attribute value is fixed
Page 3
III BSC VI SEM Web Technologies

DTD Entities or DTD entity References


There are two ways to specify DTD for your XML elements.
1. Internal DTD: In this type, the XML attributes and their DTD’s are specified in the same document.
student.xml
<!xml version=”1.0” Encoding=”UTF-8”?>
<!Doctype student(
<!ELEMENT student(name, address, study, marks)>
<!ELEMENT name(#PCDATA)>
<!ELEMENT address(#PCDATA)>
<!ELEMENT std(#PCDATA)>
<!ELEMENT marks(#PCDATA)>
)>
<student>
<name>Raju</name>
<address>Vinukonda</address>
<std>LKG</std>
<marks>30</marks>
</student>

2. External DTD: In this type, an external DTD file is created and its name must be specified in the corresponding XML file. The
following XML document illustrates the use of external DTD.

student.dtd
<!Doctype student(
<!ELEMENT student(name, address, study, marks)>
<!ELEMENT name(#PCDATA)>
<!ELEMENT address(#PCDATA)>
<!ELEMENT std(#PCDATA)>
<!ELEMENT marks(#PCDATA)>
)>
student.xml
<!xml version=”1.0” Encoding=”UTF-8”?>
<!Doctype student SYSTEM=”student.dtd”>
<student>
<name>Raju</name>
<address>Vinukonda</address>
<std>LKG</std>
<marks>30</marks>
</student>
Q) Document Object Model (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 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.
The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically
access and update the content, structure, and style of a document.
For example, consider this table, taken from an HTML document:
<TABLE>
<ROWS>
<TR>

Page 4
III BSC VI SEM Web Technologies

<TD>A</TD>
<TD>B</TD>
</TR>
<TR>
<TD>C</TD>
<TD>D</TD>
</TR>
</ROWS>
</TABLE>
The Document Object Model represents this table like this:

Q) Presenting XML:
What is XSLT
Before XSLT, first we should learn about XSL. XSL stands for EXtensible Stylesheet Language. It is a styling language for XML just like
CSS is a styling language for HTML.
XSLT stands for XSL Transformation. It is used to transform XML documents into other formats (like transforming XML into HTML).
What is XSL
In HTML documents, tags are predefined but in XML documents, tags are not predefined. World Wide Web Consortium (W3C)
developed XSL to understand and style an XML document, which can act as XML based Stylesheet Language.
An XSL document specifies how a browser should render an XML document.
Main parts of XSL Document
 XSLT: It is a language for transforming XML documents into various other types of documents.
 XPath: It is a language for navigating in XML documents.
 XQuery: It is a language for querying XML documents.
 XSL-FO: It is a language for formatting XML documents.
Advantage of XSLT
A list of advantages of using XSLT:
 XSLT provides an easy way to merge XML data into presentation because it applies user defined transformations to an XML
document and the output can be HTML, XML, or any other structured document.
 XSLT provides Xpath to locate elements/attribute within an XML document. So it is more convenient way to traverse an
XML document rather than a traditional way, by using scripting language.
 XSLT is template based. So it is more resilient to changes in documents than low level DOM and SAX.
 By using XML and XSLT, the application UI script will look clean and will be easier to maintain.
 XSLT templates are based on XPath pattern which is very powerful in terms of performance to process the XML
document.
 XSLT can be used as a validation language as it uses tree-pattern-matching approach.
 You can change the output simply modifying the transformations in XSL files.

How XSLT Works


The XSLT stylesheet is written in XML format. It is used to define the transformation rules to be applied on the target XML document.
The XSLT processor takes the XSLT stylesheet and applies the transformation rules on the target XML document and then it generates a
formatted document in the form of XML, HTML, or text format. At the end it is used by XSLT formatter to generate the actual output
and displayed on the end-user.

Page 5
III BSC VI SEM Web Technologies

Image representation:

Example:
student.xsl
<xsl:stylesheet version = "1.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "/class">
<html>
<body>
<h2>Student List</h2>

<table border = "1">


<tr bgcolor="lightgreen">
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
</tr>

<xsl:for-each select = "student">

<tr>
<td><xsl:value-of select = "firstname"/></td>
<td><xsl:value-of select = "lastname"/></td>
<td><xsl:value-of select = "nickname"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
students.xml
<?xml-stylesheet type = "text/xsl" href = "student.xsl"?>
<class>
<student>
<firstname>Graham</firstname>
<lastname>Bell</lastname>
<nickname>Garry</nickname>
</student>
<student>
<firstname>Albert</firstname>
<lastname>Einstein</lastname>
<nickname>Ally</nickname>
</student>
<student>
<firstname>Thomas</firstname>
<lastname>Edison</lastname>
<nickname>Eddy</nickname>
</student>
</class>

Page 6
III BSC VI SEM Web Technologies

Q. Explain about XML Schema.


XML Schema is commonly known as XML Schema Definition (XSD). It is used to describe and validate the structure and the
content of XML data. XML schema defines the elements, attributes and data types. Schema element supports Namespaces. It is
similar to a database schema that describes the data in a database.
Syntax
You need to declare a schema in your XML document as follows −

Example
The following example shows how to use schema −
<?xml version = "1.0" encoding = "UTF-8"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema">
<xs:element name = "contact">
<xs:complexType>
<xs:sequence>
<xs:element name = "name" type = "xs:string" />
<xs:element name = "company" type = "xs:string" />
<xs:element name = "phone" type = "xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The basic idea behind XML Schemas is that they describe the legitimate format that an XML document can take.
Elements
As we saw in the XML - Elements chapter, elements are the building blocks of XML document. An element can be defined within an
XSD as follows −
<xs:element name = "x" type = "y"/>
You can define XML schema elements in the following ways −
i. Simple Type
Simple type element is used only in the context of the text. Some of the predefined simple types are: xs:integer, xs:boolean, xs:string,
xs:date. For example −
<xs:element name = "phone_number" type = "xs:int" />
ii. Complex Type
A complex type is a container for other element definitions. This allows you to specify which child elements an element can contain
and to provide some structure within your XML documents. For example −
<xs:element name = "Address">
<xs:complexType>
<xs:sequence>
<xs:element name = "name" type = "xs:string" />
<xs:element name = "company" type = "xs:string" />
<xs:element name = "phone" type = "xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
In the above example, Address element consists of child elements. This is a container for other <xs:element> definitions, that allows to
build a simple hierarchy of elements in the XML document.
Attributes
Attributes in XSD provide extra information within an element. Attributes have name and type property as shown below −
<xs:attribute name = "x" type = "y"/>
Q) Web Services What
is Web Service
A Web Service is can be defined by following ways:
 It is a client-server application or application component for communication.
 The method of communication between two devices over the network.
 It is a software system for the interoperable machine to machine communication.
 It is a collection of standards or protocols for exchanging information between two devices or application.
Let's understand it by the figure given below:

Page 7
III BSC VI SEM Web Technologies

As you can see in the figure, Java, .net, and PHP applications can communicate with other applications through web service over the
network. For example, the Java application can interact with Java, .Net, and PHP applications. So web service is a language
independent way of communication.
Web services are XML-based information exchange systems that use the Internet for direct application-to- application interaction. These
systems can include programs, objects, messages, or documents.
Components of Web Services
The basic web services platform is XML + HTTP. All the standard web services work using the following components −
 SOAP (Simple Object Access Protocol)
 UDDI (Universal Description, Discovery and Integration)
 WSDL (Web Services Description Language)
How Does a Web Service Work?
A web service enables communication among various applications by using open standards such as HTML, XML, WSDL, and SOAP. A
web service takes the help of −
 XML to tag the data
 SOAP to transfer a message
 WSDL to describe the availability of service.
You can build a Java-based web service on Solaris that is accessible from your Visual Basic program that runs on Windows.
You can also use C# to build new web services on Windows that can be invoked from your web application that is based on JavaServer
Pages (JSP) and runs on Linux.
Example
Consider a simple account-management and order processing system. The accounting personnel use a client application built with Visual
Basic or JSP to create new accounts and enter new customer orders.
The processing logic for this system is written in Java and resides on a Solaris machine, which also interacts with a database to store
information.
The steps to perform this operation are as follows –
 The client program bundles the account registration information into a SOAP message.
 This SOAP message is sent to the web service as the body of an HTTP POST request.
 The web service unpacks the SOAP request and converts it into a command that the application can understand.
 The application processes the information as required and responds with a new unique account number for that customer.
 Next, the web service packages the response into another SOAP message, which it sends back to the client program in
response to its HTTP request.
 The client program unpacks the SOAP message to obtain the results of the account registration process.

UNIT-V
Important Questions

Short Questions
1. Define XML and Explain Goals of XML.
2. Web Service and its components.
3. Briefly write about presenting XML.

Essay Questions
1. Explain the syntax rule of xml document with example.
2. Why XML? Write about xml elements.

Page 8
III BSC VI SEM Web Technologies

3. Explain XML Schema with example.


4. Document Object Model.

Page 9

You might also like