WT Unit Ii (HTML&XML)
WT Unit Ii (HTML&XML)
HTML Introduction
Example
<html>
<body>
</body>
</html>
What is HTML?
HTML Tags
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph</p>
</body>
</html>
Example Explained
The text between <html> and </html> describes the web page
The text between <body> and </body> is the visible page content
The text between <h1> and </h1> is displayed as a heading
In this tutorial we use a plain text editor (like Notepad) to edit HTML. We
believe this is the best way to learn HTML.
When you save an HTML file, you can use either the .htm or the .html
extension. We use .htm in our examples. It is a habit from the past, when
the software only allowed three letters in file extensions.
<h1>This is a heading</h1>
<h2>This is a heading</h2>
<h3>This is a heading</h3>
HTML Paragraphs
<p>This is a paragraph</p>
<p>This is another paragraph</p>
HTML Links
An HTML element is everything from the start tag to the end tag:
HTML elements without content are called empty elements. Empty elements
can be closed in the start tag.
<br> is an empty element without a closing tag (it defines a line break).
In XHTML, XML, and future versions of HTML, all elements must be closed.
Adding a slash to the start tag, like <br />, is the proper way of closing
empty elements, accepted by HTML, XHTML and XML.
Even if <br> works in all browsers, writing <br /> instead is more future
proof.
HTML Attributes
HTML links are defined with the <a> tag. The link address is provided as an
attribute:
HTML Comments
Comments can be inserted in the HTML code to make it more readable and
understandable. Comments are ignored by the browser and are not
displayed.
HTML uses tags like <b> and <i> for formatting output, like bold or italic
text.
Tag Description
<b> Defines bold text
<big> Defines big text
<em> Defines emphasized text
<i> Defines italic text
Tag Description
<abbr> Defines an abbreviation
<acronym> Defines an acronym
<address> Defines an address element
<bdo> Defines the text direction
<blockquote> Defines a long quotation
<q> Defines a short quotation
<cite> Defines a citation
<dfn> Defines a definition term
HTML Links
A link is the "address" to a document (or a resource) on the web.
Hyperlinks, Anchors, and Links
Hyperlinks can point to any resource on the web: an HTML page, an image, a
sound file, a movie, etc.
The HTML anchor element <a>, is used to define both hyperlinks and
anchors.
We will use the term HTML link when the <a> element points to a resource,
and the term HTML anchor when the <a> elements defines an address inside
a document..
An HTML Link
Link syntax:
Note: The element content doesn't have to be text. You can link from an
image or any other HTML element.
The target attribute defines where the linked document will be opened.
The code below will open the document in a new browser window:
<a href="http://www.w3schools.com/"
target="_blank">Visit W3Schools!</a>
When the name attribute is used, the <a> element defines a named anchor
inside a HTML document.
Named anchor are not displayed in any special way. They are invisible to the
reader.
Example:
<a href="#tips">
Jump to the Useful Tips Section</a>
<a href="http://www.w3schools.com/html_tutorial.htm#tips">
Jump to the Useful Tips Section</a>
entire table
<cols> Specifies the no of cols in the table
<height> Gives the height of the table
<width> Set the width of the table
<TR>….</TR>
TABLE ROW TAG ATTRIBUTES
<TAG> DESCRIPTION
<align> Horizontal alignment of the text,like
LEFT,CENTER,RIGHT
<bgcolor> Set the background color of the table
<bordercolor> Set the external border color for the
row
<valign> Vertical alignment of the data in this
row.TOP,MIDDLE,BOTTOM
Tables are defined with the <table> tag. A table is divided into rows (with
the <tr> tag), and each row is divided into data cells (with the <td> tag).
The letters td stands for "table data," which is the content of a data cell. A
data cell can contain text, images, lists, paragraphs, forms, horizontal rules,
tables, etc.
<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
If you do not specify a border attribute the table will be displayed without
any borders. Sometimes this can be useful, but most of the time, you want
the borders to show.
To display a table with borders, you will have to use the border attribute:
<table border="1">
<tr>
<td>Row 1, cell 1</td>
<td>Row 1, cell 2</td>
</tr>
</table>
Headings in a Table
<table border="1">
<tr>
<th>Heading</th>
<th>Another Heading</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
Table cells with no content are not displayed very well in most browsers.
<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td></td>
</tr>
</table>
Note that the borders around the empty table cell are missing (NB! Mozilla
Firefox displays the border).
<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td> </td>
</tr>
</table>
you have Internet Explorer 5.0 or newer, you can view a working example in
our XML tutorial.
Table Tags
Tag Description
<table> Defines a table
<th> Defines a table header
<tr> Defines a table row
<td> Defines a table cell
<caption> Defines a table caption
<colgroup> Defines groups of table columns
<col> Defines the attribute values for one or more columns in a table
<thead> Defines a table head
<tbody> Defines a table body
<tfoot> Defines a table footer
Form elements are elements that allow the user to enter information (like
text fields, textarea fields, drop-down menus, radio buttons, checkboxes,
etc.) in a form.
<form>
.
input elements
.
</form>
Input
The most used form tag is the <input> tag. The type of input is specified
with the type attribute. The most commonly used input types are explained
below.
Text Fields
Text fields are used when you want the user to type letters, numbers, etc. in
a form.
<form>
First name:
<input type="text" name="firstname" />
<br />
Last name:
<input type="text" name="lastname" />
</form>
First name:
Last name:
Note that the form itself is not visible. Also note that in most browsers, the
width of the text field is 20 characters by default.
Radio Buttons
Radio Buttons are used when you want the user to select one of a limited
number of choices.
<form>
<input type="radio" name="sex" value="male" /> Male
<br />
<input type="radio" name="sex" value="female" /> Female
</form>
Male
Female
Checkboxes
Checkboxes are used when you want the user to select one or more options
of a limited number of choices.
<form>
I have a bike:
<input type="checkbox" name="vehicle" value="Bike" />
<br />
I have a car:
<input type="checkbox" name="vehicle" value="Car" />
<br />
I have an airplane:
<input type="checkbox" name="vehicle" value="Airplane" />
</form>
I have a bike:
I have a car:
I have an airplane:
When the user clicks on the "Submit" button, the content of the form is sent
to the server. The form's action attribute defines the name of the file to send
the content to. The file defined in the action attribute usually does something
with the received input.
Submit
Username:
If you type some characters in the text field above, and click the "Submit"
button, the browser will send your input to a page called
"html_form_submit.asp". The page will show you the received input.
Form Tags
Tag Description
<form> Defines a form for user input
<input> Defines an input field
<textarea> Defines a text-area (a multi-line text input control)
<html>
<head>
<title>Document name goes here</title>
</head><body>
Visible text goes here...
</body>
</html>
Heading Elements
<h1>Largest Heading</h1>
<h2> . . . </h2>
<h3> . . . </h3>
<h4> . . . </h4>
<h5> . . . </h5><h6>Smallest Heading</h6>
Text Elements
<p>This is a paragraph</p>
<br /> (line break)
<hr /> (horizontal rule)
<pre>This text is preformatted</pre>
Logical Styles
<em>This text is emphasized</em>
<strong>This text is strong</strong>
<code>This is some computer code</code>
Physical Styles
<b>This text is bold</b>
<i>This text is italic</i>
A named anchor:
<a name="tips">Useful Tips Section</a>
<a href="#tips">Jump to the Useful Tips Section</a>
Unordered list
<ul>
<li>First item</li>
<li>Next item</li>
</ul>
Ordered list
<ol>
<li>First item</li>
<li>Next item</li>
</ol>
Definition list
<dl>
<dt>First term</dt>
<dd>Definition</dd>
<dt>Next term</dt>
<dd>Definition</dd>
</dl>
Tables
<table border="1">
<tr>
<th>Tableheader</th>
<th>Tableheader</th>
</tr>
<tr>
<td>sometext</td>
<td>sometext</td>
</tr>
</table>
Frames
<frameset cols="25%,75%">
<frame src="page1.htm" />
Forms
<form action="http://www.nie.com" method="post/get">
<select>
<option>Apples</option>
<option selected="selected">Bananas</option>
<option>Cherries</option>
</select><textarea name="comment" rows="60" cols="20"></textarea>
</form>
Entities
< is the same as <
> is the same as >
© is the same as ©
Other Elements
<blockquote>
Text quoted from a source.
</blockquote>
<address>
Written by <br />
<a href="www.nie.org">Email us</a><br />
Address: Aluguraju Palli,Macherla<br />
Phone: +12 34 56 78
</address>
Frames
With frames, you can display more than one HTML document in the same
browser window. Each HTML document is called a frame, and each frame is
independent of the others.The disadvantages of using frames are:
The <frameset> tag defines how to divide the window into frames
Each frameset defines a set of rows or columns
The values of the rows/columns indicate the amount of screen area
each row/column will occupy
The <frame> tag defines what HTML document to put into each frame
In the example below we have a frameset with two columns. The first column
is set to 25% of the width of the browser window. The second column is set
to 75% of the width of the browser window. The HTML document
"frame_a.htm" is put into the first column, and the HTML document
"frame_b.htm" is put into the second column:
<frameset cols="25%,75%">
<frame src="frame_a.htm">
<frame src="frame_b.htm">
</frameset>
Note: The frameset column size value can also be set in pixels
(cols="200,500"), and one of the columns can be set to use the remaining
space (cols="25%,*").
UNIT-III XML
XML was designed to transport and store data.
HTML was designed to display data.
What is XML?
HTML was designed to display data, with focus on how data looks
HTML is about displaying information, while XML is about carrying
information.
XML is a software- and hardware-independent tool for carrying
information.
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML documents form a tree structure that starts at "the root" and branches
to "the leaves".
XML Documents Form a Tree Structure
XML documents must contain a root element. This element is "the parent"
of all other elements.
The elements in an XML document form a document tree. The tree starts at
the root and branches to the lowest level of the tree.
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
The terms parent, child, and sibling are used to describe the relationships
between elements. Parent elements have children. Children on the same
level are called siblings (brothers or sisters).
All elements can have text content and attributes (just like in HTML).
Example:
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
The root element in the example is <bookstore>. All <book> elements in the
document are contained within <bookstore>.
XML tags are case sensitive. The tag <Letter> is different from the tag
<letter>.
Opening and closing tags must be written with the same case:
<Message>This is incorrect</message>
<message>This is correct</message>
Note: "Opening and closing tags" are often referred to as "Start and end
tags". Use whatever you prefer. It is exactly the same thing.
<note date="12/11/2007">
<to>Tove</to>
<from>Jani</from>
</note>
An XML document contains XML Elements.
other elements
text
attributes
or a mix of all of the above...
<person>
<sex>female</sex>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
XML - DTDs
An XML DTD can be either specified inside the document, or it can be kept in
a separate document and then liked separately.
Syntax
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.
Internal DTD
Syntax
Example
<address>
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
</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.
Several elements are declared here that make up the vocabulary of the
<name> document. <!ELEMENT name (#PCDATA)> defines the
element name to be of type "#PCDATA". Here #PCDATA means parse-able
text data.
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.
Rules
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
Example
Types
System Identifiers
As you can see, it contains keyword SYSTEM and a URI reference pointing to
the location of the document.
Public Identifiers
XML Validation
XML with correct syntax is "Well Formed" XML.
XML validated against a DTD is "Valid" XML.
Well Formed XML Documents
A "Well Formed" XML document has correct XML syntax.
XML documents must have a root element
XML – Schemas
<xs:sequence>
<xs:element name = "name" type = "xs:string" />
<xs:element name = "company" type = "xs:string" />
<xs:element name = "phone" type = "xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The basic idea behind XML Schemas is that they describe the legitimate
format that an XML document can take.
Elements
Elements are the building blocks of XML document. An element can be
defined within an XSD as follows −
<xs:element name = "x" type = "y"/>
Definition Types
You can define XML schema elements in the following ways −
Simple Type:
Simple type element is used only in the context of the text. Some of the
predefined simple types are: xs:integer, xs:boolean, xs:string, xs:date.
For example −
<xs:element name = "phone_number" type = "xs:int" />
Complex Type:
A complex type is a container for other element definitions. This allows you
to specify which child elements an element can contain and to provide some
structure within your XML documents. For example −
Global Types
With the global type, you can define a single type in your document, which
can be used by all other references. For example, suppose you want to
generalize the person and company for different addresses of the company.
In such case, you can define a general type as follows −
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
XSD is extensible while DTD is not. This makes it easier to derive new
elements from existing elements in XSD.
1) DTD stands for Document Type XSD stands for XML Schema Definition.
Definition.
3) DTD doesn't support datatypes. XSD supports datatypes for elements and
attributes.
5) DTD doesn't define order for XSD defines order for child elements.
child elements.
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 XSD provides more control on XML structure.
XML structure.
9) File in DTD is saved as .dtd file File in XSD is saved as .xsd file
10) It uses #PCDATA which is a string It uses fundamental and primitive data types.
data type.
Complete Example for Schema: First you can create note.xsd file and then
link with note.xml
note.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.w3schools.com"
xmlns="https://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
note.xml
<?xml version="1.0"?>
<note
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="note.xsd">
<to>sudhakar</to>
<from>venkat</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML Namespaces
XML Namespace is used to avoid element name conflict in XML document.
Example:
<?xml version="1.0" encoding="UTF-8"?>
<cont:contact xmlns:cont="http://sssit.org/contact-us">
<cont:name>Vimal Jaiswal</cont:name>
<cont:company>SSSIT.org</cont:company>
<cont:phone>(0120) 425-6464</cont:phone>
</cont:contact>
Here
Namespace Prefix: cont
Namespace Identifier: http://sssit.org/contact-us
It specifies that the element name and attribute names with cont prefix
belongs to http://sssit.org/contact-us name space.
Generally these conflict occurs when we try to mix XML documents from
different XML application.
<table>
<tr>
<td>Apple</td>
<td>Banana</td>
</tr>
</table>
<table>
<name>Computer table</name>
<width>80</width>
<length>120</length>
</table>
If you add these both XML fragments together, there would be a name
conflict because both have <table> element. Although they have different
name and meaning.
<h:table>
<h:tr>
<h:td>Apple</h:td>
<h:td>Banana</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>Computer table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
2. By Using xmlns Attribute
You can use xmlns attribute to define namespace with the following
syntax:
<element xmlns:name = "URL">
Example:
<root>
<h:table xmlns:h="http://www.abc.com/TR/html4/">
<h:tr>
<h:td>Apple</h:td>
<h:td>Banana</h:td>
</h:tr>
</h:table>
<f:table xmlns:f="http://www.xyz.com/furniture">
<f:name>Computer table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
In the above example, the <table> element defines a namespace and when
a namespace is defined for an element, the child elements with the same
prefixes are associated with the same namespace.
XML Parsers
XML parser validates the document and check that the document is well
formatted.
Advantages
1) It supports both read and write operations and the API is very simple to
use.
2) It is preferred when random access to widely separated parts of a
document is required.
Disadvantages
1) It is memory inefficient. (Consumes more memory because the whole
XML document needs to load into memory).
2) It is comparatively slower than SAX parser.
XML DOM defines a standard way to access and manipulate XML documents.
We can modify or delete their content and also create new elements. The
elements, their content (text and attributes) are all known as nodes.
<TABLE>
<ROWS>
<TR>
<TD>A</TD>
<TD>B</TD>
</TR>
<TR>
<TD>C</TD>
<TD>D</TD>
</TR>
</ROWS>
</TABLE>
The Document Object Model represents this table like this:
This example parses an XML document (note.xml) into an XML DOM object
and extracts information from it with JavaScript.
note.xml
The HTML file that extracts the data of XML document using DOM.
xmldom.html
<html>
<body>
<h1>Important Note</h1>
<div>
<b>To:</b> <span id="to"></span><br>
<b>From:</b> <span id="from"></span><br>
<b>Message:</b> <span id="message"></span>
</div>
<script>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","note.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
</script>
</body>
</html>
Output:
Important Note
To: [email protected]
From: [email protected]
Message: Hello XML DOM
SAX represents a simple API for XML and a SAX API is implemented by SAX
Parser. This API was called event-based API which provides interfaces on
handlers. There are four handler interfaces. ContentHandler, DTDHandler,
EntityResolver, and ErrorHandler interface. It does not create any internal
structure rather it takes the occurrences of components of an input
document as events, and then it tells the client what it reads as it reads
through the input document. It is suitable for large XML files because it
doesn’t require loading the whole XML file.
Clients does not know what methods to call, they just overrides the
methods of the API and place his own code inside method.
It is an event based parser, it works like an event handler in Java.
Advantages
1) It is simple and memory efficient.
2) It is very fast and works for huge documents.
Disadvantages
1) It is event-based so its API is less intuitive.
2) Clients never know the full information because the data is broken into
pieces.
Difference between DOM and SAX Parsers
SAX Parser DOM Parser
SAX Parser is slower than DOM DOM Parser is faster than SAX
Parser. Parser.
Best for the larger sizes of files. Best for the smaller size of files.
It is suitable for making XML files in It is not good at making XML files in
Java. low memory.
A small part of the XML file is only It loads whole XML documents in
loaded in memory. memory.
What is CSS?
Cascading Style Sheets (CSS) is used to format the layout of a webpage.
With CSS, you can control the color, font, the size of text, the spacing
between elements, how elements are positioned and laid out, what
background images or background colors are to be used, different displays
for different devices and screen sizes, and much more!
Using CSS
CSS can be added to HTML documents in 3 ways:
Inline - by using the style attribute inside HTML elements
Internal - by using a <style> element in the <head> section
External - by using a <link> element to link to an external CSS file
Inline CSS
Example: inlinecss.html
<html>
<body>
</body>
</html>
Internal CSS
The following example sets the text color of ALL the <h1> elements (on that
page) to blue, and the text color of ALL the <p> elements to red. In addition,
the page will be displayed with a "powderblue" background color:
Example: internalcss.html
<!DOCTYPE html>
<html>
<head>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Output: internalcss.html
External CSS
An external style sheet is used to define the style for many HTML pages.
To use an external style sheet, add a link to it in the <head> section of each
HTML page:
Example: externalcss.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
The external style sheet can be written in any text editor. The file must not
contain any HTML code, and must be saved with a .css extension.
Here is what the "styles.css" file looks like:
"styles.css":
body {
background-color: powderblue;
}
h1 {
color: blue;
}
p{
color: red;
}
output: externalcss.html