0% found this document useful (0 votes)
19 views

unit 2 wt full notes

The document provides an overview of CSS (Cascading Style Sheets) and XML (Extensible Markup Language), detailing their syntax, types, and applications. It explains the structure of CSS rules, the differences between inline, internal, and external CSS, as well as the features and uses of XML in data storage and transport. Additionally, it includes examples and guidelines for writing XML documents and elements.

Uploaded by

228r1a0581
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)
19 views

unit 2 wt full notes

The document provides an overview of CSS (Cascading Style Sheets) and XML (Extensible Markup Language), detailing their syntax, types, and applications. It explains the structure of CSS rules, the differences between inline, internal, and external CSS, as well as the features and uses of XML in data storage and transport. Additionally, it includes examples and guidelines for writing XML documents and elements.

Uploaded by

228r1a0581
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/ 71

Scanned by CamScanner

Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
CSS stands for Cascading Style Sheets. It is a style sheet language used to style and enhance
website presentation.

CSS (Cascading Style Sheets) is a language designed to simplify the process of making web
pages presentable.

• It allows you to apply styles to HTML documents by prescribing colors, fonts, spacing,
and positioning.
• The main advantages are the separation of content (in HTML) and styling (in CSS) and
the same CSS rules can be used across all pages and not have to be rewritten.
• HTML uses tags and CSS uses rule sets.
• CSS styles are applied to the HTML element using selectors.

CSS Syntax

CSS consists of style rules that are interpreted by the browser and applied to the corresponding
elements.

A style rule set includes a selector and a declaration block.

Selector: Targets specific HTML elements to apply styles.

Declaration: Combination of a property and its corresponding value.

The declaration block contains one or more declarations separated by semicolons.

Each declaration includes a CSS property name and a value, separated by a colon.

Multiple CSS declarations are separated with semicolons, and declaration blocks are surrounded
by curly braces.

Example:
// CSS Style

h1 { color: blue; font-size: 12px; }


Where -

Selector - h1

Declaration - { color: blue; font-size: 12px; }

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

Types of CSS (Cascading Style Sheet)

CSS (Cascading Style Sheets) is used to style and layout of web pages, and controlling the
appearance of HTML elements. CSS targets HTML elements and applies style rules to dictate
their appearance.

Below are the types of CSS:

1. Inline CSS
2. Internal or Embedded CSS
3. External CSS

1. Inline CSS
• Inline CSS involves applying styles directly to individual HTML elements using
the style attribute.
• This method allows for specific styling of elements within the HTML document,
overriding any external or internal styles.

Example:

<html>
<body>
<p style="color:green;font-size:50px;font-style:italic;text-align:center;">
This is the demo on inline CSS
css-Cascading Style Sheet
</p>
</html>
Pros of inline CSS:

• We can create CSS rules on the HTML page.

Cons of inline CSS:

• Inline CSS, adding CSS rules to HTML elements is time-consuming and messes up the
HTML structure.

• It styles multiple elements at the same time which can affect the page size and download
time of the page.

2. Internal or Embedded CSS

Internal or Embedded CSS is defined within the HTML document’s <style> element.
It applies styles to specified HTML elements.
The CSS rule set should be within the HTML file in the head section i.e. the CSS is embedded
within the <style> tag inside the head section of the HTML file.
Example:
<html>
<head>
<style>
body{background-color: black;}
h2 {color: red;padding: 50px; }
p{color:blue}
</style>
</head>
<body>
<h2>CSS types</h2>
<p>Cascading Style sheet types: inline, external and internal</p>
</body>
</html>

Pros of internal css:Adding code in the HTML document will reduce the page size and loading
time of the webpage.

Cons of internal css:

Internal CSS cannot upload multiple files when we add the code with the HTML page

3. External CSS
External CSS contains separate CSS files that contain only style properties with the help of tag
attributes (For example class, id, heading, … etc). CSS property is written in a separate file with
a .css extension and should be linked to the HTML document using a link tag. It means that, for
each element, style can be set only once and will be applied across web pages.

Example:

Write a separate file containing the styles and save it with .css extension

style.css

body{background-color: black;}

h2 {color: red;padding: 50px; }

p{color:blue}

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

write a html file with link tag and ‘href’ attributee

<!DOCTYPE html>

<html>

<head>

<link rel="stylesheet" href="style.css">

</head>

