0% found this document useful (0 votes)
175 views49 pages

Web Technologies Unit-Ii: .SGML .XSLT .Soap SAX - Dom - Use of XML

XML is a markup language that allows users to define their own tags. It is more flexible than HTML as it allows users to create custom tags. XML documents are well-formed if they follow rules like matching opening and closing tags. DTDs are used to define the structure and elements of an XML document and ensure documents are valid by conforming to the DTD. DTDs specify elements, attributes, element order and whether elements are optional or mandatory.

Uploaded by

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

Web Technologies Unit-Ii: .SGML .XSLT .Soap SAX - Dom - Use of XML

XML is a markup language that allows users to define their own tags. It is more flexible than HTML as it allows users to create custom tags. XML documents are well-formed if they follow rules like matching opening and closing tags. DTDs are used to define the structure and elements of an XML document and ensure documents are valid by conforming to the DTD. DTDs specify elements, attributes, element order and whether elements are optional or mandatory.

Uploaded by

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

Web Technologies

UNIT-II
XM
L

INTRODUCTION TO XML:

(eXtensible markup language) the World Wide Web consortium (W3C) begins their work on
XML, another meta markup language. The first XML version 1.0, published in 1998. The
difference between XML and HTML is HTML is a mark up language where as XML is a mark
up language used to create new mark up languages. HTML limits you to use only fixed number
of tags, where as XML allows to create new tags. For example, you are developing website for
a college, then you have tags like
<SNO>,<STUDENTNAME>,<DOB> etc. HTML,XML languages are derived from
standard generalized markup language (SGML).
XML and related technologies and those are:
SGML,XSL,W3C,SOAP,XSLT,DOM,SAX
.SGML, standard generalized markup language is basis for all markup language.
XSL,extensible stylesheet language is a combination of XML and style sheets.
.XSLT,XSL transformations provides rules for transformations from one XML to
another.
.SOAP, simple object access protocol is communication protocol for internet to
XML documents and it provides notifications for events.
.SAX , simple APL for XML, predefined application package interface.
. DOM, document object model.
---------------use of XML------------------------
-simplifies the data exchange procedure
-easy to organize the document
-tags or document elements are reusable
-XML provides consistency in display of information

Applications of XML
1.electronic commerce (popularly known as E-commerce)
2. financial funds transfer
multimedia messages and messaging exchange
3.better environment for data transfer

The XML document is language neutral. That means a java program can generate an
XML document and this document can be parsed by perl.
XML files are independent of an operating system.

1
3.1.3 Differences between XML and HTML

XML HTML

User defined tags Predefined tags

User has control on tags As predefined, on such


control
XML separates content from
presentation HTML specifies presentation

XML allows any kind of tag HTML defines set of legal


names tags
like<UNAME></UNAME>

XML based on SGML HTML based on SGML

XML allows users to create HTML doesn’t allow users to


new tags create new tags

Self describing data can be No possibility


possible

You can generate new mark No such possibility


up languages using XML

XML is case sensitive HTML is not case sensitive


Root element is <HTML>
Root element is user defined
and only one root element
allowed.

XML features:
XML allows the user to define his own tags and his own document structure.
XML document is pure information wrapped in XML tags.
XML is a text based language, plain text files can be used to share data. XML
provides a software and hardware independent way of sharing data.
simple XML document:

XML tags and attributes depend on the user. XML declaration always starts with xml key word.
<?xml version =’’1.0’’ encoding=”UTF-8” standardalone=”no”?>
Processing instruction:<?xml-stylesheet href=”simple.xsl” type=”text/xsl”
?>

Comments: <!-- yytotqhou -->

Here you can give version along with the xml key word, version is attributing and value
1.0 indicates that xml 1.0 version you are using.

First declaration tag starts with left angular bracket along with question
mark(<?),ending with question mark followed by the right angular braket(?>)second
assignment is comment and then we start with actual xml program. First element<student>
called root element. A root element contains other sub elements.here we used <name>as sub
element and its also known as container element,because it contains other sub elements<first
name>,<last name>.sub elements is also called as children.
Example 1:
<?xml version="1.0"?>
<student>
<name >
<firstname>Raju</firstname>
<lastname>ch</lastname>
</name>
<name>
<firstname>mamatha</firstname>
<lastname>ch</lastname>
</name>
</student>
<!--<para type=”note”>xml data cannot contain &lt;</para> &apso; &amp; &quot;-->

The filename extension used for xml program is .xml. the name of above program is one
.xml.
Empty elements:
<line width=”100” height=”200” />

<line width=”100”> </line>

<element-name attribute-name=”attribute-value”> </Element-name>


Elements and attributes:

In XML the basic entity is element the elements are used for defining the tags.

The elements typically consist of opening and closing tag. Mostly only one element is used to
define a single tag.
The syntax of writing any element for opening tag is <element name>
The syntax of writing any closing element for closing tag is </element name>
An empty tag can be defined by putting a /(forward slash)before closing bracket. A
space or a tab character is not allowed in the element name or in attribute name.

Namespace in XML:

Sometime we need to create two different elements by the same name. The xml
document allows creating different elements which are having the common name. This
technique is known as name space. In some web documents it becomes necessary to have the
same name for two different elements. Here different elements mean the elements which are
intended for different purpose.
Example 2:

<?xml version="1.0"?>
< file>
<text fname="photo1.jpg">
<describe> it is a .jpg file</describe>
</text>
<text fname="photo2.bmp">
<describe>it is abmp file</describe>
</text>
</file>
An XML document can be defined as:
• Well-formed: If the XML document adheres to all the general XML rules such as tags
must be properly nested, opening and closing tags must be balanced, and empty tags must
end with '/>', then it is called as well-formed.
OR
Valid: An XML document said to be valid when it is not only well-formed, but it also
conforms to available DTD that specifies which tags it uses, what attributes those tags can
contain, and which tags can occur inside other tags, among other properties

Well formed xml : 1)should contain one and exactly one root element

2) All tags must be closed


<contact>
<person>
<name>roy</name>
<number>998877766</number>
</person>
</contact>

In HTML:<img> <br><hr>
In XML: i)<hr/> ii)<hr></hr>

3)ALL Tags must be properly nested:


<b><i>this is example</b></i>--------incorrect
<b><i>this is example</i></b>--------correct

4)XML tags are case sensitive:


<Msg>this is simple</Msg>
<msg>this is simple</msg>

5)Attributes must always be quoted:


<speed unit=”rpm”>1000</speed>

6)Certain characters are reserved for processing:


<cond>if salary < 1000 then</cond>------incorrect
<cond>if salary &lt; 1000 then</cond>--------correct

Predefined entities:
&lt; &gt; &amp; &quot; &apos;
&#60; &#62; &#38; &#34; &#39;

NAMESPACE:

Client.xml server.xml

<table>
<table><ro><co>javascript</co></ro>
<tr><td>javascript</td></tr>
</table> </table>

Combining both the above files:

<tech>
<table>

<tr><td>javascript</td></tr>
</table>
<table>
<ro><co>javascript</co></ro>
</table>
</tech>

Adding additional elements:


<tech>
<client>
<table>

<tr><td>javascript</td></tr>
</table>
</client>
<server>
<table>
<ro><co>javascript</co></ro>
</table>
</server>
</tech>

Solution:
Client.xml
<c:table>

<c:tr><c:td>javascript</c:td></c:tr>

</c:table>

Server.xml: <s:table>
<ro><co>javascript</co></ro>
</s:table>

BINDING:
<c:table xmlns:c=”http://it.site.jusl.ac/client”>

<tr><td>javascript</td></tr>

</c:table>

<tech xmlns:c=http://it.site.jusl.ac/client xmlns:s=http://it.site.jusl.ac/server >


<c:table>
<c:tr><c:td>javascript</c:td></c:tr>

</c:table>

<s:table>
<s:ro><s:co>javascript</s:co></s:ro>
</s:table>
</tech>

3.2.DOCUMENT TYPE DEFINITION (DTD):


XML Document Type Declaration commonly known as DTD is a way to describe precisely the XML
language. DTDs check the validity of, structure and vocabulary of an XML document against the
grammatical rules of the appropriate XML language.

DOCTYPE

Types
DTD can be classified on its declaration basis in the XML document, such as:
 Internal DTD

 External DTD

When a DTD is declared within the file it is called Internal DTD and if it is declared


in a separate file it is called External DTD.
We will learn more about these in the chapter DTD Syntax

Features
Following are some important points that a DTD describes:
 the elements that can appear in an XML document.

 the order in which they can appear.

 optional and mandatory elements.

 element attributes and whether they are optional or mandatory.

 whether attributes can have default values.

Advantages of using DTD


 Documentation - You can define your own format for the XML files. Looking at this document a
user/developer can understand the structure of the data.

 Validation - It gives a way to check the validity of XML files by checking whether the elements appear
in the right order, mandatory elements and attributes are in place, the elements and attributes have not
been inserted in an incorrect way, and so on.

Disadvantages of using DTD


 It does not support the namespaces.

 It supports only the text string data type

Various building blocks of XML are


1. Elements
2. Attribute.
3. CDATA
4. PCDATA

Elements: the basic entity is element. The elements are used for defining the tags. The
elements typically consist of opening and closing tag. Mostly only one element is used to
define a single tag.

Syn: <!ELEMENT element-name(content)