<body>

<h2>CSS types</h2>

<p>Cascading Style sheet types: inline, external and internal</p>

</body>

</html>

Output:
Pros of External CSS:

• Our files have a cleaner structure and smaller in size.

• We use the same .css file for multiple web pages in external CSS.

Cons of External CSS:

• The pages cannot be delivered correctly before the external CSS is loaded.

• In External CSS, uploading many CSS files can increase the download time of a website.

Note: Inline CSS has the highest priority, so it overrides internal and external styles.
Internal CSS comes next, overriding external styles, while external CSS is applied only if
no inline or internal styles are set.

--------------------------------------
INDEX

Sl no CONTENT Pg no
1 Introduction to XML 1-2
• Features of XML
• Applications of XML
2 Defining XML tags, their attributes and values 2-8
• Structure of XML document
3 Document Type Definition (DTD) 8-11

• Internal DTD
• External DTD
• Limitations of DTD
4 XML Schemas 11-13
• XML XSD(XML Schema Definition)
5 Document Object Model 14-17
• XML DOM
6 17-24
XHTML Parsing XML Data
DOM Parser
• Features of DOM Parser
• Advantages of DOM Parser
• Disadvantages of DOM Parser
SAX Parser
• Features of SAX Parser
• Advantages of SAX Parser
• Disadvantages of SAX Parser
INDEX FOR PROGRAMS

CODE No. CODE DESCRIPTION Pg no.

C 2.1 Sample XML Document 4

C 2.2 XML code to demonstrate XML Elements 5

C 2.3 XML code to demonstrate XML Attributes 7

C 2.4 XML Internal DTD 9

C 2.5 XML External DTD 10

C 2.6 XML Schema 11

C 2.7 XML code for XSD 12

C 2.8 XML code for storing Books Information 16

C 2.9 Java program to parse XML elements using DOM 18

parser

C 2.10 XML employee file to be passed to the java program 20

C 2.11 Code snippet for importing classes for SAX parser 22

C 2.12 Code snippet for Setting up the I/O 22

C 2.13 Code snippet for Implementing the Content Handler 22

C 1.14 Code snippet for Handling Content Events 23

C 1.15 Code snippet for Setting up the SAX Parser 23


CSE Dpet., CMREC

Introduction to XML
▪ XML stands for EXtensible Markup Language

▪ XML is a markup language much like HTML.

▪ XML was designed to describe data not to display data

▪ XML tags are not prédéfinie in XML,we need to define Our own tags.

▪ XML uses a DTD (Document Type Definition) to formally describe the data.

▪ XML was designed to store and transport data.

▪ XML user to define the formatting rules for the user defined tags.

▪ XML was designed to be both human and machine readable.

▪ XML is platform independent and language independent.( language neutral)

▪ XML became a World Wide Web Consortium (W3C)Recommendation on February 10, 1998.

Markup language: A markup language is a modern system for highlight or underline a document.

Features of XML/ Applications for XML

1) XML separates data from HTML

• With XML, data can be stored in separate XML files. This way separate focus on using
HTML/CSS for display and layout is possible, and changes in the underlying data will not
require any changes to the HTML.
• With a few lines of JavaScript code, an external XML file can be read and the data
content of web page can updated.

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.

Page 1
CSE Dpet., CMREC

3) XML simplifies data transport

• One of the most time-consuming challenges for developers is to exchange data between
incompatible systems over the Internet.
• Exchanging data as XML greatly reduces this complexity, since the data can be read by
different incompatible applications.

4) XML simplifies Platform change

• Upgrading to new systems (hardware or software platforms), is always time consuming.


Large amounts of data must be converted and incompatible data is often lost.
• XML data is stored in text format. This makes it easier to expand or upgrade to new
operating systems, new applications, or new browsers, without losing data.

5) XML increases data availability

• Different applications can access your data, not only in HTML pages, but also from XML
data sources.
• With XML, your data can be available to all kinds of "reading machines" (Handheld
computers, voice machines, news feeds, etc), and make it more available for blind people,
or people with other disabilities.

6) XML can be used to create new internet languages

• A lot of new Internet languages are created with XML.


Example :XHTML (Extensible HyperText Markup Language), MathML (Mathematical Markup Language), SVG
(Scalable Vector Graphics), RSS (Really Simple Syndication), RDF (Resource Description Framework), and KML
(Keyhole Markup Language)