EX: DTD________ <!ELEMENT name(#PCDATA)>

name>Tutorials Point</name>
XML__________________ <

3TYPES OF ELEMENTS:
:1.EMPTY ELEMENTS/standalone Elements

An element that can not contain any content


The html image tag in xml would typically be
empty, such as <image></image> or
<image/>
EXAMPLE:DTD <!ELEMENT test EMPTY>
empty elements are more XML useful
<test /> with the use
OR <test></test>

of attributes
2.ANY ELEMENT(unrestricted element)
• An element that can contain any content
• useful when transferring a lot of mixed or unknown data
• Note:It removes all validity checkings.so avoid using this type of declaration
EXAMPLE: <!ELEMENT test ANY>

3.MIXED ELEMENT/Compound Elements


• Elements that can contain a set of content alternatives
• Separate the options with the “or” symbol “|”
EXAMPLE: <!ELEMENT test <#PCDATA | name>
<test>
xml
<name>extensible markup language</name>
</test>

4.simple Element: Elements that cannot contain other elements


EXAMPLE: <!ELEMENT test (#PCDATA)>
<test>extensible markup language</test>
Occurance Indicator:
<ELEMENT student(name)*>
Here * indicates zero or more occurrences.other characters are
+ indicates 1,2,n.
? indicate 0,1
Default exactly once.

DECLARING MULTIPLE CHILDREN:


Sequence(,)
Choice( | )
Composite( () )
combining

sequence operator:
in DTD-----<!ELEMENT parent (exp1,exp2,exp3,……..)>

<!ELEMENT emp (fname,midname,lname) >


<!ELEMENT fname (#PCDATA)>
<!ELEMENT midname (#PCDATA)>
<!ELEMENT lname (#PCDATA)>

In XML------
<emp>
<fname>uttam</fname>
<midname>kumar</midname>
<lname>roy</lname>
</emp>

Combining simple and compound elements:


<!ELEMENT emp(name,contact)
<!ELEMENT name (fname,midname,lname) >
<!ELEMENT fname (#PCDATA)>
<!ELEMENT midname (#PCDATA)>
<!ELEMENT lname (#PCDATA)>
<!ELEMENT contact(#PCDATA)
In XML------
<emp>
<name>
<fname>uttam</fname>
<midname>kumar</midname>
<lname>roy</lname>
</name>
<contact>9988776655</contact>
</emp>

Choice operator:[ | ]

-----<!ELEMENT parent (exp1 | exp2 | exp3,……..)>

-----<!ELEMENT product (price | discountprice)>

<product>
<discountprice>2000</discountprice>
</product>

Composite operator:[ ( ) ]

-----<!ELEMENT biodata (dob, (company,title)*, contact+)>


<!ELEMENT dob(#PCDATA)>
<!ELEMENT company(#PCDATA)>
<!ELEMENT title(#PCDATA)>
<!ELEMENT contact(#PCDATA)>
<biodata>
<dob>20/05/1990</dob>
<company>wipro</company>
<title>manager</title>
<contact>8877665544</contact>
</biodata>
<biodata>
< company>infosys</company>
<title>sr manager</title>
<contact>8877665544</contact>
</biodata>

Combining operators:

-----<!ELEMENT emp( fname, midname?, lname)>

Elements containing both text and other Elements:

<!ELEMENT order(item+)>
<!ELEMENT item(#PCDATA | desc)*>
<!ELEMENT desc(#PCDATA)>

<order>
<item>
XBEE
<desc>WPAN development kit</desc>
</item>
</order>

Entities
There are five built-in entities that play in well-formed XML, they are:

&lt; &gt; &amp; &quot; &apos;


&#60; &#62; &#38; &#34; &#39;

Attributes: it provides extra information about elements. These are always placed inside the
starting tag of an element. Attribute always come in name/value pairs.
Ex:<img src=’raj.jpg”>

Element –img attribute-src value of attribute -raj.jpg.


EXAMPLE: <?xml version = "1.0"?>

<!DOCTYPE address [

<!ELEMENT address ( name )>

<!ELEMENT name ( #PCDATA )>

<!ATTLIST name id CDATA #REQUIRED>


]>

<address>

<name id="123">Tanmay Patil</name>

</address>

CDATA: CDATA also 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.

EXAMPLE:

<code>

<![CDATA[

<script language=”javascript”>

function less(x,y){

Return x<y;

A=less(2,3);

</script>

]]>

<code>

CDATA
CDATA attributes are strings , any text is allowed

ATTRIBUTES:

<!ATTLIST elementname attributename attributetype default-value>

<!ELEMENT line EMPTY>


<ATTLIST line width CDATA ‘100’>

<line width=’100’/>

Attribute types:
1)String type:
CDATA
2)Tokenized type
i)ID ii)IDREF iii)IDREFS iv)NMTOKEN(S) V)Entitie(s)
3)Enumerated type
4)Notation type

ID
The values of an ID attribute must be a name, that name must be unique. ID resembles the
primary key concept
EXAMPLE:<!ATTLIST question no ID #REQUIRED>
<!ATTLIST emp id ID #REQUIRED>

IDREF or IDREFS
It is similar to that of Foreign key concept in data base
It is used to establish connections between elements
Example(in DTD):<!ATTLIST question no ID #REQUIRED>
<!ATTLIST answer qno IDREF #REQUIRED>
(IN XML): <question no=“1”>
What is full form of DTD?
</question>

<question no=“2>”
What is full form of XML?
</question>

<answer qno=“1”>
Document type Definition
</answer>

IDREFS: DTD----- <!ATTLIST stu roll ID #REQUIRED>


<!ATTLIST sub sid ID #REQUIRED>
<!ATTLIST marks ref IDREFS #REQUIRED>

XML-------<stu roll=’101’>samir</stu>
<sub sid=’s1’>web technology</sub>
<marks ref=’101 s1’>82</marks>

ENTITY or ENTITIES
An ENTITY attribute’s must be the name of a single ENTITY. The value of an
ENTITIES attribute may contain multiple entity names separated by white space.

EX:1. <!ATTLIST photo src ENTITY #REQUIRED>


<photo src=‘logo’ />----------------(XML)

EX:2 <!ATTLIST photo src ENTITIES #REQUIRED>


<photo src=‘logo’ ‘home’ /> --------- -------(XML)

NMTOKEN or NMTOKENS

Name token attributes are a restricted form of string attribute, but there are no other restrictions
on the word.

Ex:1 . <!ATTLIST item serialno NMTOKEN #REQUIRED>


<item serialno=‘A8GD -SX34’ > (should not include white spaces)

Ex:2 <!ATTLIST emp profile NMTOKENS #REQUIRED>


<emp profile =‘male Married’> Mark </emp>
<emp profile =‘female Single’>ANNI </emp>

ENUMARATED TYPES:
Enumeration
notation

List of Names Enumerated : You can specify that the value of an attribute must be taken from a
specific list of names. This frequently called an enumerated type because each of the possible
values must be explicitely enumerated in the declaration

Ex:1 <!ATTLIST emp gender (male | female) #REQUIRED>

<emp gender=‘male’> RAM </emp>


<emp gender=‘female’></emp>

Notation:
<!NOTATION jpg SYSTEM ‘image/gif’>
<!ENTITY logo SYSTEM ‘ju.jpg NDATA jpg>

<!ELEMENT photo (#PCDATA)>


<!ATTLIST photo format NOTATION(jpg) #IMPLIED>

XML-------<photo src=’logo’ format=’jpg’/>

Attribute Defaults:
Default value EXPLANATION

Default The default value of the attribute


#REQUIRED Attribute is required
#IMPLIED Arrtibute is not required
#FIXED Attribute value is fixed
Default:This is optional
DTD-------<!ATTLIST line width CDATA ‘100’>
XML-------1)<line width=’200’/>
2)<line/>(This is equalent to below

Statement)

<line width=’100’/>

#REQUIRED
The attribute must have an explicitly specified value for every occurrence of the element in the
document
DTD---------<!ATTLIST price currency CDATA #REQUIRED>
XML--------<price currency=’INR’>100</price>-----------correct
<price>100</price>-------------------------------incorrect

#IMPLIED
The attribute value is not required and no default value is provided. If a value is not specified
the XMP processor must proceed without one.

Ex:<!ATTLIST speed unit CDATA #IMPLIED>


<speed unit=‘rpm’>1000</speed> (OR)
<speed>1000</speed>

“value” An attrubute can be given any legal value as a default. The attribute value is not
required on each element of the document, and if it is not present it will appear to be the
specified default

Ex: :<!ATTLIST speed unit CDATA ‘100’>


<speed unit=‘200’ </speed>

Ex: <speed /> -----------------unit attribute is not provided , so it takes 100

#FIXED “value” An attribute declaration may specify that an attribute has a fixed value. In
this case, the attribute is not required, but if it occurs, it must have the specified value. If it is
not present, it will appear to be the specified default

Ex:<!ATTLIST speed unit CDATA #FIXED ‘rpm’>


<speed unit=‘rpm’>1000</speed> (OR) <speed>1000</speed>

<speed unit=’rps’> 120</speed>----------incorrect

1. InternalDTD:
In an internal DTD we enclose the DTD in a <DOCTYPE> element, providing the name
of the root element of the document in the <DOCTYPE>
EXAMPLE 3
<?xml version="1.0"?>
<!DOCTYPE PURCHASE[
<!ELEMENT PURCHASE (CUSTOMER)?>
<!ELEMENT CUSTOMER (name,address,date,order)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT address (#PCDATA)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT order (items*)>
<!ELEMENT item (product,price)>
<!ELEMENT product (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>
<purchase>
<customer>
<name>vandana</name>
<address>vzm</address>
<date>dec,14,1986</date>
<order>
<item>
<product>GOLD</product>
<price>$.55</price>
</item>
<item>
<product>SAREES</product>
<price>$.55</price>
</item>
</order>
</customer>
<customer>
<name>ramesh</name>
<address>vzm</address>
<date>may,08,1979</date>
<order>
<item>
<product> web technologies</product>
<price>$00</price>
</item>
</order>
</customer>
</purchase>

3.2.1.2. External DTD:

In this type, an external DTD file is created ans its name must be specified in the
corresponding XML file. XML document illustrate the use of external DTD.
Step1: creation of DTD file[student.dtd]
Open notepad type following code into it-
<!ELEMENT PURCHASE (CUSTOMER*)>
<!ELEMENT CUSTOMER (name,address,date,order)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT address (#PCDATA)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT order (items*)>
<!ELEMENT item (product,price)>
<!ELEMENT product (#PCDATA)>
<!ELEMENT price (#PCDATA)>

Step2:creation of XML document[example3.xml]


Now create a XML document as follows-
<?xml version="1.0" ?>
<!DOCTYPE purchase SYSTEM "exdtd.dtd">
<purchase>
<customer>
<name>vandana</name>
<address>vzm</address>
<date>dec,14,1986</date>
<order>
<item>
<product>GOLD</product>
<price>$.55</price>
</item>
<item>
<product>SAREES</product>
<price>$.55</price>
</item>
</order>
</customer>
<customer>
<name>ramesh</name>
<address>vzm</address>
<date>may,08,1979</date>
<order>
<item>
<product> web technologies</product>
<price>$00</price>
</item>
</order>
</customer>
</purchase>
Command to validate xml with DTD : xmllint --noout --dtdvalid test.dtd test.xml
XML SCHEMAS:

Xml schema is an XML alternative to DTD. The XML schemas are used to represent the
structure of XML document. XML schema language is also referred to as XML schema
definition. The XML schema language is called as XML schema definition language
(XSD).XML schema are written in XML . XML schema supports name spaces.

Limitations of DTD:
There is no built-in datatype in DTD

No New datatypes can be created

The use of cardinality in DTD is limited

Namespace are not supported

Little control over mixed content(text plus Elements)

Strengths of SCHEMA:

XML Schemas provide much greater specification than DTD

They support large no.of built-in datatypes

They are namespaces-aware

XML schema definition:(XSD)

The <schema >element is the root element of every XML schema.<schema>element may
contains some attributes.

SCHEMA STRUCTURE:

<?xml version=”1.0” standalone=”no”?>


<xs:schema>

</xs:schema>

Schema Definition Components:


Schema element
Element definition
Attribute definition
Annotations
Type definition
Schema Element:

<?xml version=”1.0” >


<xs:schema xmlns:xs=’http://www.w3.org/2001/XMLSchema’>

Targetnamespace
elementFormDefault: qualified or unqualified

SCHEMA ELEMENTS:
<all> <attribute> <choice> <union> <complexType> <group>
<element> <list> <schema>

Element Definition:

XSD-----<xs:element name=’elementname’ type=’element-type’>


<xs:element name=’stu’ type=’string’>

Xml-----<stu>

Global and Local Declarations:

XSD elements are two types

1.simple element.

2. complex element.

1.simple element is an XML element that can contain only text. It can’t contain any other
elements or attributes.
Syntax: <xs:element name=”aaa” type=”bbb”/>
--------------------------------------------------------------------------------------------------------------------

EX: <?xml version=”1.0”?>------------------------save this file as sample.xsd

<xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema>
<xs:element name=’greeting’ type=’xs:string’/>
</xs:schema>
IN XML: <?xml version=”1.0”?> ----------------------------save it as sam.xml
<greeting>HELLO EVERYBODY</greeting>

Validating command: Xmllint –schema sample.xsd sam.xml


--------------------------------------------------------------------------------------------------------------------

EXAMPLE 4:
XML schema[student schema.xsd]
<?xml version=”1.0”?>
<xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema>
<xs:element name=”student”/>
<xs:complex type>
<xs:sequence>
<xs:element name=”name”type=”xs:string”/>
<xs:element name=”address” type=”xs:string”/>
<xs:element name=”std” type=”xs:string”/>
<xs:element name=”marks” type=”xs:integer”/>
</xs:sequence>
</xs:complex type>
</xs:element>
</xs:schema>

The xs is qualifier used to identify the schema elements and types.the document
element of schema is xs:schema. The xs:schema is root element.it takes the attribute
xmlns:xs which has the value http://www.w3.org/2001/XML schema. this declaration
indicates that document should follow the rules of XML schema.
Then comes xs:element which is used to define the xml element. In above case the
element student is of complex type who have four chils elements:name,address,std and
marks. All these elements are of simple type string.

3.3.2 XML datatypes:


Various data types are
String, date, numeric, Boolean

Xs:string it contains group of characters, lines, tabs or white spaces.

Xs:date used to represent date.the format of this date is YYYY-mm-dd

xs:time use to represent time. The format of this time is hh:mm:ss


Xs:decimal used to represent float values.
Xs:integer used to represent integer values.
Xs:Boolean used to resent Boolean values either true or false.
Declaring simple Elements:
<xs:element name=’elementname’ type=’element-type’>
<xs:element name=”person” type=”xs:string”/>

Defaults and fixed values:

<xs:element name=”married” type=”xs:boolean” default=”false” ” />

<xs:element name=”institution” type=”xs:string” fixed=”JU”/>

Occurrence Indicator:
minOccurs: <xs:element name=”married” type=”xs:boolean” minOccurs=’0’/>
maxOccurs: <xs:element name=”married” type=”xs:boolean” minOccurs=’4’ maxOccurs=’6’
/>

<xs:element name=”married” type=”xs:boolean” minOccurs=’0’


maxOccurs=’unbounded’/>

Schema DTD
minOccurs=’0’ maxOccurs=’unbounded’ * Zero or More

minOccurs=’1’ maxOccurs=’unbounded’ + one or more

minOccurs=’0’ ? optional
none none Exactly once

Declaring Complex Elements:


<xs:element name=’elementname’ type=’element-type’>

<xs:complexType>

Skeleton of complex type

</xs:complexType>
Ex:<xs:complexType name=’person’>
<xs:sequence>
<xs:element name=’fname’ type=’xs:string’/>
<xs:element name=’lname’ type=’xs:string’/>
</xs:sequence>
</xs:complexType >

Xml: <person>
<fname>uttam</fname>
<lname>Roy</lname>
</person>

Defining Attributes:

<xs:attribute name=’attribute name’ type=’attribute type’>

<xs:attribute name=’id’ type=’xs:string’/>

EX:
<xs:complexType name=’person’>
<xs:sequence>
<xs:element name=’fname’ type=’xs:string’/>
<xs:element name=’lname’ type=’xs:string’/>
</xs:sequence>
<xs:attribute name=’id’ type=’xs:string’/>
</xs:complexType >
<xs:element name=’emp’ type=’person’/>

Xml:
<emp id=’1234’>
<fname>uttam</fname>
<lname>Roy</lname>
</emp>

Attribute Element Properties: optional; required prohibited


Use

Default

fixed

<xs:attribute name=’id’ type=’xs:string’ use=’required’/>


<xs:attribute name=’id’ type=’xs:boolean’ default=’false’/>

<xs:attribute name=’id’ type=’xs:boolean’ fixed=’false’/>

Occurrence Indicator:
Order indicator:

Sequence : the child elements in xml document must appear in the order they are declared in
the XSD schema

all : The child elements described in XSD schema can appear in the XML document in
any order

choice : only one of the child elements describes in XSD schema can in XML Document

Sequence indicator:
<xs:complexType name=’person’> XML
<xs:sequence minOccurs=’0’> <person>
<xs:element name=’fname’ type=’xs:string’/> <fname>uttam</fname>
<xs:element name=’lname’ type=’xs:string’/> <lname>Roy</lname>
</xs:sequence> </person>
</xs:complexType >

all indicator:
<xs:complexType name=’person’> <emp>
<xs:all> <lname>Roy</lname>
<xs:element name=’fname’ type=’xs:string’/> <fname>Uttam</fname>
<xs:element name=’lname’ type=’xs:string’/> </emp>
</xs:all>
</xs:complexType >
<xs:element name=’emp’ type=’person’/>

EX:2<xs:complexType name=’person’>
<xs:all>
<xs:element name=’fname’ type=’xs:string’/>
<xs:element name=’Mname’ type=’xs:string’ minOccurs=’0’/>
<xs:element name=’lname’ type=’xs:string’/>
</xs:all>
</xs:complexType >
<xs:element name=’emp’ type=’person’/>
choice indicator:

<xs:complexType name=’person’>

<xs:choice>

<xs:element name=’fname’ type=’xs:string’/>

<xs:element name=’lname’ type=’xs:string’/>

</xs:choice>

<xs:attribute name=’id’ type=’xs:string’/>

</xs:complexType >

<xs:element name=’emp’ type=’person’/>

XML:

<emp id=121>

<lname>Roy</lname>

</emp>

EX:2

<xs:complexType name=’documenttype’>

<xs:choice maxOccurs=’unbounded’>

<xs:element name=’paragraph’ type=’xs:string’/>

<xs:element name=’image’ type=’xs:string’/>

</xs:choice>

</xs:complexType >

<xs:element name=’document’ type=’ documenttype’/>

Composite Indicators:
<xs:complexType name=’person’>
<xs:sequence>
<xs:element name=’fname’ type=’xs:string’/>
<xs:element name=’lname’ type=’xs:string’/>
<xs:choice>
<xs:element name=’dob’ type=’xs:date’/>
<xs:element name=’age’ type=’xs:integer’/>
</xs:choice>
</xs:sequence>
</xs:complexType >
<xs:element name=’emp’ type=’person’/>

XML-------<emp>

<fname>Uttam</fname>

<lname>Roy</lname>

<age>19</age>

</emp>

REFERENCING:

<xsd:element ref=’dob’ maxOccurs=’1’/>

EX:

<xs:element name=’fname’ type=’xs:string’/>

<xs:element name=’roll’ type=’xs:integer’/>

<xs:element name=’salary’ type=’xs:integer’/>

<xs:complexType name=’stuType’>

<xs:sequence>

<xs:element ref=’fname’/>

<xs:element ref=’roll’/>

</xs:sequence>

<xs:complexType >
<xs:complexType name=’empType’>

<xs:sequence>

<xs:element ref=’fname’/>

<xs:element ref=’salary’/>

</xs:sequence>

<xs:complexType>

GROUP INDICATORS:

1 Group

2 attributegroup

Element group indicator:

<xs:group name=’groupname’>

…..

…….

</xs:group>

Ex:<xs:group name=’personinfo’>

<xs:sequence>

<xs:element name=’fname’ type=’xs:string’/>

<xs:element name=’lname’ type=’xs:string’/>

</xs:sequence>

</xs:group>

<xs:complexType name=’stuType’>

<xs:sequence>

<xs:group ref=’personinfo’/>

<xs:element name=’roll’ type=’xs:string’/>


</xs:sequence>

</xs:complexType>

Attribute Group Indicator:

<xs:attributeGroup name=’datedata’>

<xs:attribute name=’issudate’ type=’xs:date’ use=’required’/>

<xs:attribute name=’expirydate’ type=’xs:date’ use=’required’/>

</xs:attributeGroup >

MIXED CONTENT:

<biodata>

Worked for <company>TCS</company>as a<title>programmer</title>

</biodata>

<xs:complexType name=’biodataType’ mixed=’true’>

<xs:sequence maxOccurs=’unbounded’>

<xs:element name=’company’ type=’xs:string’/>

<xs:element name=’title’ type=’xs:string’/>

</xs:sequence>

</xs:complexType >

<xs:element name=’biodata’ type=’biodataType’/>


3.3.3. PRESENTING XML:
XSL:
XSL is a language capable of transforming as well as formatting given XML
document.XSL means extensible style sheet language.
Working of XSL:

1. Start with a raw XML document (create a xml file)

2. Create an XSL style sheet (create a XSL file)


3. Link the XSL style sheet to the XML document.

4. Use XSLT complaint browser to transform your XML into XHTML.

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.

XSLT Syntax
Let's take an example to take a sample XML file and transform it into a well
formatted HTML document.

See this example:

Create an XML file named employee.xml, having the following code:


employee.xml

Define an XSLT stylesheet document for the above XML document. You should follow the criteria give
below:

o Page should have a title employee.


o Page should have a table of employee's details.
o Columns should have following headers: id, First Name, Last Name, Nick Name, Salary
o Table must contain details of the employees accordingly.

Step1: Create XSLT document

Create the XSLT document which satisfies the above requirements. Name it as employee.xsl and save
it in the same location of employee.xml.

Employee.xsl

1. <?xml version = "1.0" encoding = "UTF-8"?>  
2. <!-- xsl stylesheet declaration with xsl namespace:   
3. Namespace tells the xlst processor about which   
4. element is to be processed and which is used for output purpose only   
5. -->   
6. <xsl:stylesheet version = "1.0"   
7. xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">     
8. <!-- xsl template declaration:    
9. template tells the xlst processor about the section of xml   
10. document which is to be formatted. It takes an XPath expression.   
11. In our case, it is matching document root element and will   
12. tell processor to process the entire document with this template.   
13. -->   
14.    <xsl:template match = "/">   
15.       <!-- HTML tags   
16.          Used for formatting purpose. Processor will skip them and browser   
17.             will simply render them.   
18.       -->   
19.       <html>   
20.          <body>  
21.             <h2>Employee</h2>   
22.             <table border = "1">   
23.                <tr bgcolor = "#9acd32">   
24.                   <th>ID</th>   
25.                   <th>First Name</th>   
26.                   <th>Last Name</th>   
27.                   <th>Nick Name</th>   
28.                   <th>Salary</th>   
29.                </tr>   
30.                <!-- for-each processing instruction   
31.                Looks for each element matching the XPath expression   
32.                -->   
33.               <xsl:for-each select="class/employee">   
34.                   <tr>   
35.                      <td>   
36.                         <!-- value-of processing instruction   
37.                         process the value of the element matching the XPath expression   
38.                         -->   
39.                         <xsl:value-of select = "@id"/>   
40.                      </td>   
41.                      <td><xsl:value-of select = "firstname"/></td>   
42.                      <td><xsl:value-of select = "lastname"/></td>   
43.                      <td><xsl:value-of select = "nickname"/></td>   
44.                      <td><xsl:value-of select = "salary"/></td>     
45.                   </tr>   
46.                </xsl:for-each>   
47.             </table>   
48.          </body>   
49.       </html>   
50.    </xsl:template>    
51. </xsl:stylesheet>  

Step2: List the XSLT document to the XML document

Update employee.xml document with the following xml-stylesheet tag. Set href value to employee.xsl

1. <?xml version = "1.0"?>   
2. <?xml-stylesheet type = "text/xsl" href = "employee.xsl"?>   
3. <class>   
4. ...   
5. </class>  

Updated "employee.xml"

1. <?xml version = "1.0"?>  
2. <?xml-stylesheet type = "text/xsl" href = "employee.xsl"?>   
3. <class>   
4.    <employee id = "001">  
5.       <firstname>Aryan</firstname>   
6.       <lastname>Gupta</lastname>   
7.       <nickname>Raju</nickname>   
8.       <salary>30000</salary>  
9.    </employee>   
10.    <employee id = "024">   
11.       <firstname>Sara</firstname>   
12.       <lastname>Khan</lastname>   
13.       <nickname>Zoya</nickname>   
14.       <salary>25000</salary>  
15.    </employee>   
16.    <employee id = "056">   
17.       <firstname>Peter</firstname>   
18.       <lastname>Symon</lastname>   
19.       <nickname>John</nickname>   
20.       <salary>10000</salary>   
21.    </employee>   
22. </class>  

Step3: View the XML document in Internet Explorer

XSLT <xsl:value-of> Element


The XSLT <xsl:value-of> element is used to extract the value of selected node. It puts the value of
selected node as per XPath expression, as text.
1. <xsl:value-of  
2.    select = Expression  
3.    disable-output-escaping = "yes" | "no">  
4. </xsl:value-of>   
5.

Employee.xsl

1. <?xml version = "1.0" encoding = "UTF-8"?>  
2. <xsl:stylesheet version = "1.0"   
3. xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">     
4.    <xsl:template match = "/">   
5.       <html>   
6.          <body>   
7.             <h2>Employee</h2>   
8.             <table border = "1">   
9.                <tr bgcolor = "purple">   
10.                   <th>ID</th>   
11.                   <th>First Name</th>   
12.                   <th>Last Name</th>   
13.                   <th>Nick Name</th>   
14.                   <th>Salary</th>   
15.                </tr>   
16.                <!-- for-each processing instruction   
17.                Looks for each element matching the XPath expression   
18.                -->   
19.                <xsl:for-each select="class/employee">   
20.                   <tr>   
21.                      <td>   
22.                         <!-- value-of processing instruction   
23.                         process the value of the element matching the XPath expression   
24.                         -->  
25.                         <xsl:value-of select = "@id"/>   
26.                      </td>   
27.                      <td><xsl:value-of select = "firstname"/></td>   
28.                      <td><xsl:value-of select = "lastname"/></td>   
29.                      <td><xsl:value-of select = "nickname"/></td>   
30.                      <td><xsl:value-of select = "salary"/></td>     
31.                   </tr>   
32.                </xsl:for-each>   
33.             </table>  
34.          </body>  
35.       </html>   
36.    </xsl:template>    
37. </xsl:stylesheet>  
XSLT <xsl:if> Element
The XSLT <xsl:if> element is used to specify a conditional test against the content of the XML file.

1. <xsl:if test="expression">  
2.   ...some output if the expression is true...  
3. </xsl:if>   

Parameter explanation
test: It specifies a condition in xml data to test.

XSLT <xsl:if> Element Example


Let's take an example to create a table of element with its attribute "id" and its child <firstname>,
<lastname>, <nickname>, and <salary> by iterating over each employee. Let's take a condition
which takes salary to be greater than 15000 and then prints the employee's details.

1. <?xml version = "1.0" encoding = "UTF-8"?>   
2. <xsl:stylesheet version = "1.0"   
3.    xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">  
4.    <xsl:template match = "/">   
5.       <html>   
6.          <body>   
7.             <h2>Employee </h2>   
8.             <table border = "1">   
9.                <tr bgcolor = "pink">   
10.                   <th>ID</th>   
11.                   <th>First Name</th>   
12.                   <th>Last Name</th>   
13.                   <th>Nick Name</th>   
14.                   <th>Salary</th>   
15.                </tr>   
16.        
17.                <xsl:for-each select = "class/employee">   
18.        
19.                   <xsl:if test = "salary > 30000">   
20.                      <tr>>   
21.                         <td><xsl:value-of select = "@id"/></td>   
22.                         <td><xsl:value-of select = "firstname"/></td>   
23.                         <td><xsl:value-of select = "lastname"/></td>  
24.                         <td><xsl:value-of select = "nickname"/></td>>   
25.                         <td><xsl:value-of select = "salary"/></td>  
26.                      </tr>   
27.                   </xsl:if>   
28.                </xsl:for-each>   
29.        
30.             </table>   
31.          </body>   
32.       </html>  
33.    </xsl:template>    
34. </xsl:stylesheet>  

XSLT <xsl:choose> Element


The XSLT <xsl:choose> element is used to specify a multiple conditional test against the content of
nodes with the <xsl:otherwise> and <xsl:when> elements.

1. <xsl:choose>  
2.   <xsl:when test="expression">  
3.     ... some output ...  
4.   </xsl:when>  
5.   <xsl:otherwise>  
6.     ... some output ....  
7.   </xsl:otherwise>  
8. </xsl:choose>  

Parameter explanation
test: It specifies a condition in xml data to test.

XSLT <xsl:choose> Element Example


Let's take an example to create a table of element with its attribute "id" and its child <firstname>,
<lastname>, <nickname>, and <salary> by iterating over each employee. It checks the salary and
then prints the grade details.

1. <?xml version = "1.0" encoding = "UTF-8"?>   
2. <xsl:stylesheet version = "1.0"   
3.    xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">     
4.    <xsl:template match = "/">   
5.       <html>   
6.          <body>   
7.             <h2>Employee</h2>   
8.             <table border = "1">   
9.                <tr bgcolor = "pink">   
10.                   <th>ID</th>   
11.                   <th>First Name</th>   
12.                   <th>Last Name</th>   
13.                   <th>Nick Name</th>   
14.                   <th>Salary</th>   
15.                   <th>Grade</th>   
16.                </tr>   
17.        
18.                <xsl:for-each select = "class/employee">   
19.        
20.                   <tr>   
21.                      <td><xsl:value-of select = "@id"/></td>   
22.                      <td><xsl:value-of select = "firstname"/></td>   
23.                      <td><xsl:value-of select = "lastname"/></td>   
24.                      <td><xsl:value-of select = "nickname"/></td>   
25.                      <td><xsl:value-of select = "salary"/></td>   
26.          
27.                      <td>   
28.                         <xsl:choose>   
29.                            <xsl:when test = "salary > 50000">   
30.                               High   
31.                            </xsl:when>   
32.            
33.                            <xsl:when test = "salary > 40000">   
34.                               Medium   
35.                            </xsl:when>   
36.            
37.                            <xsl:otherwise>   
38.                               Low   
39.                            </xsl:otherwise>   
40.                         </xsl:choose>   
41.                      </td>   
42.                   </tr>   
43.                </xsl:for-each>   
44.             </table>   
45.          </body>   
46.       </html>   
47.    </xsl:template>    
48. </xsl:stylesheet>  

XSLT <xsl:apply-template> Element


The XSLT <xsl:apply-template> element is used to tell XSLT processor to find the appropriate
template to apply according to the type and context of each selected node.

1. <xsl:apply-template   
2.    select = Expression   
3.    mode = QName>  
4. </xsl:apply-template>   

Parameter explanation

Inde Name Description


x

1) selec It is used to process nodes selected by XPath expressions from the list of all
t nodes and its children.

XSLT <xsl:apply-template> Element Example


Let's take an example to create a list of <employee> element with its attribute "id" and its child
<firstname>, <lastname>, <nickname>, and <salary> by iterating over each employee.

1. <?xml version = "1.0" encoding = "UTF-8"?>   
2. <xsl:stylesheet version = "1.0"   
3.    xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">     
4.    <xsl:template match = "/">   
5.       <html>   
6.          <body>   
7.             <h2>Employees</h2>   
8.             <xsl:apply-templates select = "class/employee" />   
9.          </body>   
10.       </html>  
11.    </xsl:template>    
12.   
13.    <xsl:template match = "class/employee">   
14.       <xsl:apply-templates select = "@id" />   
15.       <xsl:apply-templates select = "firstname" />   
16.       <xsl:apply-templates select = "lastname" />   
17.       <xsl:apply-templates select = "nickname" />   
18.       <xsl:apply-templates select = "salary" />   
19.       <br />   
20.    </xsl:template>    
21.    <xsl:template match = "@id">   
22.       <span style = "font-size = 25px;">   
23.          <xsl:value-of select = "." />   
24.       </span>   
25.       <br />   
26.    </xsl:template>    
27.    <xsl:template match = "firstname">   
28.       First Name:<span style = "color:brown;">   
29.          <xsl:value-of select = "." />   
30.       </span>   
31.       <br />   
32.    </xsl:template>    
33.    <xsl:template match = "lastname">   
34.       Last Name:<span style = "color:green;">   
35.          <xsl:value-of select = "." />   
36.       </span>   
37.       <br />   
38.    <</xsl:template>    
39.   
40.    <xsl:template match = "nickname">   
41.       Nick Name:<span style = "color:blue;">   
42.          <xsl:value-of select = "." />   
43.       </span>   
44.       <br />   
45.    </xsl:template>    
46.    <xsl:template match = "salary">   
47.       Marks:<span style = "color:red;">   
48.          <xsl:value-of select = "." />  
49.       </span>   
50.       <br />   
51.    </xsl:template>      
52. </xsl:stylesheet>  

5. EXAMPLE 6:
First create .xml file[demo.xml]
<?xml-stylesheet type="text/xsl" href="demo1.xsl"?>
<menu>
<dvd>
<name>Raju</name>
<actor>chiru</actor>
<place>London</place>
<amount>$00</amount>
</dvd>
<dvd>
<name>Ravi</name>
<actor>pavan</actor>
<place>America</place>
<amount>$00</amount>
</dvd>
</menu>
Demo1.xsl
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<th>name</th>
<th>actor</th>
</tr>
<xsl:for-each select="menu/dvd">
<tr>
<td><xsl:value-of select="name"/>
</td>
<td><xsl:value-of select="actor"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XML WITH CSS:
EXAMPLE 7: [sampcss.xml]
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="student.css"?>
<class>
<student>
<rno>121</rno>
<name>sridhar</name>
<branch>CSE</branch>
<marks>60</marks>
</student>
<student>
<rno>131</rno>
<name>siva ranjini</name>
<branch>IT</branch>
<marks>70</marks>
</student>
<student>
<rno>141</rno>
<name>shruthi</name>
<branch>ECE</branch>
<marks>80</marks>
</student>
<student>
<rno>151</rno>
<name>ramesh</name>
<branch>EEE</branch>
<marks>90</marks>
</student>
<student>
<rno>161</rno>
<name>vandana</name>
<branch>ME</branch>
<marks>40</marks>
</student>
<student>
<rno>171</rno>
<name>babu</name>
<branch>MCA</branch>
<marks>50</marks>
</student>
</class>
DOCUMENT OBJECT MODEL (DOM):

DOM is a set of platform independent and language neutral application


programming interface(API) which describes how to access and manipulate the
information stored in XML or in HTML documents.
XML DOM for loading the XML document.accessing the elements of XML document.
Deleting the elements of XML documents . changing the elements of XML document.
The document object model(DOM) is an application programming interface(API) for HTML
and XML documents. It defines the logical structure of documents and the way a document is
accesses and manipulated. In the DOM specification ,the term “document” is used in the
broadsense-increasingly, XML is b eing used as a way of representing many different kinds of
information that may be stored in diverse systems, and much of this would traditionally be seen
as data rather than as documents. Nevertheless, XML presents this data as documents, and the
DOM may be used to manage this data.
With the document object model, programmers can build documents, navigate their
structure, and add, modify, or delete elements and content. Anything found in an HTML or
XML document can be accessed, changed, deleted, or added using the document object model,
with a few exceptions- in particular; the DOM interfaces for the XML internal and externals
subsets have not yet been specified.
The DOM is separated into 3 different parts/levels:

1. Core DOM- standard model for any structured document.

2. XML DOM- standard model for XML documents.

3. HTML DOM-standard model for HTML documents.


Parsing XML:
There are two ways to parse XML documents

1.Tree based parsing(DOM parser) 2.Event based parser(SAX)

DOM TREE:(for the below HTML program using


JAVASCRIPT) HTML

Body

P
PR

This is para1 This is new para

<html>
<body>
<p>this is para1</p>
<script language='javascript'>
var a=document.createElement("pr");
var b=document.createTextNode("This is new para");
a.appendChild(b);
document.body.appendChild(a);
</script>
</body>
</html>

What is the XML DOM?


(https://www.tutorialspoint.com/dom/xml_
dom_add_node.htm)
The XML DOM is:
A standard object model for XML.
a standard programming interface for XML.
Platform-and language –independent.
A W3C standard

THE XML DOM defines the objects and properties of all XML elements ,and the
methods to access them.
all modern browsers have a build –in XML parser that can be used to read and
manuplate XML.
the parser reads XML into memory and converts it into an XML DOM object that can be
access with java script.
There are some differences between Microsoft’s XML parser and the parsers used in other
browsers. The Microsoft parser supports loading of both XML files and XML strings (text),
while other browsers use separate parsers, all parsers contain functions to traverse XML
trees, access, insert, and delete nodes.
The DOM is a programming API for documents. It closely resembles the structure of
the documents it models. For instance, consider this table, taken from an HTML document:

<TABLE>
<BODY>
<TR>
<TD> shady grove</TD>
<TD>Aeolian</TD>
</TR>
<TR>
<TD> over the river, Charlie</TD>
<TD> dorian</TD>
</TR>
</TBODY>
</TABLE>
The DOM represents this table likes this:
(figure)

Example 8:[overseas.html]
<html>
<body>
<script type =”text/javascript”>
Try
{
xmlDocument=new ActiveXObject(“Microsoft.XMLDOM”);
}
Catch(e)
{
Try
{
xmlDocument=document.implementation.createDocument(“,”null);
}
Catch(e)
{
Alert(e.message)
}
}
Try
{
xmlDocument.async=false;
xmlDocument.load(“example3.xml);
document.write(“xml document example3.xml is loaded”);
}
Catch(e)
{
Alert(e.message);
}
</script>
</body>
</html>
(figure)
using XML processors: DOM AND SAX

Difference between DOM and SAX:

S.NO DOM(Document object model) SAX (simple API for xml parsing)

1 DOM is a tree based parsing SAX is an event based parsing method


method
2 We can insert or delete a node We can insert or delete a node
3 Traverse in any direction Top to bottom traversing
4 Stores the entire XML Parses node by node
document in to memory
before processing.

5 Occupies more memory Doesn’t store the XML in memory


6 DOM preserves comments SAX doesn’t preserve comments.
7 Import javax.xml.parsers.*; Import javax.xml.sax.*; Import
Import org.w3c.dom.*; org.xml.sax.helpers.*;
8 DOM approach is useful for SAX approach is useful for larger
smaller applications applications.

XML and HTML


We can combine XML and HTML into single document. This is possible with XML tag.
The following program explains how data of XML is embedded into HTML table form.
Example 9:
Write a program to combine XML and HTML
Reg.xml
<?xml version="1.0"?>
<members>
<member>
<sno>01</sno>
<sname>Y.ramesh kumar</sname>
<branch>H O D</branch>
</member>
<member>
<sno>02</sno>
<sname>N.suresh</sname>
<branch>MCA</branch>
</member>
<member>
<sno>03</sno>
<sname>ch.santhosh kumar</sname>
<branch>MCA</branch>
</member>
<member>
<sno>04</sno>
<sname>k.ramesh babu</sname>
<branch>MCA</branch>
</member>
<member>
<sno>05</sno>
<sname>srinu</sname>
<branch>IT</branch>
</member>
<member>
<sno>06</sno>

<sname>guru charan</sname>
<branch>IT</branch>
</member>
<member>
<sno>07</sno>
<sname>K SV K srikanth</sname>
<branch>IT</branch>
</member>
<member>
<sno>08</sno>
<sname>Praveen kumar</sname>
<branch>IT</branch>
</member>
<member>
<sno>09</sno>
<sname> santhosh</sname>
<branch> IT</branch>
</member>
<member>
<sno>10</sno>
<sname> murthy</sname>
<branch> IT</branch>
</member>
<member>
<sno> 11</sno>
<sname>suresh </sname>
<branch>IT</branch>
</member>
<member>
<sno>12</sno>
<sname>raju</sname>
<branch>IT</branch>
</member>
<member>
<sno>13</sno>
<sname>praveen</sname>
<branch>IT</branch>
</member>
<member>
<sno>14</sno>
<sname>praneeth</sname>
<branch>IT</branch>
</member>
<member>
<sno>15</sno>
<sname>praveen</sname>
<branch>IT</branch>
</member>
</members>

Regmembers.html
<html>
<head><title>registered members:portal team</title></head>
<body bgcolor="wheat">
<center><font size=5 face="bookman old style" color="blue"> portal team
details</font></center>
<br><br>
<xml id="rRecords" src="D:\Raju\WT Examples\reg.xml"></xml>
<table id ="emptable" width="50%" align="center" border="2" bordercolor="blue"
cellspacing="0" datasrc="#rRecords" dataPageSize="5">
<thead>
<tr style="background-color:cyan ;color:blue;">
<th>S.No.</th>
<th>Name</th>
<th>branch</th>
</tr>
</thead>
<tbody>
<tr>
<td align ="center"><span datafld="sno"></span></td>
<td><span datafld="sname"></span></td>
<td><span datafld="branch"></span></td>
</tr>
</tbody>
</table>
<br><br>
<center>
<input type="button" value="<<first" onClick="emptable.firstPage()">
<input type="button" value="<previous" onClick="emptable.previousPage()">
<input type="button" value="next>" onClick="emptable.nextPage()">
<input type="button" value="last>>" onClick="emptable.lastPage()">
</center>

</body>
</html>

Difference between HTML and XHTML

S.NO HTML XHTML


1 Not case sensitive Case sensitive
2 Need not be paired tag Need to be paired tag
3 No short cut tags. Shortcut tags allowed as
ending tags
4 All attributes may or may not be in All attributes must be in
quotations double quotations

You might also like