Structure of XML Documents

All elements can have sub elements (child elements).


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

Page 2
CSE Dpet., CMREC

Fig 1.XML Tree Structure

An XML document comprises of the following basic units:


▪ Element: includes the start-tag, the enclosing character data and/or nested elements, and the

end-tag.

▪ Attribute: defined in the start-tag to provide extra information about the element, in the form
of attribute_name="attribute_value".
▪ Entities References: in the form of &name;, e.g., &lt; (<), &gt; (>), &amp; (&), &quot; ("),
and &apos; (').
▪ Character References: in the form of &#decimal-number; or &#xhex-code; for replacing any
Unicode character, e.g., both &#169; and &#xA9; can be used for copyright symbol ©.

▪ PCDATA (Parsed Character Data): Text between start-tag and end-tag that will be examined
by the parser for entity references and nested elements.

▪ CDATA (Character Data): Text between start-tag and end-tag that will NOT be examined
by the parser for entity references and nested tags.

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

Page 3
CSE Dpet., CMREC

The terms parent, child, and sibling are used to describe the relationships between elements.

Parent have children. Children have parents. Siblings are children on the same level (brothers
and sisters).

C 2.1Sample XML Document

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

Explanation:

The first line is the XML declaration. This line is called the XML prolog.

The XML prolog is optional. If it exists, it must come first in the document.
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"):

The next 4 lines[3,4,5,6 lines] describe 4 child elements of the root (to, from, heading, and body)
And finally the last line defines the end of the root element.

Page 4
CSE Dpet., CMREC

XML Element

• 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:
Following is the syntax to write an XML element:
<element-name attribute1 attribute2>
....content
</element-name>
Where,
element-name is the name of the element. The name its case in the start and end tags must match.

An element can contain:

• text
• attributes
• other elements
• or a mix of the above

C 2.2 XML file to demonstrate elements

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

Page 5
CSE Dpet., CMREC

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

XML Elements Rules


Following rules are required to be followed for XML elements:
• An element name can contain any alphanumeric characters. The only punctuation
mark allowed in names are the hyphen (-), under-score (_) and period (.).
• Names are case sensitive. For example, Address, address, and ADDRESS are
different names.
• Start and end tags of an element must be identical.
An element, which is a container, can contain text or elements as seen in the above
example.

XML Attribute
▪ Attribute: defined in the start-tag to provide extra information about the element,
in the form of attribute_name="attribute_value",they define properties of elements.
An XML attribute is always a name-value pair.

Syntax:
An XML attribute has the following syntax:
<element-name attribute1 attribute2 >
....content..
< /element-name>
Where,

attribute1, attribute2 are attributes of the element separated by white spaces.


An attribute defines a property of the element. It associates a name with a value, which is a string
of characters.
An attribute is written as:
name = "value"
name is followed by an = sign and a string value inside double (" ") or single (' ') quotes.

Page 6
CSE Dpet., CMREC

C 2.3 XML file to demonstrate atttributes

<bookstore>
<book category="children">
<title>Harry Potter</title>
<?xml version="1.0"
<author>J encoding="UTF-8"?>
K. Rowling</author>
<!DOCTYPE
<year>2005</year> [
garden
<!ELEMENT garden (plants)*>
<price>29.99</price>
<!ELEMENT
</book> plants (#PCDATA)>
<!ATTLIST plants category CDATA #REQUIRED>
<book category="web">
]><title>Learning XML</title>
<garden>
<author>Erik T. Ray</author>
<plants category="flowers" />
<year>2003</year>
<plants category="shrubs">
<price>39.95</price>
</plants>
</book>
</garden>
</bookstore>

Attribute Types
Attribute Type Description
StringType It takes any literal string as a value. CDATA is a StringType. CDATA
is character data. This means, any string of non-markup characters
is a legal part of the attribute.
TokenizedType This is a more constrained type. The validity constraints noted in the
grammar are applied after the attribute value is normalized. The
TokenizedType attributes are given as:
ID: It is used to specify the element as unique.
IDREF: It is used to reference an ID that has been named for another
element.
IDREFS: It is used to reference all IDs of an element.
ENTITY: It indicates that the attribute will represent an external
entity in the document.
ENTITIES: It indicates that the attribute will represent external
entities in the document.
NMTOKEN: It is similar to CDATA with restrictions on what data
can be part of the attribute.
NMTOKENS: It is similar to CDATA with restrictions on what data
can be part of the attribute.
EnumeratedType This has a list of predefined values in its declaration, out of which, it
must assign one value. There are two types of enumerated attribute:
Notation Type: It declares that an element will be referenced to a
NOTATION declared somewhere else in the XML document.
Enumeration: Enumeration allows you to define a specific list of
values that the attribute value must match.

Page 7
CSE Dpet., CMREC

Element Attribute Rules


Following are the rules that need to be followed for attributes:
❖ 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 (<).

Problems with usage of attributes:


▪ Attributes cannot contain multiple values (elements can)
▪ Attributes are not expandable (for future changes)
▪ Attributes cannot describe structures (like child elements can)
▪ Attributes are more difficult to manipulate by program code
▪ Attribute values are not easy to test against a dtd

Document Type Definition:


❖ Document Type Definition (DTD) is used to define the structure of an XML document.
❖ It describes the objects (such as elements, attributes, entities) and the relationship of the
objects.

❖ DTD defines the structure and legal elements and attributes of an XML document.
❖ DTDs check vocabulary and validity of the structure of XML documents against
grammatical rules of appropriate XML language.
❖ It specifies a set of constraints and establishes the trees that are acceptable in an XML
document.

Syntax
Basic syntax of a DTD is as follows:
<!DOCTYPE element DTD identifier
[
declaration1
declaration2
........
]>
In the above syntax,
❖ The DTD starts with <!DOCTYPE delimiter.
❖ An element tells the parser to parse the document from the specified root element.
❖ DTD identifier is an identifier for the document type definition, which may be the

Page 8
CSE Dpet., CMREC

path to a file on the system or URL to a file on the internet.


❖ If the DTD is pointing to external path, it is called External Subset.
❖ The square brackets [ ] enclose an optional list of entity declarations called Internal
Subset.

DTD-XML Building Blocks :


From a DTD point of view,all XML documents are made up by the following building blocks
i)Elements
ii)Attributes
iii)Entities
iv)PCDATA
V)CDATA.

i)Elements :
the syntax of declaring Element
< !ELEMENT element_name category>

Or
< !ELEMENT element_name (element-content)>

Page 9
In above example : element name is payment ;
attribute name is type ; attribute type is CDATA ; attribute value is Check

Page 10
Page 11
Page 12
Internal DTD
A DTD is referred to as an internal DTD, if elements are declared within the XML files.
To refer it as internal DTD, standalone attribute in XML déclaration must be set to « yes ».
This means, the declaration works independent of an external source.
Syntax
Following is the syntax of internal DTD:
<!DOCTYPE root-element [element-declarations]>
where root-element is the name of root element and element-declarations is where you
declare the elements.

C 2.4 XML internal DTD

address.xml

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


<!DOCTYPE address [
<!ELEMENT address (name,company,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
]>
<address>
<name>uma</name>
<company>cmec</company>
<phone>(011) 123-4567</phone>
</address>

Start Declaration - Begin the XML declaration with the following statement.
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
DTD - Immediately after the XML header, the document type declaration follows,
commonly referred to as the DOCTYPE:
<!DOCTYPE address [
The DOCTYPE declaration has an exclamation mark (!) at the start of the element name.
The DOCTYPE informs the parser that a DTD is associated with this XML document.
DTD Body - The DOCTYPE declaration is followed by the body of the DTD, where you
declare elements, attributes, entities, and notations.
<!ELEMENT address (name,company,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT phone_no (#PCDATA)>

End Declaration - Finally, the declaration section of the DTD is closed using a closing bracket and
a closing angle bracket (]>). This effectively ends the definition, and thereafter, the XML
document follows immediately.

Page 13
If the xml is follows the structure that is specified then it is called as valid XML otherwise it
is called as invalid XML
To check wether above xml is valid or not we will write an PHP file as follows

<?php
$xml=new DOMDocument;
$xml->load('address.xml');
if($xml->validate())
{
echo "it is valid document";
}
else
{
echo "it is invalid document";
}
?>
Output :
it is valid document

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

Page 14
Page 15
In external DTD elements are declared outside the XML file. They are
accessed by specifying the system attributes which may be either the legal .dtd file or a valid URL.
To refer it as external DTD, standalone attribute in the XML declaration must be set as no. This
means, declaration includes information from the external source.
Syntax
Following is the syntax for external DTD:
<!DOCTYPE root-element SYSTEM "file-name">
where file-name is the file with .dtd extension.

Page 16
C 2.5 XML External DTD

The content of the DTD file address.dtd is as shown:


<!ELEMENT address (name,company,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
---------------------------------------------------------------------------------------------------------
Add.xml
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE address SYSTEM "address.dtd">
<address>
<name>uma/name>
<company> cmrec</company>
<phone>(011) 123-4567</phone>
</address>

<?php
$xml=new DOMDocument;
$xml->load('Add.xml');
if($xml->validate())
{
echo "it is valid document";
}
else
{
echo "it is invalid document";
}
?>

Page 17
Limitations of DTD:
❖ DTD has its own syntax (which is inherited from SGML DTD) and requires a dedicate

processing tool to process the content. It does not use XML syntax and XML processor.

❖ DTD does not support object-oriented concepts such as hierarchies and inheritance.

❖ DTD's data type is limited to text string; and does not support other data types like
number, date etc.

❖ DTD does not support namespaces.


DTD's occurrence indicator is limited to 0, 1 and many; cannot support a specific number
such as 8.

Page 18
XML Schemas

XML Schema is commonly known as XML Schema Definition (XSD).

• XML Schemas are written in XML


• XML Schemas are extensible to additions
• XML Schemas support data types
• XML Schemas support namespaces
• It is used to describe and validate the structure and the content of XML data.
• XML schema defines the elements, attributes, and data types.
• It is similar to a database schema that describes the data in a database.
• XML Schema is an XML-based alternative to DTD.

Syntax

You need to declare a schema in your XML document as follows:


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

XML Schema Data types

There are two types of data types in XML schema.

1. SimpleType

2. ComplexType

SimpleType

The simpleType allows you to have text-based elements. It contains less attributes, child elements, and cannot
be left empty.
Example : xs :integer , xs :boolean , xs :string ; xs :date
<xs:element name="phone" type="xs:int" />
ComplexType

The complexType is a container for other elements definations.Ihis allows you to hold multiple attributes sub
elements for an element . It can contain additional sub elements and can be left empty.
<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>

XML schema
employee.xsd
<?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>
Description of XML Schema for the above code

</xs:element>
<xs:element name=" contact "> : It defines the element name contact.

<xs:complexType> : It defines that the element ' contact ' is complex type.
</xs:schema>

<xs:sequence> : It defines that the complex type is a sequence of elements.


<xs:element name="name" type="xs:string"/> : It defines that the element 'name' is of string/text
type.

<xs:element name="company" type="xs:string"/> : It defines that the element ' company ' is of
string/text type.

<xs:element name="phone" type="xs:int"/> : It defines that the element ' phone ' is of
string/integer type.

The basic idea behind XML Schemas is that they describe the acceptable format that an
XML document can take.

An element can be defined within an XSD as follows:


<xs:element name="x" type="y"/> the xml file using XML schema or XSD file.

C 2.7 XML code for XSD

employee.xml

<?xml version="1.0"?>

<contact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="employee.xsd">

<name>uma</name>

<company>cmrec</company>

<phone>123456789</phone>

</contact>

PHP program to valid XML as per XSD


<?php
$xml = new DOMDocument();
$xml->load('employee.xml');
if (!$xml->schemaValidate('employee.xsd'))
{
print 'invalid xml';
}
else
{
print 'valid xml';
}
?>
DTD vs XSD
There are many differences between DTD (Document Type Definition) and XSD (XML Schema Definition). In short, DTD
provides less control on XML structure whereas XSD (XML schema) provides more control.

The important differences are given below:

No. DTD XSD

1) DTD stands for Document Type Definition. XSD stands for XML Schema Definition.

2) DTDs are derived from SGML syntax. XSDs are written in XML.

3) DTD doesn't support datatypes. XSD supports datatypes for elements and attributes.

4) DTD doesn't support namespace. XSD supports namespace.

5) DTD doesn't define order for child elements. XSD defines order for child elements.

6) DTD is not extensible. XSD is extensible.

7) DTD is not simple to learn. XSD is simple to learn because you don't need to learn new
language.

8) DTD provides less control on XML structure. XSD provides more control on XML structure.

CDATA vs PCDATA
CDATA
CDATA: (Unparsed Character data): CDATA contains the text which is not parsed further in an XML document. Tags inside
the CDATA text are not treated as markup and entities will not be expanded.Let's take an example for CDATA:

1. <?xml version="1.0"?>
2. <!DOCTYPE employee SYSTEM "employee.dtd">
3. <employee>
4. <![CDATA[
5. <firstname>uma</firstname>
6. <lastname>desa</lastname>
7. <email>uma.vishwam@cmrec.ac.in</email>
8. ]]>
9. </employee>
In the above CDATA example, CDATA is used just after the element employee to make the data/text unparsed, so it will
give the value of employee:

<firstname>uma</firstname>
<lastname>desa</lastname>
<email>uma.vishwam@cmrec.ac.in</email>
PCDATA
PCDATA: (Parsed Character Data): XML parsers are used to parse all the text in an XML document. PCDATA stands for
Parsed Character data. PCDATA is the text that will be parsed by a parser. Tags inside the PCDATA will be treated as
markup and entities will be expanded.In other words you can say that a parsed character data means the XML parser
examine the data and ensure that it doesn't content entity if it contains that will be replaced.

Let's take an example:

1. <?xml version="1.0"?>
2. <!DOCTYPE employee SYSTEM "employee.dtd">
3. <employee>
4. <firstname>uma</firstname>
5. <lastname>desa</lastname>
6. <email>uma.vishwam@cmrec.ac.in</email>
7. </employee>
Output :

uma
desa
uma.vishwam@cmrec.ac.in
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Document Object Model

❖ The Document Object Model (DOM) is the foundation of XML.


❖ The DOM defines a standard for accessing and manipulating documents.
❖ "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."
❖ The HTML DOM defines a standard way for accessing and manipulating HTML
documents. It presents an HTML document as a tree-structure.
❖ The XML DOM defines a standard way for accessing and manipulating XML
documents. It presents an XML document as a tree-structure.
❖ A DOM document is a collection of nodes or pieces of information organized in a
hierarchy.This hierarchy allows a developer to navigate through the tree looking for
specific information. Because it is based on a hierarchy of information, the DOM is
said to be tree based.
HTML code for table

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

The Equivalent Document Object Model representation for the above html code.

Fig 2: DOM representation for the above HTML code


CSE Dpet., CMREC

The XML DOM example.


XML code (Books.xml)

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


<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>

The equivalent DOM representation for the above XML code

Fig 3: DOM representation for the above XML code


CSE Dpet., CMREC

This below code retrieves the text value of the first <title> element in an XML document:

txt=xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;

XHTML Parsing XML Data– DOM and SAX Parsers in java.

XML parser is a software library or a package that provides interface for client applications to

work with XML documents. It checks for proper format of the XML document and may also

validate the XML documents. Modern day browsers have built-in XML parsers.

Types of XML Parsers

These are the two main types of XML Parsers:

1. DOM (Document Object Model)

2. SAX (Simple API for XML)

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.

Features of DOM Parser

DOM is an object-oriented API. The DOM parser explicitly builds an object model, in the form of
a tree structure, to represent an XML document. Your application can then manipulate the nodes
in the tree. DOM is a platform- and language-independent interface for processing XML
documents. The DOM API defines the mechanism for querying, traversing and manipulating the
object model built.

Page 19
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. DOM Parser has a tree based structure.

Page 20
CSE Dpet., CMRE

Fig 4: Working DOM parser

C 2.9 Java program to parse XML elements using DOM parser.

import org.w3c.dom.*;

import javax.xml.parsers.*;

import java.io.*;

import java.util.Scanner;

public class ReadXML {

public static void main(String a[]) throws Exception{

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();

//Build Document

Document document = builder.parse(new


File("C:\\Users\\Naresh\\Desktop\\employees.xml"));

--PTO--

Page 21
CSE Dpet., CMREC

--Contd…

//Normalize the XML Structure; It's just too important !!

document.getDocumentElement().normalize();

//Here comes the root node

Element root = document.getDocumentElement();

//Get all employees

NodeList nList = document.getElementsByTagName("employee");

System.out.println("enter employee id:");

Scanner s=new Scanner(System.in);

String id=s.next();

for (int temp = 0; temp < nList.getLength(); temp++)

Node node = nList.item(temp);

if (node.getNodeType() == Node.ELEMENT_NODE)

Element eElement = (Element) node;

if(eElement.getAttribute("id").equals(id)){

System.out.println("First Name : " +


eElement.getElementsByTagName("firstName").item(0).getTextContent());

System.out.println("Last Name : " +


eElement.getElementsByTagName("lastName").item(0).getTextContent());

System.out.println("Location : " +
eElement.getElementsByTagName("location").item(0).getTextContent());

}}}}}

Page 22
CSE Dpet., CMREC

C 2.10 XML employee file to be passed to the above java program

<employees>

<employee id="111">

<firstName>Naresh</firstName>

<lastName>Gupta</lastName>

<location>India</location>

</employee>

</employees>

OUTPUT:
First Name: Naresh
Last Name Gupta

Location India

Advantages of DOM parser

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 of DOM parser

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.

Page 23
CSE Dpet., CMREC

SAX (Simple API for XML)

A SAX Parser implements SAX API.

Features of SAX Parser

SAX is an event-driven API. The SAX API defines a number of callback methods, which will be
called when events occur during parsing. The SAX parser reads an XML document and generate
events as it finds elements, attributes, or data in the document. There are events for document start,
document end, element start-tags, element end-tags, attributes, text context, entities, processing
instructions, comments and others.

It does not create any internal structure.

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

Fig 5: Working SAX parser

Page 24
CSE Dpet., CMREC

C 2.11 Code snippet for importing classes required for SAX parser

package sax;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

import java.util.*;
import java.io.*;

public class SAXLocalNameCount {


// ...
}

C 2.12 Code snippet for Setting up the I/O

static public void main(String[] args) throws Exception {


String filename = null;

for (int i = 0; i < args.length; i++) {


filename = args[i];
if (i != args.length - 1) {
usage();
}
}

if (filename == null) {
usage();
}
}

C 2.13 Code snippet for Implementing the Content Handler

public class SAXLocalNameCount extends DefaultHandler {


// ...
}

Page 25
CSE Dpet., CMREC

C 2.14 Code snippet for Handling Content Events

public class SAXLocalNameCount extends DefaultHandler {

private Hashtable tags;

public void startDocument() throws SAXException {


tags = new Hashtable();

public void endDocument() throws SAXException {


Enumeration e = tags.keys();
while (e.hasMoreElements()) {
String tag = (String)e.nextElement();
int count = ((Integer)tags.get(tag)).intValue();
System.out.println("Local Name \"" + tag + "\" occurs "
+ count + " times");

private static String convertToFileURL(String filename) {


// ...

// ...

C 2.15 Code snippet for Setting up the SAX Parser

static public void main(String[] args) throws Exception {

// Code to parse command-line arguments


//(shown above)
// ...

SAXParserFactory spf = SAXParserFactory.newInstance();


spf.setNamespaceAware(true);
SAXParser saxParser = spf.newSAXParser();
}

Page 26
CSE Dpet., CMREC

Advantages of SAX parser

1) It is simple and memory efficient.

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

Disadvantages of SAX parser

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.

Page 27
XHTML Introduction

XHTML or EXtensible HyperText Markup Language is a mix of HTML and XML, very
similar to HTML but stricter. It’s like a rulebook for creating web pages that browsers easily
understand. Unlike HTML, you have to be careful and follow the rules exactly. Most browsers
support it. Just think of it as a more precise way to write web code.

History
It was developed by the World Wide Web Consortium (W3C) and helps web developers
transition from HTML to XML. With XHTML, developers can enter the XML world with all its
features while still ensuring backward and future compatibility of the content. The XHTML
family includes three document types; the first is XHTML 1.0, which was recommended by
W3C on January 26, 2000. The second is XHTML 1.1, which was recommended by W3C on
May 31, 2001.
The third is XHTML5, a standard used for developing an XML adaptation of the HTML5
specification. An XHTML document must have an XHTML <!DOCTYPE> declaration.

Elements of XHTML:

XHTML Element Description

Used to declare the Document Type


Definition (DTD), specifying the rules for
<!DOCTYPE>
the markup language, ensuring proper
rendering in browsers.

Encloses the entire HTML or XHTML


<html>
document, serving as the root element.

Contains meta-information about the


document, such as the title, character set,
<head>
linked stylesheets, and other essential
elements.

Nested within the head section, specifies the


<title> title of the document, displayed in the
browser’s title bar or tab.
XHTML Element Description

Encloses the content of the web page,


including text, images, links, and other
<body> HTML elements. It represents the visible
part of the document displayed in the
browser.

When creating an XHTML web page, it is necessary to include a DTD (Document Type
Definition) declaration. There are three types of DTD which are discussed below:

Transitional DTD:
It is supported by the older browsers which do not have inbuilt cascading style sheets supports.
Several attributes are enclosed in the body tag which are not allowed in strict DTD.

Syntax:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

Example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-
transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Transitional DTD XHTML</title>
</head>

<body bgcolor="#dae1ed">
<div style="color:#090;font-size:40px;
font-weight:bold;text-align:center;
margin-bottom:-25px;">GeeksforGeeks</div>
<p style="text-align:center;font-size:20px;">
A computer science portal</p>
<p style="text-align:center;font-size:20px;">
Option to choose month:
<select name="month">
<option selected="selected">January</option>
<option>February</option>
<option>March</option>
<option>April</option>
<option>May</option>
<option>June</option>
<option>July</option>
<option>Augusy</option>
<option>September</option>
<option>October</option>
<option>November</option>
<option>December</option>
</select>
</p>
</body>

</html>

Strict DTD:
Strict DTD is used when XHTML page contains only markup language. Strict DTD is used
together with cascading style sheets, because this attribute does not allow CSS property in
body tag.

Syntax:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

Example
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-
strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Strict DTD XHTML</title>
</head>

<body>
<div style="color:#090;font-size:40px;
font-weight:bold;text-align:center;
margin-bottom:-25px;">GeeksforGeeks</div>
<p style="text-align:center;font-size:20px;">
A computer science portal</p>
<p style="text-align:center;font-size:20px;">
Option to choose month:
<select name="month">
<option selected="selected">January</option>
<option>February</option>
<option>March</option>
<option>April</option>
<option>May</option>
<option>June</option>
<option>July</option>
<option>Augusy</option>
<option>September</option>
<option>October</option>
<option>November</option>
<option>December</option>
</select>
</p>
</body>

</html>
Frameset DTD:
The frameset DTD is used when XHTML page contains frames. This DTD is identical to the
HTML 4.01 Transitional DTD except for the content model of the HTML element.

Syntax:
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
"DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
Example
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xml:lang="en" lang="en">

<head>
<title>Frameset DTD XHTML</title>
</head>
<frameset cols="30%, 20%, *">
<frameset rows="40%, 30%, *">
<frame src="gfg.html" />
<frame src="gfg1.html" />
<frame src="geeks.html" />
</frameset>
<frameset rows="40%, 60%">
<frame src="g4g.html" />
<frame src="g4g1.html" />
</frameset>
<frameset rows="20%, 20%, 30%, *">
<frame src="geeksforgeeks.html" />
<frame src="geeksforgeeks1.html" />
<frame src="geeksforgeeks2.html" />
<frame src="geeksforgeeks3.html" />
</frameset>
</frameset>
</html>

Why use XHTML?


• XHTML documents are validated with standard XML tools.
• It is easily to maintain, convert, edit document in the long run.
• It is used to define the quality standard of web pages.
• XHTML is an official standard of the W3C, your website becomes more compatible and
accurate with many browsers.
Benefits of XHTML:
• All XHTML tags must have closing tags and are nested correctly. This generates cleaner
code.
• XHTML documents are lean which means they use less bandwidth. This reduces cost
particularly if your web site has 1000s of pages.
• XHTML documents are well formatted well–formed and can easily be transported to
wireless devices,
• All new developments will be in XML (of which XHTML is an application).
• XHTML works in association with CSS to create web pages that can easily be updated.

Difference Between HTML and XHTML:

HTML XHTML

XHTML (Extensible HyperText Markup


HTML or HyperText Markup Language is Language) is a family of XML markup
the main markup language for creating web languages that mirror or extend versions of
pages the widely used Hypertext Markup
Language (HTML)

Flexible framework requiring lenient Restrictive subset of XML which needs to


HTML specific parser be parsed with standard XML parsers

World Wide Web Consortium


Proposed by Tim Berners-Lee in 1987
Recommendation in 2000.

Application of Standard Generalized


Application of XML
Markup Language (SGML).
HTML XHTML

Extended from SGML. Extended from XML, HTML

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy