You are on page 1of 143

Grading Scheme

Internet &
Intranet Applications

5 Homework (30%)
A midterm exam (30%)
November 16th 2007

Binnur Kurt, PhD

A final exam (40%)

bkurt@itu.edu.tr

Academic dishonesty including but not limited to


cheating, plagiarism, collaboration is unacceptable and
subject to disciplinary actions. Any student found guilty
will have grade -100. Assignments are due in class on the
due date. Late assignments will NEVER be accepted.

Istanbul Technical University


Computer Engineering Department

BTE550 Internet and Intranet Applications

Copyleft
Copy
left 2005-2007

version 1.3

About the Lecturer


BSc, MSc, PhD
ITU, Computer Engineering 1995, 1997, 2007
Areas of Interest

Tell me and I forget.


Show me and I remember.
Let me do and I understand.
Chinese Proverb

Digital Image/Video Analysis


Real-Time Computer Vision Systems
Multimedia: Indexing and Retrieval
Software Engineering
OO Analysis and Design
Web Services
2

BTE550 Internet and Intranet Applications

Welcome to the Course

Course Outline
1. Networking Fundamentals

Important Course Information

2. Introduction to Web Technologies

18:30-21:30, Friday

3. XML

Course Web Page

4. XML using DTDs

http://www.cs.itu.edu.tr/~kurt/Courses/bte550

5. HTML
6. XHTML
7. XPath and XSLT
8. CSS
9. Multimedia Standards: Image, Video, Audio, Animation, VR, 3D
10. Dynamic HTML: JavaScript
11. Web Applications: Forms, JSP, PHP
3

BTE550 Internet and Intranet Applications

How to Use the Icons


Discussion

Reference
f

Networking Funndamentals

Demonstration

Objectives (Cont.)

Exercise

BTE550 Internet and Intranet Applications

Discuss the functions of each of the seven layers of the


OSI reference model and provide examples of each
Describe the basic process of communication between the
layers of the OSI reference model
Describe the functions of the TCP/protocol stack and
provide examples of each layer
layerss function
Compare the TCP/IP protocol stack to the OSI reference
model

10

BTE550 Internet and Intranet Applications

Networking Funndamentals

Basic Networking Terminology

Networking Fundamentals

WWW Request-Response
1

Define basic networking terms


Describe some commonly used network applications
Describe the main purposes and functions of computer
networking
y and ppurposes
p
of the OSI reference
Describe the history
model

Networking Funndamentals

Objectives

Web Browser
BTE550 Internet and Intranet Applications

11

BTE550 Internet and Intranet Applications

Copyleft 2005, Binnur Kurt

Networking Funndamentals

NIC
Media
Protocol
NOS
Connectivity devices
LAN
MAN
WAN
Physical topology
Logical topology

BTE550 Internet and Intranet Applications

Web Server
12

Sending E-Mail

Client A

E-mail
Server

Networking Funndamentals

Networking Funndamentals

E-mail
Server

The OSI Reference Model

Client B

13

BTE550 Internet and Intranet Applications

Networking Funndamentals

Networking Funndamentals

Internet

Branch Office

Application
Presentation

Session

T
Transport
t

Network

Data Link

Physical

14

Transport

Network

Data Link

Physical

16

Reduces complexity
Standardizes interfaces
Facilitates modular engineering
Ensures interoperable technology
Accelerates evolution
Simplifies teaching and learning

The Seven Layers of the OSI Model

Networking Funndamentals

Branch
Office

Floor 2
Server
Farm

Floor 1
ISDN

Remote

17

BTE550 Internet and Intranet Applications

Computer Networks
1

Session

Main Office

BTE550 Internet and Intranet Applications

Networking Funndamentals

Mobile Users

BTE550 Internet and Intranet Applications

Presentation

Why a Layered Network Model?


7

Home
Office

Application

BTE550 Internet and Intranet Applications

Defining Components of the Network


Home Office

Application

Presentation

Session

Transport
p

Network

Data Link

Physical

Network Processes to Applications


Provides network services to
application processes (such as
electronic mail, file transfer, and
terminal emulation)

Campus
15

BTE550 Internet and Intranet Applications

18

The Seven Layers of the OSI Model

Presentation

Network Process to Applications


Data Representation

Networking Funndamentals

Networking Funndamentals

Network Process to Applications

Application

Presentation

Session

Interhost Communication

Transport

End-to-End Connections

Network

Data Link

Physical

Application

The Seven Layers of the OSI Model

Ensures data is readable by


receiving system

Session

Transport

Network

Structures data

Data Link

Negotiates data transfer syntax


for application layer

Physical

Formats data

Data Representation

Data Delivery
Provides connectivity and path
selection between two host
systems
Routes data packets
Selects best path to deliver data

19

BTE550 Internet and Intranet Applications

The Seven Layers of the OSI Model

Presentation

Session

Transport

Network

Data Link

Physical

Network Process to Applications

Networking Funndamentals

Networking Funndamentals

Data Representation
Interhost Communication
Establishes, manages, and
terminates sessions
between applications

20

BTE550 Internet and Intranet Applications

Application

Presentation

Session

Interhost Communication

Transport

End-to-End Connections

Network

Data Link

Physical

Network Process to Applications

6
5

Presentation
Session

Transport

Network

Data Link

Physical

BTE550 Internet and Intranet Applications

Data Representation
Interhost Communication
End-to-End
d to d Co
Connections
ect o s
Handles transportation issues between hosts
Ensures data transport reliability
Establishes, maintains and terminates virtual
circuits
Provides reliability through fault detection
and recovery information flow control

21

Networking Funndamentals

Application

Data Delivery
Access to Media
Defines how data is formatted for
transmission and how access to
the network is controlled
23

The Seven Layers of the OSI Model

Data Representation

BTE550 Internet and Intranet Applications

The Seven Layers of the OSI Model

Networking Funndamentals

Network Process to Applications

Application

The Seven Layers of the OSI Model

22

BTE550 Internet and Intranet Applications

Network Process to Applications

Application

Presentation

Session

Interhost Communication

Transport

End-to-End Connections

Network

Data Delivery

Data Link

Access to Media

Physical

BTE550 Internet and Intranet Applications

Data Representation

Binary Transmission
Defines the electrical, mechanical,
procedural, and functional specifications
for activating, maintaining, and
deactivating the physical link.
24

Data Encapsulation

TCP/IP Model
1

User Data
7

Application

Presentation

Session

Transport

Network

Data Link

Physical

L4
HDR
L4
L3
HDR HDR
L2
L3
L4
HDR HDR HDR

L5
HDR
L5
HDR
L5
HDR
L5
HDR

L6
HDR
L6
HDR
L6
HDR
L6
HDR
L6
HDR

L7
HDR
L7
HDR
L7
HDR
L7
HDR
L7
HDR
L7
HDR

User Data
User Data
User Data
User Data
User Data
User Data

Application

Networking Funndamentals

Networking Funndamentals

Sender

FCS

Bits

Presentation
Internet
Network Access

HDR = header
25

BTE550 Internet and Intranet Applications

Data De-Encapsulation

TCP/IP Protocol Stack vs. OSI Model

Receiver

OSI Model

Networking Funndamentals

TCP/IP Protocol Stack

1
Networking Funndamentals

28

BTE550 Internet and Intranet Applications

User Data
7

Application

Presentation
i

Session

Transport

Network

Data Link

Physical

L4
HDR
L3
L4
HDR HDR
L2
L3
L4
HDR HDR HDR

L5
HDR
L5
HDR
L5
HDR
L5
HDR

L6
HDR
L6
HDR
L6
HDR
L6
HDR
L6
HDR

L7
HDR
L7
HDR
L7
HDR
L7
HDR
L7
HDR
L7
HDR

User Data
User Data
User Data
User Data
User Data
User Data

FCS

Application
Presentation

Application

Application
Layers

Protocols

Session
Transport

Transport

Internet

Network

Network
Access

Networks

Bits

Data Link

Data Flow
Layers

Physical

HDR = header
26

BTE550 Internet and Intranet Applications

Peer-to-Peer Communication

Summary

Application

Application

Presentation

Presentation

Session
Transport
Network
Data Link
Physical

BTE550 Internet and Intranet Applications

Segments
Packets
Frames
Bits
Network

Networking Funndamentals

Networking Funndamentals

Receiver

Sender

29

BTE550 Internet and Intranet Applications

Session
Transport
Network
Data Link
Physical

27

There are a number of basic computer network terms,


including NIC, media, protocol, physical topology, and
logical topology, that are fundamental to an understanding
of networks.
Network applications are software programs that run
between computers that are connected together on a
network.
Some common network applications include HTTP,
POP3, FTP, Telnet, and SNMP.

BTE550 Internet and Intranet Applications

30

There are many different ways in which a computer


network can be constructed to meet the requirements of an
organization, but user components are generally grouped
into the categories of main office, remote locations, and
mobile users.
The ISO created and released the OSI reference model in
1984 to provide vendors with a set of standards to ensure
greater compatibility and interoperability between various
types of network technologies.

BTE550 Internet and Intranet Applications

Repeater

Networking Funndamentals

Networking Funndamentals

Summary (Cont.)

31

BTE550 Internet and Intranet Applications

The OSI reference model reduces complexity,


standardizes interfaces, facilitates modular engineering,
ensures interoperable technology, accelerates evolution,
and simplifies teaching and learning.
Each layer of the OSI model has a set of unique
functions. The seven layers of the OSI model are the
application, presentation, session, transport, network, datalink, and physical layers.
Encapsulation is the process in which data is wrapped in
a particular protocol header before network transit.

BTE550 Internet and Intranet Applications

Hub (Multiport Repeater)

Networking Funndamentals

Networking Funndamentals

Summary (Cont.)

32

BTE550 Internet and Intranet Applications

35

Network Interface Card


1

The TCP/IP protocol stack has four layers: the


application layer, transport layer, internet layer, and
network access layer.
There are both similarities and differences between the
TCP/IP protocol stack and the OSI reference model.

Networking Funndamentals

Networking Funndamentals

Summary (Cont.)

BTE550 Internet and Intranet Applications

34

33

BTE550 Internet and Intranet Applications

36

Segment 1

Networking Funndamentals

Networking Funndamentals

Summary

Bridge

Segment 2

Corporate
Internet

37

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

1
Networking Funndamentals

Networking Funndamentals

38

BTE550 Internet and Intranet Applications

No Access
to HQ

AAA
Server

Networking Funndamentals

1
Networking Funndamentals

41

Summary (Cont.)

California

Corporate
Headquarters

Permit access from MA


Permit packets from NH
Permit packets from VT
Deny all other packets

New
Hampshire

The term "hub" (also called a multiport repeater) is used


instead of repeater" when referring to the device that
serves as a connection point in a network. Hubs work
Layer 1 only and make no filtering decisions.
Layer 2 LAN switches work at Layer 2, and they make
limited MAC hardware address decisions.
Routers can make decisions as to the best path for
delivery of data on the network.

BTE550 Internet and Intranet Applications

Firewalls

Allow
Access
to HQ

40

Summary (Cont.)

Switch

Massachusetts

Networking devices are products used to connect


networks.
Hubs, switches, and routers interconnect devices within
LANs, MANs, and WANs.
Networking devices function at different layers of the
OSI model,
model primarily Layers 1,
1 2,
2 and 33.
Repeaters reshape, amplify, and retime signals before
sending them along the network.

Working at Layers 2, 3, and 4, multilayer switches enable


implementation of Layer 3 QoS and security functionality
and perform many of the same functions as routers do, but
in hardware.
Voice gateways, DSLAMs, and optical devices are newer
types
yp of network connectivityy devices.
Firewalls and AAA servers provide security to the
network.

Allow
Access
to HQ

Vermont
BTE550 Internet and Intranet Applications

39

BTE550 Internet and Intranet Applications

42

Bus Topology

Ring Topology

Extended Star Topology

Networking Funndamentals

Networking Funndamentals

Star Topology

Physical Topologies

Star Topology

Mesh Topology

43

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

Logical Topologies

Extended-Star Topology
Internet

1
Main
Server

Main
Switch

Networking Funndamentals

Networking Funndamentals

46

E2

E0
E1

F
E

Workgroup
Switch
Repeater
A

C
H
G

Bridge
J
I

BTE550 Internet and Intranet Applications

44

BTE550 Internet and Intranet Applications

Networking Funndamentals

Networking Funndamentals

Ring Topology

Bus Topology

47

BTE550 Internet and Intranet Applications

45

BTE550 Internet and Intranet Applications

48

Networking Funndamentals

Networking Funndamentals

Summary

Dual-Ring Topology

A physical topology describes the plan for wiring the


physical devices, while a logical topology describes how
information flows through a network.
In a physical bus topology, a single cable connects all the
devices.
The most commonly used architecture in Ethernet LANs
is the physical star topology, with each host in the network
connected to the central device with its own cable.

Two links connected to the


same networking device
BTE550 Internet and Intranet Applications

49

Networking Funndamentals

Networking Funndamentals

Summary (Cont.)

Full-Mesh Topology

BTE550 Internet and Intranet Applications

50

When a star network is expanded to include additional


networking devices that are connected to the main
networking device, it is called an extended-star topology.
In a ring topology, all the hosts are connected in the form
of a ring or circle.
A full
full-mesh
mesh topology connects all devices to each other,
other
while in a partial-mesh topology, at least one device has
multiple connections to others.

53

BTE550 Internet and Intranet Applications

1
Networking Funndamentals

Communications Protocol

Networking Funndamentals

Partial-Mesh Topology

BTE550 Internet and Intranet Applications

52

BTE550 Internet and Intranet Applications

51

Host

Host

TCP

BTE550 Internet and Intranet Applications

54

TCP/IP Protocol Stack

Application Layer Overview

FTP,
Telnet,
SMTP

Presentation

FTP *

NetBIOS

Session
4

Transport

TCP

Network

IP

OSPF

IGRP

RIP

INT, IS-IS ICMP

ARP, RARP, SNAP

Data-link

LLC

Physical

Many Physical Implementations

OSI Reference Model

TFTP *

DNS, SNMP

Application

NFS

Networking Funndamentals

1
Networking Funndamentals

File Transfer
7

TCP/IP Protocol Stack

E-Mail
SMTP

Application

Remote Login
Telnet *
rlogin *

Transport

Network Management
SNMP *

Network

Name Management

Data Link

DNS*

*Used by the router

Physical

55

BTE550 Internet and Intranet Applications

TCP Characteristics

Summary
1

Connection-Oriented Protocol
Full-Duplex Operation

Networking Funndamentals

Networking Funndamentals

58

BTE550 Internet and Intranet Applications

Error Checking
Sequencing
Acknowledgments
Flow Control

Protocols define a standard set of rules for


communicating between devices.
TCP/IP is a suite of protocols arranged as a stack.
TCP is a connection-oriented protocol that provides flow
control and reliable data delivery services.
UDP provides
id minimal,
i i l non-guaranteed,
t d transport
t
t
services.
The transport layer supports multiple application
protocols.

Packet Recovery
56

BTE550 Internet and Intranet Applications

59

BTE550 Internet and Intranet Applications

UDP Characteristics

Network and Host Addresses


1

Minimal Service

Networking Funndamentals

Networking Funndamentals

Network Host

Unreliable
Not-Guaranteed
Direct Access to Datagrams

BTE550 Internet and Intranet Applications

57

1
2
3

2.1
1.2
3.1

1.1

1.3

BTE550 Internet and Intranet Applications

60

10

An IP address is a 32-bit binary number:


10101100000100001000000000010001
The 32-bit binary number can be divided into four octets:
10101100 00010000 10000000 00010001
Each octet (or byte) can be represented in decimal:
172 16 128 17
The address can be written in dotted-decimal notation:
172.16.128.17

IP Address Range
IP Address Class

Networking Funndamentals

Networking Funndamentals

Dotted-Decimal Notation

IP Address Range
(First octet decimal value)

Class A

1-126 (00000001-01111110) *

Class B

128-191 (10000000-10111111)

Class C

192-223 (11000000-11011111)

Class D

224-239 (11100000-11101111)

Class E

240-255 (11110000-11111111)

Determine the class based on the decimal


value of the first octet.

*127 (011111111) is a Class A address reserved for loopback testing and cannot be assigned to a network.
61

BTE550 Internet and Intranet Applications

IP Address Classes
No. of bits

Network no.

Host no.

Network Addresses

24

Class A:

Class B:

No. of bits

Class C:

14
Network no.
21
Network no.

No of bits 1
No.

28

Class D:*

Address

No. of bits 1

28

Class E:**

Address

16
Host no.
8
Host no.

*Class D addresses are used for multicast groups. There is no


need to allocate octets or bits to separate network and host
addresses.
**Class E addresses are reserved for research use only.

Networking Funndamentals

Networking Funndamentals

No. of bits

62

BTE550 Internet and Intranet Applications

32 Bits
NETWORK

HOST

8 Bits
1 Byte

8 Bits
1 Byte

16

1 Byte
8 Bits

Class A:

Class B:

Class C:

Network Address (host bits = all zeros)

65

1
1 Byte
8 Bits

BTE550 Internet and Intranet Applications

Networking Funndamentals

1 Byte
8 Bits

8 Bits
1 Byte

Broadcast Address

1 Byte
8 Bits

8 Bits
1 Byte

172

IP Address Class Components

Networking Funndamentals

64

BTE550 Internet and Intranet Applications

N = Network number assigned by ARIN


H = Host number assigned by administrator

32 Bits
NETWORK
8 Bits
1 Byte

HOST
8 Bits
1 Byte

N
172

8 Bits
1 Byte

16

8 Bits
1 Byte

255

Network Address (host bits = all zeros)

N
172

16

255

Broadcast Address (host bits = all ones)

BTE550 Internet and Intranet Applications

63

BTE550 Internet and Intranet Applications

66

11

The Internet
In the early days of computer
networking, local area networks (LANs)
were created to share resources among
members of a particular institution. These
networks were constrained to short
distances.

Class

RFC 1918 Internal Address Range

10.0.0.0 to 10.255.255.255

172.16.0.0 to 172.31.255.255

192.168.0.0 to 192.168.255.255

Int. To Web A
App. Tech.

Networking Funndamentals

Private IP Addresses

67

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

Networking Funndamentals

70

Client-Server Architecture

Other
Classes
12.5%

Int. To Web A
App. Tech.

IPv4 Address Allocation

Class C
12.5%

The Internet is a Network


of Networks

Class B
25%
Class A
50%

With Class A and B addresses virtually exhausted, Class C addresses (12.5 percent of the total
space) are left to assign to new networks.
BTE550 Internet and Intranet Applications

68

BTE550 Internet and Intranet Applications

71

Introduction to Web
Application Technologies

Copyleft 2005, Binnur Kurt

Int. To Web A
App. Tech.

Hypertext Transfer Protocol


The Hypertext Transfer Protocol (HTTP) is similar to FTP
because it is a protocol to transfer files from the server to
the client.
HTTP was created in conjunction with the related
Hypertext Markup Language (HTML) standard.
There is one fundamental difference between FTP and
HTTP: HTTP supports only one request per connection.
This means that the client connects to the server to
retrieve one file and then disconnects. This mechanism
allows more users to connect to a given server over a
period of time.
BTE550 Internet and Intranet Applications

72

12

73

BTE550 Internet and Intranet Applications

76

Web Browsers and Web Servers

Uniform Resource Locator

To view an HTML document with rich media content, a


graphical user interface (GUI) was built on top of the
client-side HTTP. This GUI is called a Web browser.
The server-side HTTP component is called a Web server.
Several companies have developed Web browsers and
Web servers; some have developed both. The first Web
server was a process called httpd; the first widely used
browser was Mosaic, created by National Center for
Supercomputing Applications (NCSA).

A URL is a canonical name that locates a specific


resource on the Internet:
protocol://host:port/path/file
Example:
http://www.soccer.org:80/league/Spring2001.html
h pathh element
The
l
includes
i l d the
h complete
l directory
di
structure
path to find the file. The port number is used to identify
the TCP port that is used by the protocol on the server. If
the port number is the standard port for the given protocol,
then that number can be ignored in the URL.
For example, port 80 is the default HTTP port.

BTE550 Internet and Intranet Applications

For every exchange over the Web using HTTP, there is a


request and a response.

Int. To Web A
App. Tech.

HTML is a document display language that allows users


to link from one document to another.
HTML also permits images and other media objects to be
embedded in an HTML document. The media objects are
stored in files on a server.
HTTP also retrieves these files. HTTP can therefore be
used to transmit any file that conforms to the
Multipurpose Internet Mail Extensions (MIME)
specification.

BTE550 Internet and Intranet Applications

Int. To Web A
App. Tech.

HTTP Client-Server Architecture

Int. To Web A
App. Tech.

Int. To Web A
App. Tech.

HTML

74

BTE550 Internet and Intranet Applications

Web Browsers and Web Servers

Web Applications
2
Int. To Web A
App. Tech.

Int. To Web A
App. Tech.

Early web servers and browsers

BTE550 Internet and Intranet Applications

77

75

Very early in the development of HTML, the designers


created a mechanism to permit a user to invoke a program
on the Web server.
This was called the Common Gateway Interface (CGI).
When a Web site includes CGI processing, this is called a
Webb application.
l

BTE550 Internet and Intranet Applications

78

13

Usually, the browser needs to send data to the CGI program on the
server.
The CGI specification defines how the data is packaged and sent in
the HTTP request to the server. This data is usually typed into the
Web browser in an HTML form.
program
g
to execute. This might
g be
The URL determines which CGI p
a script or an executable file.
The CGI program parses the CGI data in the request, processes the
data, and generates a response (usually an HTML page). The CGI
response is sent back to the Web server, which wraps the response
in an HTTP response. The HTTP response is sent back to the Web
browser.

BTE550 Internet and Intranet Applications

Execution of CGI Programs

Int. To Web A
App. Tech.

Int. To Web A
App. Tech.

CGI Programs on the Web Server

79

82

BTE550 Internet and Intranet Applications

CGI Programs on the Web Server

Int. To Web A
App. Tech.

BTE550 Internet and Intranet Applications

80

Overview of XML

Copyleft 2005, Binnur Kurt

At runtime, a CGI program is launched by the Web server as


a separate operating system (OS) shell.
The shell includes an OS environment and process to
execute the code of the CGI program, which resides within
the servers file system.

Content
Define XML
Compare and contrast HTML and XML
Identify characteristics of XML documents

XM
ML

Int. To Web A
App. Tech.

Execution of CGI Programs

BTE550 Internet and Intranet Applications

81

BTE550 Internet and Intranet Applications

84

14

What is XML?

HTML Document (Good for Formatting)


<html><body>
<h2>Student List</h2>

W3C-endorsed standard for document markup


A generic syntax used to mark up data with simple,
human-readable tags
Provides a standard format for computer documents
Flexible enough to be customized for different domains as
diverse as web sites, electronic data interchange, vector
graphics, real-estate listings, object serialization, remote
procedure calls, voice-mail systems,...

XM
ML

XM
ML

the eXtensible Markup Language

85

BTE550 Internet and Intranet Applications

Data and presentation


logic mixed

What is no?
88

XML Document (Good for Describing Data)


<?xml version = "1.0"?>

XML is not a programming language


There's no such thing as an XML compiler that reads XML files
and produces executable code

XML is not a network transport protocol


XML is not a database
You're not going to replace an Oracle or MySQL server with
XML
A database can contain XML data, but the database itself is not
an XML document

XM
ML

What is yes?

BTE550 Internet and Intranet Applications

What is not XML?

XM
ML

<ul>
<li> 9906789 </li>
<li>Adam</li>
<li>adam@unl.ac.uk</li>
@
<li>yes - final </li>
</ul>
<ul>
<li> 9806791 </li>
<li>Adrian</li>
<li>adrian@unl.ac.uk</li>
<li>no</li>
</ul>
</body></html>

Is this the student ID? or UCAS number?

<student_list>
<student>
<id> 9906789 </id>
<name>Adam</name>
<email>adam@unl.ac.uk</email>
<bsc level=final>yes</bsc>
</student>
/
<student>
<id> 9806791 </id>
<name>Adrian</name>
<email>adrian@unl.ac.uk</email>
<bsc>no</bsc>
</student>

Only data
Data is self-describing
Custom tags describe content
(you can/will define your own tags)
Easy to locate data
(e.g. all BSc students)

</student_list>
86

BTE550 Internet and Intranet Applications

XM
ML

<ul>
<li> 9906789 </li>
<li>Adam</li>
<li>adam@unl ac uk</li>
<li>adam@unl.ac.uk</li>
<li>yes - final </li>
</ul>
<ul>
<li> 9806791 </li>
<li>Adrian</li>
<li>adrian@unl.ac.uk</li>
<li>no</li>
</ul>
</body>
</html>
BTE550 Internet and Intranet Applications

Portable Data
Portable Code

<?xml version = "1.0"?>


<student_list>
<student>
<id> 9906789 </id>
<name> Adam </name>
<email> adam@unl.ac.uk </email>
<bsc level=final>yes</bsc>
</student>

<html>
<head> </head>
<body>
<h2>Student List</h2>

89

What do we need for Web Services & B2B?

students.xml
Specifies structure of the data

XM
ML

students.html
Specifies visual presentation

BTE550 Internet and Intranet Applications

<student>
<id> 9806791 </id>
<name>Adrian</name>
<email>adrian@unl.ac.uk</email>
<bsc>no</bsc>
</student>

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


<!DOCTYPE employees SYSTEM "employees.dtd">
<employees>
<company-name>Sun Microsystems, Inc.</company-name>
p y number="2498" >
<employee
<name>

<first>Sridhar</first>
<last>Reddy</last>
</name>
<title>Staff Engineer</title>
<organization>Market Development Engineering</organization>
<address> 901 San Antonio Road, </address>
<email>sridhar.reddy@sun.com</email>
</employee>

</student_list>

</employees>
87

BTE550 Internet and Intranet Applications

90

15

Service Driven Approach

Java and XML: Symbiotic Relationship

3
XM
ML

XM
ML

client

91

BTE550 Internet and Intranet Applications

94

BTE550 Internet and Intranet Applications

Two Viewpoints of XML

XML: The Missing Link For Web Applications


Portable
data
3
XM
ML

XM
ML

Portable
code

Its a Match made in Heaven


Java enables Portable Code
XML enables Portable Data
XML tools and programs are mostly written in the Java
pprogramming
g
g language
g g
Better API support for Java platform than any other
language
Two great tastes that taste great together

Presentation Oriented Publishing (POP)


Useful for Browsers and Editors
Usually used for data that will be consumed by Humans
Message Oriented Middleware (MOM)
Useful for Machine
Machine-to-Machine
to Machine data exchange
Business-to-Business communication an excellent
example

Ubiquitous
communication
92

BTE550 Internet and Intranet Applications

POP - MOM

Presentation Oriented Publishing


(POP)

Portable data
Works anywhere
Lingua franca of the Internet
Multiple vendors
Open development process:
World Wide Web Consortium (W3C)

XM
ML

XML

XM
ML

95

BTE550 Internet and Intranet Applications

Message Oriented Middleware


(MOM)

BTE550 Internet and Intranet Applications

93

BTE550 Internet and Intranet Applications

96

16

XML

XM
ML

XML Standards
Through Standard organizations
W3C, IETF, OASIS, UN/CEFACT

XM
ML

97

XM
ML

The eXtensible Markup Language (XML) is the universal


format for structured documents and data on the Web
XML is a text-based markup language.
As with HTML, you identify data using tags (identifiers
enclosed in angle brackets, like this: <...>).
Collectively, the tags are known as "markup".
But unlike HTML, XML tags tell you what the data
means, rather than how to display it.

W3C

How XML Works

World Wide Web Consortium (W3C) creates Web


standards.
W3C's mission is to lead the Web to its full potential,
which it does by developing technologies (specifications,
guidelines, software, and tools) that will create a forum for
information commerce,
information,
commerce inspiration,
inspiration independent thought
thought,
and collective understanding.
W3C defines the Web as the universe of networkaccessible information
W3C languages RDF, XML, and digital signatures are the
building blocks of the Semantic Web.

<?xml version="1.0"?>
Data Oriented
<invoice
invoice>
<orderDate
orderDate>2005-01-01</orderDate
orderDate>
<shipDate
shipDate>2005-01-05</shipDate
shipDate>
<billingAddress
billingAddress>
<name
name>Paul Biron</name
name>
<street
street>123 IBM Avenue</street
street>
<city
city>Hawthorne</city
city>
<state
state>NY</state
state>
<zip
zip>10532</zip
zip>
This document is text and might
</billingAddress
billingAddress>
well be stored in a text file. You
<voice
voice>555-1234</voice
voice>
can edit this file with any
<fax
fax>555-4321</fax
fax>
standard text editor
</invoice
invoice>

98

BTE550 Internet and Intranet Applications

XML is an extremely flexible format for data


In theory, any data that can be stored in a computer can be
stored in XML format.
In practice, XML is suitable for storing and exchanging
any data that can plausibly be encoded as text.
Unsuitable for multimedia data such as photographs,
recorded sound, video, and other very large bit sequences

XML Parser

XM
ML

101

BTE550 Internet and Intranet Applications

XML

XM
ML

100

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

XM
ML

Standardization Activities

An XML parser is responsible for dividing the document


into individual elements, attributes, and other pieces.
It passes the contents of the XML document to an
application piece by piece.
If at any point the parser detects a violation of the well
well-p
the error to the
formedness rules of XML,, then it reports
formed
application and stops parsing.

<orderDate
orderDate>2005-01-01</orderDate
orderDate>
element
start-tag
end-tag
BTE550 Internet and Intranet Applications

99

BTE550 Internet and Intranet Applications

102

17

Individual XML applications normally dictate more


precise rules about exactly which elements and attributes
are allowed where
DTD, XML Schema
Some XML parsers compare the document to its schema
as they
y read it to see if the document satisfies the
constraints specified there
Such a parser is called a validating parser
Checking a document against a schema is called
validation
Not all parsers are validating parsers. Some merely check
for well-formedness

The problem: SGML is complicatedvery, very


complicated
It is so complex that almost no software has ever
implemented it fully
In 1996, J.Bosak, T.Bray, C.M. Sperberg, J.Clark, and
several others began work on a "lite" version of SGML
The result, in February of 1998, was XML 1.0
The next standard out of the gate was Namespaces in
XML
Next was the Extensible Stylesheet Language (XSL)

XM
ML

XM
ML

103

BTE550 Internet and Intranet Applications

106

The Evolution of XML

The Evolution of XML (Cont)

XML is a descendant of SGML, the Standard Generalized


Markup Language
SGML was invented by Charles F. Goldfarb, Ed Mosher,
and Ray Lorie at IBM in the 1970s
Became ISO standard 8879 in 1986
It is a semantic and structural markup language for text
documents
Achieved some success in the U.S. military and
government, in the aerospace sector
SGML's biggest success was HTML, which is an SGML
application

Development of extensions to the core XML specification


continues
XML Namespaces
XML DTDs, XML Schema
XSL ((Extensible Style
y Sheet Language)
g g )
XPath (=XSLT XPointer), XLink
XQL (XML Query Language)
eXcelon

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

The Evolution of XML (Cont)

XM
ML

3
XM
ML

XML Parser (Cont)

104

BTE550 Internet and Intranet Applications

XM
ML

XM
ML

XML Family of Standards

XML Base Standard

107

BTE550 Internet and Intranet Applications

105

BTE550 Internet and Intranet Applications

108

18

XML Characteristics
Elements
<PurchaseOrder
PurchaseOrder>
</PurchaseOrder
PurchaseOrder>

XM
ML

XML Fundamentals

109

BTE550 Internet and Intranet Applications

XML Characteristics

XML Documents and XML Files

Elements
Text

A very simple yet complete XML


document

XM
ML

XM
ML

An XML document contains text, never binary data


It can be opened with any program that knows how to read
a text file
<person
person>
Alan Turing
</person
person>

112

<PurchaseOrder>
<description>Battery</description>
<quantity>9</quantity>
<price>60</price>

person.xml
Your operating system may or may not like these names
But an XML parser won't care
BTE550 Internet and Intranet Applications

110

BTE550 Internet and Intranet Applications

Elements, Tags, and Character Data

XML Characteristics

Example is composed of a single element named person


The element is delimited by the start-tag <person> and the
end-tag
d
</person>.
/
Everything between the start-tag and the end-tag of the
element is called the element's content
The whitespace is part of the content, though many
applications will choose to ignore it
The string "Alan Turing" and its surrounding whitespace are
character data

XM
ML

XM
ML

<person
person>
Alan Turing
</person
person>

BTE550 Internet and Intranet Applications

113

111

Elements
Text
Attributes
<ShoeOrder id="4040458"
id="4040458">
<color>Brown</color>
<size>9</size>
<width>AA</width>
</ShoeOrder>

BTE550 Internet and Intranet Applications

114

19

Processing Instruction
<?xml version="1.0"?>
<!DOCTYPE order SYSTEM "order.dtd">
Document Type Definition (DTD)
<order >
<book isbn="0-201-34285-5">
<title>The XML Companion</title>
Attribute
<author>Neil Bradley</author>
y
<publisher>Addison-Wesley</publisher>
</book>
Element
</order>
<!-- This is a comment -->
Comment

BTE550 Internet and Intranet Applications

Case Sensitivity

XM
ML

3
XM
ML

Document R
Root Element

An XML Document

115

XML, unlike HTML, is case sensitive


<Person> is not the same as <PERSON> is not the same
as <person>.
If you open an element with a <person> tag, you can't
close it with a </PERSON> tag

XML Trees
Every XML document has one element that does not have
a parent: root element
3

Basic components of XML documents


Elements must start with a letter, underscore or colon
Encapsulate element content, usually composed of:
Other elements
Character data
Entity references
Delimited using tags
All elements must have a start-tag and an end-tag
Elements can optionally have attributes
Empty elements can use an abbreviated element form

XM
ML

XM
ML

XML Elements

BTE550 Internet and Intranet Applications

116

<invoice
invoice>
<orderDate
orderDate>2005-01-01</orderDate
orderDate>
<shipDate
shipDate>2005-01-05</shipDate
shipDate>
<billingAddress
billingAddress>
<name
name>Paul Biron</name
name>
<street
street>123 IBM Avenue</street
street>
<city
city>Hawthorne</city
city>
<state
state>NY</state
state>
<zip
zip>10532</zip
zip>
</billingAddress
billingAddress>
<voice
voice>555-1234</voice
voice>
<fax
fax>555-4321</fax
fax>
</invoice
invoice>

119

XML Trees (Cont)

<CC:LunchMenu xmlns:Camp=http://catering.com/CC>
...
<CC:MainCourse>. . .</CC:MainCourse>

fatura
3

XML Namespaces allow a prefix to be associated with an


element to avoid name collisions
XML Namespaces are a W3C specification
A unique URI must be used with a prefix to denote elements
in this namespace from other namespaces
The URI is only for distinguishing prefixes, it is not actually
resolved
Namespaces use the reserve word xmlns

XM
ML

3
XM
ML

Root Element is invoice

BTE550 Internet and Intranet Applications

XML Namespaces

BTE550 Internet and Intranet Applications

118

BTE550 Internet and Intranet Applications

sipari--tarihi
sipari

gnderim--tarihi
gnderim

isim

117

cadde

BTE550 Internet and Intranet Applications

fatura--adresi
fatura

ile

telefon

ehir

fax

posta--kodu
posta

120

20

BTE550 Internet and Intranet Applications

White Space

XM
ML

XM
ML

Attributes
Elements can contain attributes to provide information
about the element
Attributes are not considered part of an elements content
Attributes are not part of the presentation to an end user,
though they may be used to affect the presentation
pair attached to the element's
An attribute is a name-value p
start-tag
Names are separated from values by an equals sign and
optional whitespace
Values are enclosed in single or double quotation marks

121

XML defines white space as any of these 4 characters


Horizontal tab
Line feed
Carriage return
Space
An XML parser must pass all white space contained
within content to the application
An XML parser may remove white space in element tags
and attribute values
All end of line characters are converted to line feed
characters by parsers

XML Names

<person born
born="1912-06-23" died
died="1954-06-07">
Alan Turing
</person>

Attributes (Cont)

XM
ML

XM
ML

or
<person born
born= `1912-06-23` died
died= `1954-06-07`>
Alan Turing
</person>

BTE550 Internet and Intranet Applications

122

125

XML Names (Cont)

<person>
<name first="Alan" last="Turing"/>
<profession value="computer scientist"/>
<profession value="mathematician"/>
<profession value
value="cryptographer"/>
cryptographer />
</person>

XM
ML

3
XM
ML

Element and other XML names may contain essentially


any alphanumeric character.
This includes the standard English letters A through Z and
a through z as well as the digits 0 through 9.
XML names may also include non-English letters,
numbers, and ideograms such as , ,
They may also include these three punctuation characters:
_ the underscore
- the hyphen
. the period
BTE550 Internet and Intranet Applications

Attributes (Cont)

When and whether one should use child elements or


attributes to hold information?
This is a subject of heated debate
BTE550 Internet and Intranet Applications

124

BTE550 Internet and Intranet Applications

123

XML names may only start with letters, ideograms, and the
underscore character.
They may not start with a number, hyphen, or period.
There is no limit to the length of an element or other XML name.
Thus these are all well-formed elements:
<Drivers_License_Number>98
<Drivers License Number>98 NY 32
</Drivers_License_Number>
<month-day-year>7/23/2001</month-day-year>
<first_name>Alan</first_name>
<_4-lane>I-610</_4-lane>
<tlphone>011 33 91 55 27 55 27</tlphone>

BTE550 Internet and Intranet Applications

126

21

XM
ML

127

Character references represent displayable characters that


cannot otherwise be displayed
Character references are either decimal or hexadecimal
numbers
Decimals are preceded by &#
Hexadecimals are preceded by &#x
All character references end with a semicolon
Example:
&#169 or &#xA9 will display as

BTE550 Internet and Intranet Applications

130

Entity References

CDATA Sections

The character data inside an element may not contain a


raw unescaped opening angle bracket (<).
This character is always interpreted as beginning a tag
If you need to use this character in your text, you can
escape it using the &lt;
When a parser reads the document, it will replace the &lt;
entity reference with the actual < character
<publisher>O'Reilly &amp; Associates</publisher>

When an XML document includes samples of XML or


HTML source code, the < and & characters in those
samples must be encoded as &lt; and &amp;
&amp;.
The more sections of literal code a document includes and
the longer they are, the more tedious this encoding
b
becomes
Instead you can enclose each sample of literal code in a
CDATA section. CDATA sections exist for the
convenience of human authors, not for programs.
An XML parser will not attempt to process any data in a
CDATA section

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

<forbidenNames>
<A;name/>
<last@name>
<@#$%^()%+?=/>
<A*2/>
<1ex/>
</forbidenNames>

XM
ML

<permitedNames>
<name/>
<xsl:copy-of>
<A_long_element_name/>
<A.name.separated.with.full.stops/>
<a123323123 231 231/>
<a123323123-231-231/>
<_12/>
</permitedNames>

Character References

XM
ML

XM
ML

XML Names (Cont)

128

BTE550 Internet and Intranet Applications

Entity References (Cont)

CDATA Sections: Example

XML predefines exactly five entity references:


&lt;

&gt;
& t

3
XM
ML

The less-than sign; a.k.a. the opening angle bracket (<)

&amp;

131

XM
ML

The ampersand (&)


The greater-than sign; a.k.a. the closing angle bracket (>)

&quot;
The straight, double quotation marks (")

&apos;

<p>You can use a default <code>xmlns</code> attribute to avoid


having to add the svg prefix to all your elements:
</p>
<![CDATA[
<svg xmlns="http://www.w3.org/2000/svg" width="12cm"
height="10cm">
height
10cm
<ellipse rx="110" ry="130" />
<rect x="4cm" y="1cm" width="3cm" height="6cm" />
</svg> ]]
>

The apostrophe; a.k.a. the straight single quote (')


BTE550 Internet and Intranet Applications

129

BTE550 Internet and Intranet Applications

132

22

XML documents can be commented so that coauthors can


leave notes for each other and themselves, documenting
why they've done what they've done or items that remain
to be done.

XML comments are used to provide information about the


XML document
Comments are not considered part of the content
Comment have the following syntax:
<!-- comment text -->
C
Comments
can appear anywhere
h except inside
i id markup
k
tags and attribute values
XML comments should not be used to transmit
information
Comments should contain no entity or character
references

XM
ML

133

Processing Instructions

The XML Declaration

XML provides the processing instruction as a mean of


passing information to particular applications that may
read the document.
A processing instruction begins with <? and ends with ?>.
Immediately following the <? is an XML name called the
target
Processing instructions are markup, but they're not
elements.
Consequently, like comments, processing instructions may
appear anywhere in an XML document outside of a tag,
including before or after the root element.

XML documents should (but do not have to) begin with


an XML declaration.
The XML declaration looks like a processing instruction
with the name xml and version, standalone, and encoding
attributes.
Technically, it's not a processing instruction though, just
the XML declaration

BTE550 Internet and Intranet Applications

<??xml version="1.0" encoding="ASCII" standalone="yes"??>


<person>
Alan Turing
</person>

134

Encoding

mysql_connect("database.unc.edu", "clerk", "password");


$result = mysql("HR","SELECT LastName, FirstName FROM
Employees ORDER BY LastName, FirstName");

$i = 0;

XM
ML

while ($i < mysql_numrows ($result)) {

XM
ML

137

BTE550 Internet and Intranet Applications

<?php

136

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

XM
ML

<!-<!
-- I need to verify and update these links when I get a chance. --->
>

Comments may appear anywhere in the character data of a


document.
They may also appear before or after the root element.

Comments

XM
ML

3
XM
ML

Comments

$fields = mysql_fetch_row($result);
mysql fetch row($result);
echo "<person>$fields[1] $fields[0] </person>\r\n";
$i++;
}
mysql_close( );

XML documents are composed of pure text


Which encoding?
Is it ASCII? Latin-1?
Unicode? Something else?
By default XML documents are assumed to be encoded in
the
h UTF-88 variable-length
i bl l
h encoding
di off the
h Unicode
i d
character set.
However, most XML processors, especially those written
in Java, can handle a much broader range of character sets.
All you have to do is tell the parser which character
encoding the document uses.

?>
BTE550 Internet and Intranet Applications

135

BTE550 Internet and Intranet Applications

138

23

Encoding (Cont)

Well-formed XML Examples

An XML document encoded in Latin-1 which includes


letters like and needed for many non-English Western
European languages.
<person>

XM
ML

XM
ML

<?xmlversion="1.0" encoding="ISO
encoding="ISO--88598859-1" standalone="yes"?>
Erwin Schrdinger
</person>

139

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

If the standalone attribute has the value no, then an


application may be required to read an external DTD to
determine the proper values for parts of the document.
For instance, a DTD may provide default values for
attributes that a parser is required to report even though
they
h aren't' actually
ll present in
i the
h document.
d

140

143

Well-formed XML Examples: One root element

141

There is exactly one element, called the root, or document


element, no part of which appears in the content of any
other element.
3

Every XML document must be well-formed. This means it must


adhere to a number of rules, including the following:
1. Every start-tag must have a matching end-tag.
2. Elements may nest, but may not overlap.
3. There must be exactly one root element.
4 Attribute values must be quoted.
4.
quoted
5. An element may not have two attributes with the same name.
6. Comments and processing instructions may not appear inside
tags.
7. No unescaped < or & signs may occur in the character data of
an element or attribute.

<name>Binnur Kurt</name>
XM
ML

3
XM
ML

The name in an element's end-tag must match the element


type in the start-tag.
In HTML some elements do not have to have a closing
tag. The following code is legal in HTML:
< >Thi is
<p>This
i a paragraphh
<p>This is another paragraph
In XML all elements must have a closing tag like this:
<p>This is a paragraph</p>
<p>This is another paragraph</p>

BTE550 Internet and Intranet Applications

Well-Formedness

BTE550 Internet and Intranet Applications

142

Well-formed XML Examples:


Match start & end Tag

XM
ML

XM
ML

Standalone

BTE550 Internet and Intranet Applications

A well formed document with one element:


<text>This is an XML document</text>
A well formed document with several elements:
<name>
<first>Binnur</first>
<last>Kurt</last>
</name>

<name>
<first>Binnur</first>
<last>Kurt</last>
</name>

BTE550 Internet and Intranet Applications

144

24

XML Quiz 1

Each element has either the end tag or takes the special
form.
There is no difference between <AAA></AAA> and
<AAA/> in XML.
<listOfTags>
<AAA></AAA>
<BBB></BBB>
<CCC/>
<DDD/>
</listOfTags>

XM
ML

XM
ML

Well-formed XML Examples: Element end tag

145

BTE550 Internet and Intranet Applications

Solution:
<root>
<e1 a*b = "23432"/>
<e2 value = "12'/>
<e3 value
value="aa"aa"/>
aa aa />
<e4 value='bbbb'/>
<e5 xml-ID = "xml2"/>
</root>

XML Quiz 2

XML elements can have attributes in name/value pairs.


Attribute values must always be quoted
With XML, it is illegal to omit quotation marks around
attribute values.
<elements-with-attributes>
<ell _okk = "yes"
" " //>
<one attr= "a value"/>
<several first="1" second = '2' third= "333"/>

XM
ML

XM
ML

Well-formed XML Examples: Attributes

<apos_quote case1="John's" case2='He said: "Hello!"'/>

</elements-with-attributes>

146

BTE550 Internet and Intranet Applications

Find Errors:
<root>
<example>
<![CDATA[ <P>Q&R]]>
</example>
<Name>
Binnur Kurt
</Name>
<Address/>
</root>

XML Quiz 2
Solution:
No error
3

Find errors:
<root>
<e1 a*b = "23432"/>
<e2 value = "12'/>
<e3 value
value="aa"aa"/>
aa aa />
<e4 value=bbbb/>
<e5 xml-ID = "xml2"/>
</root>

XM
ML

3
XM
ML

149

BTE550 Internet and Intranet Applications

XML Quiz 1

BTE550 Internet and Intranet Applications

148

BTE550 Internet and Intranet Applications

147

BTE550 Internet and Intranet Applications

150

25

XM
ML

XML Quiz 3
Find Errors:
<root>
<isLower>
23 < 46
</isLower>
<Name>
Willey & Sons
</name>
</root>

151

BTE550 Internet and Intranet Applications

XML Using DTDs

Copyleft 2005, Binnur Kurt

Content

BTE550 Internet and Intranet Applications

Solution:
<root>
<isLower>
23 < 46
</isLower>
<Name>
Willey & Sons
</name>
</root>

XML USING DTDs

XM
ML

XML Quiz 3

152

Define Document Type Definition (DTD)


Give an example of an XML file with a DTD to illustrate
DTD syntax
Write a program that uses a validating SAX parser
g
that uses the EntityResolver
y
Write a SAX pprogram
interface to control handling of external subsets

Objectives
4

Create an XML document that captures business card


information.
Give appropriate tag names.
cd $Lab$\Mod1
Review card.txt make appropriate changes and create
card.xml

XML USING DTDs

XM
ML

Exercise: Create an XML document

BTE550 Internet and Intranet Applications

155

BTE550 Internet and Intranet Applications

153

Understand as to why to constrain XML documents


Validation of XML Documents
Understand as to how to do this using
DTDs and
XML Schema

BTE550 Internet and Intranet Applications

156

26

Valid XML documents

BTE550 Internet and Intranet Applications

XML = Portable data


XML = Unconstrained data
DTDs and XML Schemas add Constraints to XML

XML USING DTDs

XML USING DTDs

XML and Schema

157

BTE550 Internet and Intranet Applications

<?xml version="1.0"?>
<trade account=1234567 action=buy>
<symbol>SUNW</symbol>
<symbol>IBM</symbol>
<symbol>CSCO</symbol>
<quantity>200</quantity>
<quantity>100</quantity>
</trade>

158

161

A DTD defines the legal elements of an XML document.


DTD is described in XML 1.0 standard.
A DTD can be declared inline in your XML document, or
as an external reference.
Internal DOCTYPE declaration:
<!DOCTYPE root-element [element-declarations]>
External DOCTYPE declaration:
<!DOCTYPE root-element SYSTEM "filename">

159

Example: XML document with a DTD

XML USING DTDs

4
XML USING DTDs

With a DTD, independent groups of people can agree to


use a common DTD for interchanging data.
Your application can use a standard DTD to verify that the
data you receive from the outside world is valid.
You can also use a DTD to verify your own data.
A lot of forums are emerging to define standard DTDs for
almost everything in the areas of data exchange.

BTE550 Internet and Intranet Applications

XML DTD Document Type Definition

BTE550 Internet and Intranet Applications

160

Why use DTD?

XML USING DTDs

XML USING DTDs

Example: Unconstrained XML document

BTE550 Internet and Intranet Applications

A "Well Formed" XML document has correct XML


syntax.
A "Valid" XML document is not only well-formed, but
conforms to a DTD.

<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>J. Canny</to>
<from>Binnur Kurt</from>
<heading>Tebrikler</heading>
<body>Merhaba!!</body>
</note>
BTE550 Internet and Intranet Applications

162

27

The document type declaration must appear before the


first element in the document.
The name following the word DOCTYPE in the document
type declaration must match the name of root element.
When an element type has element content, that type must
contain only child elements (no character data), optionally
separated by white space.

Quiz: Is this a valid XML document?

XML USING DTDs

XML USING DTDs

Rules for DTD

163

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

name.dtd:
<!ELEMENT name (first
first, last
last)>
<!ELEMENT first (#PCDATA)>
<!ELEMENT last (#PCDATA)>
Employees.xml:
<!DOCTYPE name SYSTEM name.dtd">
<name
name>
<first
first>Sridhar</first
first>
<last
last>Reddy</last
last>
</name
name>

164

167

Quiz: Is this a valid XML document?


4

<?xml version="1.0"?>
<!DOCTYPE trade [
<!ELEMENT trade (symbol, quantity)>
<!ATTLIST trade
account CDATA #REQUIRED
action (buy | sell) #REQUIRED >
<!ELEMENT symbol (#PCDATA)>
<!ELEMENT quantity (#PCDATA)> ]>
<trade account=1234567 action=buy>
<symbol>SUNW</symbol>
<quantity>100</quantity>
</trade>

XML USING DTDs

4
XML USING DTDs

<?xml version="1.0"?>
<!DOCTYPE trade [
<!ELEMENT trade (symbol, quantity)>
<!ATTLIST trade
account CDATA #REQUIRED
action (buy | sell) #REQUIRED >
<!ELEMENT symbol (#PCDATA)>
<!ELEMENT quantity (#PCDATA)> ]>
<trade account=1234567 action=sell>
<symbol>SUNW</symbol>
<quantity>100</quantity>
</trade>
BTE550 Internet and Intranet Applications

Example: XML document with a DTD

BTE550 Internet and Intranet Applications

166

Quiz: Is this a valid XML document?

XML USING DTDs

XML USING DTDs

Example

BTE550 Internet and Intranet Applications

<?xml version="1.0"?>
<!DOCTYPE trade [
<!ELEMENT trade (symbol, quantity)>
<!ATTLIST trade
account CDATA #REQUIRED
action (buy | sell) #REQUIRED >
<!ELEMENT symbol (#PCDATA)>
<!ELEMENT quantity (#PCDATA)> ]>
<trade account=1234567 action=steal>
<symbol>SUNW</symbol>
<quantity>100</quantity>
</trade>

165

<?xml version="1.0"?>
<!DOCTYPE trade [
<!ELEMENT trade (symbol, quantity)>
<!ATTLIST trade
account CDATA #REQUIRED
action (buy | sell) #REQUIRED >
<!ELEMENT symbol (#PCDATA)>
<!ELEMENT quantity (#PCDATA)> ]>
<trade account=1234567 action=sell>
<symbol>SUNW</symbol>
<quantity>100</quantity>
<quantity>300</quantity>
</trade>
BTE550 Internet and Intranet Applications

168

28

Example using *, ? and + in DTD

Qualifier
?
*
+
none

Name
Question Mark
Asterisk
Plus Sign
Default

Here are the qualifiers you can add to an element


definition:
Meaning
Optional (zero or one)
Zero or more
One or more
Must occur once

BTE550 Internet and Intranet Applications

XML USING DTDs

XML USING DTDs

More Rules for DTD

169

4
XML USING DTDs

XML USING DTDs

170

173

Example using *, ? and + in DTD


4

tutorial.dtd:
<!ELEMENT XXX (AAA? , BBB+)>
<!ELEMENT AAA (CCC? , DDD*)>
<!ELEMENT BBB (CCC , DDD)>
<!ELEMENT CCC (#PCDATA)>
<!ELEMENT DDD (#PCDATA)>
<!DOCTYPE tutorial SYSTEM "tutorial.dtd">
<XXX>
<AAA>
<CCC/><DDD/>
</AAA>
<BBB>
<CCC/><DDD/>
</BBB>
</XXX>

XML USING DTDs

tutorial.dtd:
<!ELEMENT XXX (AAA? , BBB+)>
<!ELEMENT AAA (CCC? , DDD*)>
<!ELEMENT BBB (CCC , DDD)>
<!ELEMENT CCC (#PCDATA)>
<!ELEMENT DDD (#PCDATA)>
<!DOCTYPE tutorial SYSTEM "tutorial.dtd">
<XXX>
<BBB>
<CCC/>
<DDD/>
</BBB>
</XXX>
BTE550 Internet and Intranet Applications

Example using *, ? and + in DTD

XML USING DTDs

172

Example using *, ? and + in DTD

tutorial.dtd:
<!ELEMENT XXX (AAA? , BBB+)>
<!ELEMENT AAA (CCC? , DDD*)>
<!ELEMENT BBB (CCC , DDD)>
<!ELEMENT CCC (#PCDATA)>
<!ELEMENT
!ELEMENT DDD (#PCDATA)>
(#PCDATA)
The root element XXX can contain one element AAA
followed by one or more elements BBB.
Element AAA can contain one element CCC and several
elements DDD.
Element BBB must contain precisely one element CCC
and one element DDD.

BTE550 Internet and Intranet Applications

<!DOCTYPE tutorial SYSTEM "tutorial.dtd">


<XXX>
<AAA/>
<BBB>
<CCC/>
<DDD/>
</BBB>
</XXX>
BTE550 Internet and Intranet Applications

Example using *, ? and + in DTD

BTE550 Internet and Intranet Applications

tutorial.dtd:
<!ELEMENT XXX (AAA? , BBB+)>
<!ELEMENT AAA (CCC? , DDD*)>
<!ELEMENT BBB (CCC , DDD)>
<!ELEMENT CCC (#PCDATA)>
<!ELEMENT DDD (#PCDATA)>

171

tutorial.dtd:
<!ELEMENT XXX (AAA? , BBB+)>
<!ELEMENT AAA (CCC? , DDD*)>
<!ELEMENT BBB (CCC , DDD)>
<!ELEMENT CCC (#PCDATA)>
<!ELEMENT DDD (#PCDATA)>
<!DOCTYPE tutorial SYSTEM "tutorial.dtd">
<XXX>
<AAA>
<CCC/> <CCC/>
<DDD/> <DDD/>
</AAA>
<BBB>
<CCC/><DDD/>
</BBB>
</XXX>
BTE550 Internet and Intranet Applications

174

29

Example: Attributes declaration in DTD

XML USING DTDs

XML USING DTDs

More DTD Rules


With character [ | ] you can select one from several elements.
<!ELEMENT XXX (AAA , BBB)>
<!ELEMENT AAA (CCC , DDD)>
<!ELEMENT BBB (CCC | DDD)>
<!ELEMENT CCC (#PCDATA)>
<!ELEMENT
!ELEMENT DDD (#PCDATA)>
(#PCDATA)
The root element XXX must contain one element AAA
followed by one element BBB.
Element AAA must contain one element CCC followed by
element DDD.
Element BBB must contain either one element CCC or one
element DDD
175

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

178

Example: Attributes declaration in DTD


4

Text can be interspersed with elements


<!ELEMENT XXX (AAA+ , BBB+)>
<!ELEMENT AAA (BBB | CCC )>
<!ELEMENT BBB (#PCDATA | CCC )*>
<!ELEMENT CCC (#PCDATA)>
The element AAA can contain either BBB or CCC
On the other hand the element BBB can contain any
combination of text and CCC elements.

XML USING DTDs

DTD Rules

XML USING DTDs

<!ELEMENT attributes (#PCDATA)>


<!ATTLIST attributes
aaa CDATA #REQUIRED
bbb CDATA #IMPLIED
ccc CDATA #FIXED someData >
An attribute of CDATA type can contain any character if it
conforms to well formedness constraints.
Required attribute must be always present
Implied attribute is optional.
If an attribute has a default value declared with the #FIXED
keyword, instances of that attribute must match the default
value.

<!DOCTYPE student [
<!ELEMENT student (name)>
<!ELEMENT name (#PCDATA)>
<!ATTLIST student DOB
CADAT #REQUIRED EDU
CDATA #IMPLIED> ]>
<student DOB="Nov11,1990" EDU="4thGrade">
<name>Curran Reddy </name>
</student>
<student EDU="1st Grade" DOB="May14,1994">
<name>Kaavya Reddy</name>
</student>

BTE550 Internet and Intranet Applications

176

BTE550 Internet and Intranet Applications

Attributes are used to associate name-value pairs with


elements.
Attribute specifications may appear only within start-tags
and empty element tags.
The declaration starts with ATTLIST then follows the
name of the element the attributes belong to and then
follows the definition of the individual attributes.
The order of attributes is not important

BTE550 Internet and Intranet Applications

177

Rules in DTD: Attribute values

XML USING DTDs

XML USING DTDs

Attributes declaration in DTD

179

Permitted attribute values can be defined in DTD.


<!ELEMENT SPORT (player+,practicemonth)>
<!ELEMENT player (#PCDATA)>
<!ELEMENT practiceMonth EMPTY)>
<!ATTLIST player meetsHeightReq ( yes | no )
#REQUIRED>
<!ATTLIST practiceMonth (1|2|3|4|5|6|7|8|9|10|11|12)
#IMPLIED>
The attribute meetsHeightReq cannot have the value
"maybe
The attribute practiceMonth cannot have the value "16"
BTE550 Internet and Intranet Applications

180

30

If an attribute is implied, a default value can be provided for


the case when the attribute is not used.

Whenever possible, use an existing DTD. It's usually a lot


easier to ignore the things you don't need than to design
your own from scratch
Using a standard DTD makes data interchange possible,
and may make it possible to use data-aware tools
d l d by
developed
b others.
h
If an industry standard exists, consider referencing that
DTD with an external parameter entity.
industry-standard DTDs:
http://www.XML.org
http://www.xmlx.com

<!ELEMENT practiceMonth EMPTY)>


<!ATTLIST practiceMonth (1|2|3|4|5|6|7|8|9|10|11|12) 11>

An element can be defined EMPTY. In such a case it can


contain
i only
l attributes
ib
but
b no text.
<!ELEMENT AAA EMPTY>
<!ATTLIST AAA true ( yes | no ) "yes">

BTE550 Internet and Intranet Applications

Designing an XML Data Structure

XML USING DTDs

4
XML USING DTDs

Rules in DTD: Attribute values

181

BTE550 Internet and Intranet Applications

Parameter-entity references may only appear in the DTD.


Parameter-entity references use percent-sign (%) and semicolon (;)
as delimiters.
Example:
<!ENTITY % auction-req SYSTEM "Auction.dtd">
Usage:
%auction-req;
Errors:
Fatal Error: Missing whitespace after % in parameter entity
declaration.
Fatal Error: Illegal parameter entity reference syntax. If space
after % when referencing.
BTE550 Internet and Intranet Applications

Attributes and Elements

XML USING DTDs

XML USING DTDs

Rules in DTD: Parameter-entity

182

When to model a given data item as a subelement or as an


attribute of an existing element?
Use element if:
The data contains substructures
The data contains multiple lines
The data changes frequently
Use attribute if:
The data is a small, simple string that rarely changes
The data is confined to a small number of fixed
choices
BTE550 Internet and Intranet Applications

DTD Validation

185

Attributes and Elements


4
XML USING DTDs

XML USING DTDs

Validating with the XML Parser

BTE550 Internet and Intranet Applications

184

183

Also consideration can be based on how you would like to parse the
data.
<employee>
<id> 12056 </id>
<grade>Manager</grade>
g
g
<division>Engineering</division>
<description>
He is a manager of X product. Leads a team of 15
programmers. Also this person is in the special task
force team to build next generation app.
</description>
</employee>
BTE550 Internet and Intranet Applications

186

31

XML USING DTDs


187

How Will Agreements Be Shared?

DTDs are not defined in XML


DTDs don't distinguish between different data types, such
as dates, integers, and text strings as Data Types are not
mentioned.

We need to publish XML agreements on the Internet


human search: what industry standard meets my needs?
machine resolution/namespaces
versioning: what is the latest version of a DTD or Schema?
archiving: what is the meaning of documents generated 20
years ago?
Requirement for XML registry and repository
developed/implemented to open specifications
accessible by everyone
unencumbered use of repository

188

BTE550 Internet and Intranet Applications

189

XML USING DTDs

XML Schema facilities for describing the structure and


constraining the contents of XML 1.0 documents
The schema language, which is itself represented in XML
1.0
XML Schema was a W3C Proposed Recommendation as
of March 16, 2001
XML Schema is now a W3C Recommendation as of May
2nd 2001.
http://www.w3.org/TR/2001/REC-xmlschema-020010502/
BTE550 Internet and Intranet Applications

191

XML.ORG Registry
4

XML Schema
4

190

Limitations of DTDs

BTE550 Internet and Intranet Applications

XML USING DTDs

Electronics Supply Chain (RosettaNet)


Electronic Business (ebXML - UN/CEFACT & OASIS)
Finance (IFX)
Healthcare (XL7)
Payment Processing (Visa XML)
Hospitality (Hitis-X)
Trading Partners (tpaML - IBM)
B2B (eBIS-XML - BASDA)
Internet billing (Spectrum )
WAP (WML)
School Interoperability (SIF)
Telephony (XTML)
Travel (OTA)
and many, many, more...
BTE550 Internet and Intranet Applications

or as:
<employee grade=Manager id=12056
division=Engineering >
<description>
He is a manager generation app
</description>
</employee>

BTE550 Internet and Intranet Applications

XML USING DTDs

Example DTDs and Schemas

XML USING DTDs

XML USING DTDs

Attributes and Elements

Central clearinghouse for accessing XML schemas,


vocabularies and related documents
Self-supporting, non-commercial resource created by
OASIS for the community at large
Model for a distributed web of repositories that will
comply with OASIS Technical Committee specification

BTE550 Internet and Intranet Applications

192

32

Part 1:
Create a DTD for the XML document that captured the
information about business card in lab 1.
Part 2:
Compare
p yyour DTD to the pperson sittingg next to yyou
and see if you can combine into one which can still
validate both the XML documents.

BTE550 Internet and Intranet Applications

Atomic types

XML USING DTDs

XML USING DTDs

Exercise: Create a DTD

193

BTE550 Internet and Intranet Applications

Only one base type -- PCDATA.


No useful abstractions, e.g., unordered records.
No sub-typing or inheritance.
IDREFs are not typed
d or scoped
d -- you point
i to something,
hi but
b
you dont know what!

XML extensions to overcome the limitations.


Type systems: XML-Data, XML-Schema, SOX, DCD
Integrity Constraints

Example: define an element or an attribute


4

By database (or programming language) standard, XML


DTDs are rather weak specifications.

BTE550 Internet and Intranet Applications

194

Define the type:


<xs:simpleType name
name=carType>
carType >
<xs:restriction base=xs:string>
<xs:enumeration value=Audi>
<xs:enumeration value=BMW>
</xs:restriction>

BTE550 Internet and Intranet Applications

197

Complex types

Official W3C Recommendation


4

A rich type system


Simple (atomic, basic) types for both element and attributes

XML USING DTDs

<xs:element name=car type=carType>


<xs:attribute name=car type =carType>

</xs:simpleType>

XML Schema

XML USING DTDs

196

Atomic types: Examples

XML USING DTDs

XML USING DTDs

DTDs vs. schemas (types)

string, integer, boolean, date, ,


enumeration types
restriction and range [a-z]
list: list of values of an atomic type,

Complex types for elements


Inheritance
Constraints
key
keyref (foreign keys)

Sequence: record type ordered


All: record type unordered
Choice: variant type
Occurrence constraint: maxOccurs, minOccurs
Group: mimicking parameter type to facilitate complex
type definition
Any: open type unrestricted

uniqueness: more general keys


Namespace
BTE550 Internet and Intranet Applications

195

BTE550 Internet and Intranet Applications

198

33

Example

Inheritance Restriction
Supertype: restricting/removing certain fields of an existing
type
4

<xs:complexType name=publicationType>
<xs:sequence>
<xs:choice>
<xs:group ref=journalType>
<xs:element name
name=conference
conference type
type=xs:string/>
xs:string />
</xs:choice>
<xs:element name=title type=xs:string/>
<xs:element name=author type=xs:string
minOccur=0 maxOccur=unbounded/>
</xs:sequence>

XML USING DTDs

XML USING DTDs

A complex type for publications:

</xs:complexType>
BTE550 Internet and Intranet Applications

199

<xs:complexType name=anotherPublicationType>
<xs:complexContent>
<xs:restriction base=publicationType>
<xs:sequence>
<xs:choice>
<xs:group ref=journalType>
<xs:element name=conference type=xs:string/>
</xs:choice>
<xs:element name=author type=xs:string
minOccur=1
maxOccur=unbounded/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
Removed title; minOccur of author is incremented to 1
</xs:complexType>
202

BTE550 Internet and Intranet Applications

XML USING DTDs

Example (contd)
<xs:group name=journalType>
<xs:sequence>
<xs:element name=name type=xs:string/>
<xs:element name=volume type=xs:integer/>
<xs:element name=number
type=xs:integer/>
</xs:sequence>
</xs:group>

BTE550 Internet and Intranet Applications

200

HTML

Copyleft 2005, Binnur Kurt

Content

Subtype: extending an existing type by including


additional fields
<xs:complexType name=datedPublicationType>

Creating Basic Documents


Use HTML to structure a basic Web page by manipulating
text and graphics
5

<xs:complexContent>
<xs:extension base=publicationType>
<
<xs:sequence>
>
<xs:element name=isbn type=xs:string/>
</xs:sequence>
<xs:attribute name=publicationDate type=xs:date/>
</xs:extension>
</xs:complexContent>

HTM
ML

XML USING DTDs

Inheritance -- Extension

</xs:complexType>
BTE550 Internet and Intranet Applications

201

BTE550 Internet and Intranet Applications

204

34

Web-Authoring Tools

HTM
ML

HTM
ML

Overview
Hypertext Markup Language (HTML) is the foundation of all Web
documents, or pages.
This module will teach you the basic elements and architecture of
a web page and how HTML brings it together.
You will learn to identify tags, view code, create hierarchies,
format text and pages, and use styles and style sheets.

205

BTE550 Internet and Intranet Applications

Although creating simple Web pages in a text editor is


easy, it can become a tedious task.
Web-authoring tools, such as Microsoft FrontPage
Express and Macromedia Dreamweaver, allow you to
create HTML Web pages in the same way word
documents are created.

When a Web page is open in a browser, the HTML code


that creates the page is not visible.
Instead, the browser interprets the HTML code and
displays the page appropriately on the screen.

HTM
ML

It is important to know the elements involved with


building Web pages. Upon completion of this lesson, you
will be able to:
Summarize Web page creation
List the elements of HTML
Identify HTML tags
View and evaluate HTML code in a page

206

209

BTE550 Internet and Intranet Applications

Basics

Edit Views

The pages created with HTML are plain text.


You can create, edit, or view the HTML code for a Web
page in any text editor, such as Windows Notepad, on any
computer platform.

If you are creating a Web page in a text editor and want to


view the file, save your work and open the file in the
browser.
You can then continue to edit, save your work, and view
the results, switching between the text editor and the
browser to see the effects of the edits.

HTM
ML

HTM
ML

Browsers

HTM
ML

Introduction

BTE550 Internet and Intranet Applications

208

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

207

BTE550 Internet and Intranet Applications

210

35

The original intent of the HTML specification was to allow Web


authors to describe the structure of a page without spending time on
the look of a page.
Traditionally, each browser had its own way of interpreting the look
of the page, and the page created would appear differently on
different browsers.

When a browser differentiates a non-specific message, one


browser might display the first-level heading in a large font
centered on the page, while another browser might display
it in italics and left-aligned on the page.
As a result, authors test pages in several of the more
popular browsers.
browsers

HTM
ML

211

Standard Views

HTML can now describe the look of a page accurately.


More and more descriptive elements are being established
in the HTML specification, such as font styles, sizes,
colors, and style sheets that can maintain a consistent look
throughout a Web site.

The browser market has been consolidating and standardizing to


eliminate interpretation problems.
There are few differences in the ways competing browsers display the
widely accepted HTML features in a page.
However, new HTML features become available all the time, and
Web authors must decide whether to include a new feature in a page
when that feature may not be well interpreted by some browsers.
browsers

HTM
ML
BTE550 Internet and Intranet Applications

212

213

A Web page consists of elements, each of which is


defined by an HTML code, or tag.

HTM
ML

Elements

An author can specify a paragraph of text be defined as


one of the six heading levels, but the HTML heading code
cannot describe what a heading should look like.
The Web browser must differentiate each type of heading
from the others.

BTE550 Internet and Intranet Applications

215

BTE550 Internet and Intranet Applications

Text Limitations

HTM
ML

214

BTE550 Internet and Intranet Applications

Accurate Page Descriptions

HTM
ML

BTE550 Internet and Intranet Applications

View Testing

5
HTM
ML

Browser Limitations

BTE550 Internet and Intranet Applications

216

36

Uppercase or Lowercase
You can create a tag in either uppercase or lowercase.
For example, the two tags <H1> and <h1> are equivalent
to a browser.
5

A tag is always enclosed in angle brackets, and most tags


come in pairs, with an opening and a closing tag.
The closing tag is the same as the opening tag, but starts
with a forward slash.
For example, to define text as a first-level heading in
HTML, use the <H1> tag, as shown.

HTM
ML

HTM
ML

Tag Architecture

217

BTE550 Internet and Intranet Applications

A browser interprets tags and displays the text within the


tags appropriately.
The tags themselves are not displayed within a browser
unless there is a problem with a tag such as if one of the
angle brackets was mistakenly left out.
Most browsers will ignore any codes within angle
brackets that they do not recognize.

HTML tag
The <HTML> tag declares that the text that follows
defines an HTML Web page that can be viewed in a Web
browser.
The closing </HTML> tag ends the page.

HTM
ML

HTM
ML

Browsers

218

BTE550 Internet and Intranet Applications

HEAD tag
The <HEAD> tag defines the header area of a page, which
is not displayed within the page itself in the browser.
The closing </HEAD> tag ends the header area.
5

Some tags have optional or required attributes. An


attribute is usually a keyword that takes one of several
possible values.
A value is defined by enclosing it in quotes.
For example, the heading tag can take an optional
alignment attribute.

HTM
ML

5
HTM
ML

221

BTE550 Internet and Intranet Applications

Attributes

BTE550 Internet and Intranet Applications

220

BTE550 Internet and Intranet Applications

219

BTE550 Internet and Intranet Applications

222

37

Title TagThe text between <TITLE> and the closing


</TITLE> tag is the title of the Web page and is displayed
in the title bar of a browser.
The title should be descriptive; as it is frequently used by
Web indexing and searching programs to name the Web
page.
In Internet Explorer, a page title is the default name used
when you save the page as a favorite location.

Comment tag

HTM
ML

HTM
ML

TITLE tag

223

BTE550 Internet and Intranet Applications

The Comment tag is not revealed in the browser, but will be


advantageous when editing or viewing the HTML code for a page.
In Internet Explorer, use the <!--> tag to create descriptive
comments within the code, which will be ignored by the browser.
With other browsers, you can use the combination of symbols to
create a comment.

Spaces

The <BODY> tag delineates the actual content of the Web


page that will be displayed in the browser.
There are several optional attributes for this tag.
One of them is BACKGROUND, with which you can
specify a background graphical image for the page.

HTM
ML

HTM
ML

BODY tag

BTE550 Internet and Intranet Applications

224

You can include extra spaces and blank lines in HTML


code to make the code easier to read and interpret.
When a browser opens a Web page, it ignores multiple
spaces within the code and displays them as a single
space.

Hard Return

225

HTML code ignores all hard returns within the code: for
example, an Enter at the end of a text line you are editing
in Notepad will not be displayed in the user's browser.
5

Use the paragraph tag (<P>) to mark the beginning of a


new paragraph; the ending tag, </P>, is optional but
should be included for clarity during revisions.
You can include the ALIGN attribute to specify whether
the paragraph should be centered or right-aligned in the
page.

HTM
ML

5
HTM
ML

227

BTE550 Internet and Intranet Applications

Paragraph tag

BTE550 Internet and Intranet Applications

226

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

228

38

Save as View
To view a page's code, click View > Source in Internet
Explorer or View > Page Source in Navigator to display
the current page's HTML code within Notepad.
5

In the preformatted tag <PRE>, spaces and hard returns in


the HTML code do display.
It instructs a browser to display the text in a mono-spaced
font that allows you to align text precisely, such as you
would when showing a program listing.

HTM
ML

HTM
ML

Preformatted tag

229

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

Information

Evolving Language

5
HTM
ML

HTM
ML

For additional documentation on tags used in HTML


http://werbach.com/barebones/barebones.html

BTE550 Internet and Intranet Applications

230

HTML is constantly evolving.


Web authors may include new and improved tags within
Web pages to produce new effects.
But browser software may not recognize those HTML
features.
W3C at the MIT defines and establishes new versions of
HTML to help with this problem.
Unofficially, leaders such as Microsoft and Netscape,
regularly invent their own extensions to official HTML
which eventually may be included in the official HTML
specification.
BTE550 Internet and Intranet Applications

Underlying Code

233

Structuring Web Pages

5
HTM
ML

HTM
ML

Most Web pages are built from the same text-based


HTML language, so when viewing an interesting page in a
browser, take a look at the underlying code.

BTE550 Internet and Intranet Applications

232

231

When developing a Web page, you must determine a


structure for your text and images using HTML that best
suites your organization.
Upon completion of this lesson, you will be able to:
Add structure
Divide sections
Create hierarchies
Format text and pages
Use styles and style sheets

BTE550 Internet and Intranet Applications

234

39

Adding structure will benefit any Web page you create.


To add basic structure to a page, add paragraphs and
spaces to text.
To create a paragraph, enclose text within the paragraph
tags (<P> and </P>).
Your browser will insert some extra space between
paragraphs, so in some instances, you will not want to use
the <P> tag.

BTE550 Internet and Intranet Applications

Structure Separations
If your page has a banner across the top with a company name, you
can insert a horizontal line beneath it.
This separates it from the table of contents showing links to pages,
beneath which you can insert another line, followed by the main
body of the page.
At the bottom of the page, you can have another line that shows
i
important
page identifiers.
id ifi

HTM
ML

5
HTM
ML

Adding Structure

235

5
HTM
ML

You may not want extra space between each line of the
address.
To avoid this, use the line-break tag, <BR>.
This break tells the browser to wrap the text that follows
onto a new line without inserting any extra space between
the lines.

236

The <HR> tag takes several optional attributes. For


example, you can specify the line's thickness and how
much of the browser's window it should span (as a
percentage or in pixels).
The line thickness default is one or two pixels in most
browsers.

BTE550 Internet and Intranet Applications

239

Section Divisions

Creating Headings

A simple and effective way to separate sections within a


Web page is to insert a horizontal line, which is also called
a horizontal rule.
By default, the line stretches from one side of the page to
the other.

A common way to add structure to a Web page is through


the use of headings.
A Web page can have a maximum of six levels of
headings, the HTML codes for which are conveniently
named <H1>, <H2>, <H3>, etc.
No style is inherent in the headings different Web
browsers might interpret the look of a heading in slightly
different ways.
Structurally, however, all browsers will display headings
so low-level headings look subordinate to a higher-level
heading.

BTE550 Internet and Intranet Applications

237

5
HTM
ML

HR tag

HTM
ML

HTM
ML

Line Break tag

BTE550 Internet and Intranet Applications

238

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

240

40

Comments in HTML

HTM
ML

HTM
ML

Heading Formats
In a browser, a first-level heading is displayed in a larger, bolder
font than a lower-level heading.
You can use HTML headings in any order, but it is recommended to
use them in an outline format.
The first-level heading, <H1>, is the highest level, and the sixth
level, <H6>, is the lowest or most subordinate.

241

BTE550 Internet and Intranet Applications

Paragraphs

The comment tag is used to insert a comment in the


HTML source code. A comment will be ignored by the
browser. You can use comments to explain your code,
which can help you when you edit the source code at a
later date.
<!-!
This
hi i
is a comment -->
Note that you need an exclamation point after the opening
bracket, but not before the closing bracket.

Text Formatting Tags

Defines emphasized text

Defines big text

<em>

Defines italic text


Defines small text

<strong>

Defines strong text

<sub>

Defines subscripted text

<sup>

Defines superscripted text

<ins>

Defines inserted text

<del>

Defines deleted text

<s>

Deprecated. Use <del> instead

<strike>

Deprecated. Use <del> instead

<u>

Deprecated. Use styles instead

BTE550 Internet and Intranet Applications

245

"Computer Output" Tags

The <br> tag is used when you want to end a line, but
don't want to start a new paragraph.
The <br> tag forces a line break wherever you place it.
<p>This <br> is a para<br>graph with
line breaks</p>

HTM
ML

5
HTM
ML

Defines bold text

<big>

<small>

Line Breaks

The <br> tag is an empty tag.


It has no closing tag.

BTE550 Internet and Intranet Applications

Description

<i>

242

BTE550 Internet and Intranet Applications

Tag
<b>

HTM
ML

HTM
ML

Paragraphs are defined with the <p> tag.


<p>This is a paragraph</p>
<p>This is another paragraph</p>
HTML automatically adds an extra blank line before and
after a p
paragraph.
g p

244

BTE550 Internet and Intranet Applications

243

Tag
<code>
<kbd>
<samp>
<tt>
<var>
<pre>
<listing>
<plaintext>
<xmp>

Description
Defines computer code text
Defines keyboard text
Defines sample computer code
Defines teletype text
Defines a variable
Defines preformatted text
Deprecated. Use <pre> instead
Deprecated. Use <pre> instead
Deprecated. Use <pre> instead

BTE550 Internet and Intranet Applications

246

41

Description
Defines an abbreviation
Defines an acronym
Defines an address element
D fi
Defines
the
h text direction
di i
Defines a long quotation
Defines a short quotation
Defines a citation
Defines a definition term

BTE550 Internet and Intranet Applications

Tag
<abbr>
<acronym>
<address>
<bdo>
bd
<blockquote>
<q>
<cite>
<dfn>

The Most Common Character Entities:

HTM
ML

HTM
ML

Citations, Quotations, and Definition Tags

247

5
HTM
ML

HTM
ML

248

<

&#160;
&#60;

>
&

greater than
ampersand

&gt;
&amp;

&#62;
&#38;

"

quotation mark

&quot;

&#34;

'

apostrophe

&apos;

&#39;

250

Result

Description
cent

Entity Name Entity Number


&cent;
&#162;

pound
yen
section
copyright
registered trademark

&pound;
&yen;
&sect;
&copy;
&reg;

&#163;
&#165;
&#167;
&#169;
&#174;

multiplication
division

&times;
&divide;

&#215;
&#247;

251

The most common character entity in HTML is the nonbreaking space.


Normally HTML will truncate spaces in your text.
If you write 10 spaces in your text, HTML will remove 9
of them.
To add spaces to your text, use the &nbsp; character
entity.

249

The Anchor Tag and the href Attribute

HTM
ML

Entity Number

&nbsp;
&lt;

BTE550 Internet and Intranet Applications

Non-breaking Space

HTM
ML

Entity Name

Some Other Commonly Used Character Entities

Some characters have a special meaning in HTML, like the less than
sign (<) that defines the start of an HTML tag.
If we want the browser to actually display these characters we must
insert character entities in the HTML source.
A character entity has three parts: an ampersand (&), an entity name
or a # and an entity number, and finally a semicolon (;).
To display a less than sign in an HTML document we must write:
&lt; or &#60;
The advantage of using a name instead of a number is that a name is
easier to remember.
The disadvantage is that not all browsers support the newest entity
names, while the support for entity numbers is very good in almost
all browsers.

BTE550 Internet and Intranet Applications

Description
non-breaking space
less than

BTE550 Internet and Intranet Applications

Character Entities

BTE550 Internet and Intranet Applications

Result

HTML uses the <a> (anchor) tag to create a link to


another document.
An anchor can point to any resource on the Web: an
HTML page, an image, a sound file, a movie, etc.
The syntax of creating an anchor:
<a href="url">Text to be displayed</a>
The <a> tag is used to create an anchor to link from, the
href attribute is used to address the document to link to,
and the words between the open and close of the anchor
tag will be displayed as a hyperlink.
BTE550 Internet and Intranet Applications

252

42

With the target attribute, you can define where the linked
document will be opened.
The line below will open the document in a new browser
window:

<a href="http://www.itu.edu.tr" target="_blank">


Visit ITU!

HTM
ML

Frames

HTM
ML

The Target Attribute

</a>

BTE550 Internet and Intranet Applications

253

BTE550 Internet and Intranet Applications

The name attribute is used to create a named anchor.


When using named anchors we can create links that can
jump directly into a specific section on a page, instead of
letting the user scroll around to find what he/she is
looking for.
l is
Below
i the
h syntax off a namedd anchor:
h
<a name="label">Text to be displayed</a>
The name attribute is used to create a named anchor. The
name of the anchor can be any text you care to use.
The line below defines a named anchor:
<a name="tips">Useful Tips Section</a>
254

257

You should notice that a named anchor is not displayed in


a special way.
To link directly to the "tips" section, add a # sign and the
name of the anchor to the end of a URL, like this:
<a href="http://www.itu.edu.tr/index.html#kayit">
Kaytla lgili Bilgiler
</a>

The Frameset Tag

HTM
ML

5
HTM
ML

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

BTE550 Internet and Intranet Applications

The Anchor Tag and the Name Attribute

A hyperlink to the Useful Tips Section from WITHIN the


file index.htm" will look like this:
<a href="#tips">Jump to the Useful Tips Section</a>

BTE550 Internet and Intranet Applications

256

The Frameset Tag

HTM
ML

HTM
ML

The Anchor Tag and the Name Attribute

BTE550 Internet and Intranet Applications

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 web developer must keep track of more HTML
documents
It is difficult to print the entire page

255

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
h bbrowser window.
i d
The
Th HTML document
d
"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>
BTE550 Internet and Intranet Applications

258

43

Example

5
HTM
ML

HTM
ML

Vertical frameset
<html>
<frameset cols="25%,50%,25%">
<frame src="frame_a.htm">
<frame src="frame_b.htm">
<frame src="frame_c.htm">
</frameset>
/
</html>

259

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

5
260

<table border=5">
<tr>
<td>Row 1, cell 1</td>
<td>Row 1, cell 2</td>
</tr>
</table>
263

Headings in a Table

261

Headings in a table are defined with the <th> tag.

Tables are defined with the <table


table> tag.
A table is divided into rows (with the <tr
tr> tag), and each
row is divided into data cells (with the <td
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.

HTM
ML

5
HTM
ML

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:

BTE550 Internet and Intranet Applications

Tables

BTE550 Internet and Intranet Applications

262

Tables and the Border Attribute

HTM
ML

HTM
ML

Horizontal frameset
<html>
<frameset rows="25%,50%,25%">
<frame src="frame_a.htm">
<frame src="frame_b.htm">
<frame src="frame_c.htm">
</frameset>
/
</html>

BTE550 Internet and Intranet Applications

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

<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>
BTE550 Internet and Intranet Applications

264

44

Empty Cells in a Table

Ordered Lists

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

HTM
ML

HTM
ML

Table cells with no content are not displayed very well in


most browsers.

265

BTE550 Internet and Intranet Applications

An ordered list is also a list of items. The list items are


marked with numbers.
An ordered list starts with the <ol> tag. Each list item
starts with the <li> tag.
<ol>
<li>Coffee</li>
<li>Milk</li>
</ol>

Table Tags

<col>
<thead>
<tbody>
<tfoot>

Description
Defines a table
Defines a table header
Defines a table row
Defines a table cell
D fi
Defines
a ttable
bl caption
ti
Defines groups of table columns

<th>
<tr>
<td>
< ti >
<caption>
<colgroup>

Definition Lists

HTM
ML

HTM
ML

Tag
<table>

Defines the attribute values for one or more columns in a


table
Defines a table head
Defines a table body
Defines a table footer

BTE550 Internet and Intranet Applications

266

A definition list is not a list of items. This is a list of terms


and explanation of the terms.
A definition list starts with the <dl> tag. Each definitionlist term starts with the <dt> tag.
Each definition-list definition starts with the <dd> tag.
<dl>
<dt>Coffee</dt>
<dd>Black hot drink</dd>
<dt>Milk</dt>
<dd>White cold drink</dd>
</dl>

An unordered list is a list of items. The list items are


marked with bullets (typically small black circles).
An unordered list starts with the <ul> tag. Each list item
starts with the <li> tag.
<ul>
<li>Coffee</li>
<li>Milk</li>
</ul>
Inside a list item you can put paragraphs, line breaks,
images, links, other lists, etc.
267

List Tags

HTM
ML

5
HTM
ML

269

BTE550 Internet and Intranet Applications

Unordered Lists

BTE550 Internet and Intranet Applications

268

BTE550 Internet and Intranet Applications

Tag
<ol>
<ul>
<li>
<dl>
<dt>
<dd>
<dir>
<menu>

Description
Defines an ordered list
Defines an unordered list
Defines a list item
Defines a definition list
Defines a definition term
Defines a definition description
Deprecated. Use <ul> instead
Deprecated. Use <ul> instead

BTE550 Internet and Intranet Applications

270

45

HTML Forms and Input

Forms and Server-Based Programs

5
<form
form name="input" action="html_form_action.asp" method="get">

HTM
ML

HTM
ML

HTML Forms are used to select different kinds of user


input.

Type your first name:


<input
input type="text
text" name
name="FirstName" value="Binnur" size
size="20">
<br>Type your last name:

Server-based programs are written in many languages


The earliest and most commonly used are Common
Gateway Interface (CGI) scripts that are written in perl.
Other popular languages include:

AppleScript
ASP
ColdFusion
C/C++

- PHP
- TCL
- the Unix shell
- Visual Basic

<input
input type="text
text" name
name="LastName" value="Kurt" size="20">
<br>
<input
input type="submit
submit" value
value="Submit
Submit">
</form
form>
BTE550 Internet and Intranet Applications

271

BTE550 Internet and Intranet Applications

Introducing Web Forms

Creating the Form Element

HTM
ML
272

Forms are created using the form element, structured as


follows:
<form attributes>
elements
</form>
where attributes are the attributes that control
how the form is processed and elements are elements
places within the form.

BTE550 Internet and Intranet Applications

275

Forms and Server-Based Programs

Creating the Form Element

While HTML supports the creation of forms, it does


not include tools to process the information.
The information can be processed through a
program running on a Web server.

Form attributes usually tell the browser the location of the


server-based program to be applied to the forms data.
Always specify an id or name for the form.
Two attributes are available to identify the form: id and
name.

HTM
ML

HTM
ML

BTE550 Internet and Intranet Applications

Input boxes
Selection lists
Drop-down list boxes
Option buttons or radio buttons
Check boxes
Group boxes
Text areas
Form buttons

HTM
ML

Web forms collect information from customers.


Web forms include different control elements including:

274

BTE550 Internet and Intranet Applications

273

BTE550 Internet and Intranet Applications

276

46

Creating the Form Element

Setting the Size of an Input Box

The syntax of the id and name attributes are as follows:

Example:

HTM
ML

HTM
ML

id=id> </form>

<form name=name

<form name=reg
name= reg id=
id=reg>
reg >
</form>

BTE550 Internet and Intranet Applications

277

where type specifies the type of input field, and the


name and id attributes provide the fields name and id.

HTM
ML

<i
<input
t t
type=text
t t name=city
it id
id=city
it />

HTM
ML

<input type=type name=name id=id />

278

button can be clicked to perform an action from a script

browse button to locate and select a file


hidden field, not viewable on the form

type=image

Clickable inline image - performs an action from a


script
text box in which hides text entered by the user

type=radio

radio (option) button

type=reset

button which resets the form when clicked

type=submit
type=text

check box

type=hidden

type=password

link a label with an associated text element for scripting


purposes.
The syntax for creating a form label is as follows:

Description of what is displayed

type=file

281

Working with Form Labels

HTM
ML

HTM
ML

type=checkbox

A password field is an input box where characters typed


by the user are displayed as bullets or asterisks to protect
private or sensitive information on a Web site.
The syntax for creating a Password field is as follows:
<input type=password />

BTE550 Internet and Intranet Applications

Form Input Types: <input type=


type=button

280

Creating a Password Field

The general syntax of input elements is as follows:

Type

<input type=text name=zip id=zip


size=5 />
/

BTE550 Internet and Intranet Applications

Creating Input Boxes

BTE550 Internet and Intranet Applications

By default, an input box displays 20 characters of text.


To change the width of an input box, use the size attribute
which is displayed as follows:
<input size=value />

<label for=zip>Zip Code </label>


Where id is the value of the id attribute for a field on the form,
and
d
label text is the text of the label.

button which submits the form when clicked


text box in which displays text entered by the user

BTE550 Internet and Intranet Applications

279

BTE550 Internet and Intranet Applications

282

47

Creating a Selection List

Modifying the Appearance of a Selection List

list box from which a user selects a particular value or set of


values.
Selection lists are useful when there are a fixed set of possible
responses from the user.

create selection list using <select> tag.


tag
specify each individual selection item using the <option>
tag.

HTM
ML

HTM
ML

A selection list

283

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

Making Multiple Selections

<form id=reg>
<select name=item id=item>
<option> dogs </option>
<option> cats </option>
<option> mice </option>
</select>
</form>

BTE550 Internet and Intranet Applications

Add the multiple attribute to the select element to create


multiple selections

284

BTE550 Internet and Intranet Applications

Modifying the Appearance of a Selection List

5
HTM
ML

5
HTM
ML

by modifying the size attribute.


Syntax:
<select size= value> </select>
Where
Wh value is
i the
h number
b off items
i
that
h the
h selection
l i list
li
displays in the form.

285

287

Working with Option Groups

change the number of options displayed in the selection


list

BTE550 Internet and Intranet Applications

<select multiple=multiple> </select>

HTM
ML

HTM
ML

Select list

286

Use option groups to organize selection lists into distinct groups.


<select attributes>
<optgroup label=label1>
<option>itema1</option>
<option>itema2</option>

<select attributes>
<optgroup label=label1>
<option>itema1</option>
<option>itema2</option>

</optgroup>

</select>
BTE550 Internet and Intranet Applications

288

48

Creating a Text Area Box

BTE550 Internet and Intranet Applications

HTML and XHTML allow you to organize option


buttons into a group of fields called field sets.
Most browsers place a group box around a field set
indicates fields belong to a common group.
<fieldset>
fields
</fieldset>
where fields are the individual fields within a set.

HTM
ML

HTM
ML

Creating a Field Set

289

To create a text area box, use the textarea element:


<textarea name=name id=id rows=value
cols=value>
default text
</textarea>
Where the rows and cols attributes define
dimensions of the input box
rows attribute indicates the number of lines in the input box.

BTE550 Internet and Intranet Applications

Working with Form Buttons

BTE550 Internet and Intranet Applications

Buttons are a type of control element that performs an


action.
Types of buttons:
5

To create a check box, use:


<input type=checkbox name=name id=id
value=value/>
where the name and id attributes identify the check box field
and
the value attribute specifies the value sent to the server if the
check box is selected.
selected
To specify that a check box be selected by default, use the checked
attribute as follows:
<input type=checkbox checked=checked />
or <input type=checkbox checked />

HTM
ML

HTM
ML

Creating Check Boxes

290

Command button
Submit button
Reset button
File button

293

Creating a Command button

Command buttons are created using the <input> tag:


<input type=button value=text />

HTM
ML

Text area boxes allow users to enter comments about the


products theyve purchased.
An input box would be too small to accommodate the
length of text for this use.

HTM
ML

BTE550 Internet and Intranet Applications

Creating a Text Area Box

BTE550 Internet and Intranet Applications

292

291

Submit buttons submit forms to the server for processing


when clicked. Syntax is as follows:
<input
p
type=submit
yp
value=text />
Reset buttons reset forms to their original (default) values.
Syntax is as follows:
<input type=reset value=text />

BTE550 Internet and Intranet Applications

294

49

Working with Form Attributes

BTE550 Internet and Intranet Applications

5
HTM
ML

HTM
ML

Creating a File button


File buttons are
used to select files
so that their
contents can be
submitted for
processing to a
program.
The
Th Web
W b page
then only displays
the files location,
not the files
contents.

295

The method attribute can have one of two values:


Post
Get
The get method is the default
get appends the form data to the end of the URL
specified in the action attribute.
The post method sends form data in a separate data stream
allows the Web server to receive the data through
standard input.

BTE550 Internet and Intranet Applications

Using the mailto Action


The mailto action accesses the users own e-mail
program and uses it to mail form information to a specified
e-mail address.
5

Hidden fields are added to a form, but not displayed in


the Web page.
The syntax is as follows:
<input type=hidden name=name
id=id value=value />

HTM
ML

HTM
ML

Working with Hidden Fields

298

By-passes the need for server-based programs.


Syntax:
<form action-mailto:e-mail_address method=post
enctype=text/plain> </form>

where e-mail_address is the e-mail address of the


recipient in the form.
Newer browsers do not allow this action for security
reasons
BTE550 Internet and Intranet Applications

296

BTE550 Internet and Intranet Applications

Specifying the Tab Order

After adding the elements to your form, youll need to specify


where to send the form data and how to send it.
Use the following attributes:
<form action=url method=type
enctype=type>

</form>

HTM
ML

HTM
ML

Working with Form Attributes

where url specifies the filename and location of the program that
processes
the form and the method attribute specifies how your Web
browser sends data to the server.
The enctype attribute specifies the format of the data stored in
the forms field.
BTE550 Internet and Intranet Applications

299

297

Users typically navigate through a form with the tab key


Specify an alternate tab order by adding the tabindex
attribute to any control element in your form.
Example:
p name=fname tabindex=1 />
<input

assigns the tab index number 1 to the fname


field from the registration form.

BTE550 Internet and Intranet Applications

300

50

An access key
single key typed with the Alt key
in order to jump to one of the control elements in the
form.
Create an access key by adding the accesskey attribute to
any control
t l element.
l
t
Example of creating an access key for the lname field:
<input name=lname accesskey=1 />

The Alt Attribute

HTM
ML

HTM
ML

Specifying an Access Key

301

BTE550 Internet and Intranet Applications

Defines a label to a control


Defines a fieldset
Defines a caption for a fieldset
Defines a selectable list (a drop-down box)
Defines an option group

<option>
<button>

<label>
<fieldset>
<legend>
<select>
<optgroup>

Defines an option in the drop-down box


Defines a push button

BTE550 Internet and Intranet Applications

The <body> tag has two attributes where you can specify
backgrounds. The background can be a color or an image.
The bgcolor attribute specifies a background-color for an
HTML page.
The value of this attribute can be a hexadecimal number,
an RGB value, or a color name
<body bgcolor="#000000">
<body bgcolor="rgb(0,0,0)">
<body bgcolor="black">
The lines above all set the background-color to black

302

In HTML, images are defined with the <img> tag.


The <img> tag is empty, which means that it contains
attributes only and it has no closing tag
To display an image on a page, you need to use the src
attribute
src stands for "source"
The value of the src attribute is the URL of the image you
want to display on your page
The syntax of defining an image:
<img src
src="url">
303

Background

HTM
ML

5
HTM
ML

305

BTE550 Internet and Intranet Applications

The Image Tag and the src Attribute

BTE550 Internet and Intranet Applications

304

Backgrounds

HTM
ML

5
HTM
ML

Description
Defines a form for user input
Defines an input field
Defines a text-area (a multi-line text input control)

The "alt" attribute tells the reader what he or she is


missing on a page if the browser can't load images. The
browser will then display the alternate text instead of the
image.
It is a good practice to include the "alt" attribute for each
image on a page, to improve the display and usefulness of
your document for people who have text-only browsers.
BTE550 Internet and Intranet Applications

Form Tags
Tag
<form>
<input>
<textarea>

The alt attribute is used to define an "alternate text" for an


image.
The value of the alt attribute is an author-defined text
<img src="boat.gif" alt="Big Boat">

The background attribute specifies a background-image


for an HTML page.
The value of this attribute is the URL of the image you
want to use.
If the image is smaller than the browser window, the
image will repeat itself until it fills the entire browser
window
<body background="clouds.gif">
<body
background="http://www.cs.itu.edu.tr/bg.gif">

BTE550 Internet and Intranet Applications

306

51

The bgcolor, background, and the text attributes in the


<body> tag are deprecated in the latest versions of HTML
(HTML 4 and XHTML).
The World Wide Web Consortium (W3C) has removed
these attributes from its recommendations.
Style sheets (CSS) should be used instead (to define the
layout and display properties of HTML elements).

The combination of Red, Green and Blue values from 0 to


255 gives a total of more than 16 million different colors
to play with (256 x 256 x 256).
Most modern monitors are capable of displaying at least
16384 different colors.
If you look at the color table (next slide), you will see the
result of varying the red light from 0 to 255, while
keeping the green and blue light at zero

BTE550 Internet and Intranet Applications

16 Million Different Colors

HTM
ML

5
HTM
ML

Useful Tips

307

Color Values

Red

rgb(0,0,0)
rgb(8,0,0)
rgb(16,0,0)

HTM
ML

Colors are defined using a hexadecimal notation for the


combination of Red, Green, and Blue color values (RGB).
The lowest value that can be given to one light source is 0
(#00). The highest value is 255 (#FF).

HTM
ML

310

BTE550 Internet and Intranet Applications

rgb(255,0,0)
BTE550 Internet and Intranet Applications

308

311

BTE550 Internet and Intranet Applications

HTM
ML

Color Names
A collection of color names is supported by most
browsers.
Only 16 color names are supported by the W3C HTML
4.0 standard (aqua, black, blue, fuchsia, gray, green, lime,
maroon, navy, olive, purple, red, silver, teal, white, and
yellow).
ll )
For all other colors you should use the Color HEX value.

BTE550 Internet and Intranet Applications

309

XHTML

Copyleft 2005, Binnur Kurt

52

All New Browsers Support XHTML

Introduction to XHTML
Why XHTML
XHTML vs HTML
XHTML Syntax
XHTML DTD
XHTML Modules
XHTML Attributes

BTE550 Internet and Intranet Applications

XHTML is compatible with HTML 4.01.


All new browsers have support for XHTML.
XHTM
ML 6

XHTM
ML 6

Content

313

BTE550 Internet and Intranet Applications

Why XHTML?

XHTML stands for EXtensible HyperText Markup


Language
XHTML is aimed to replace HTML
XHTML is almost identical to HTML 4.01
XHTML is a stricter and cleaner version of HTML
XHTML is HTML defined as an XML application
XHTML is a W3C Recommendation

BTE550 Internet and Intranet Applications

XHTM
ML 6

XHTM
ML 6

What is XHTML?

314

317

Why XHTML?

XHTML 1.0 became a W3C Recommendation January


26, 2000.
A W3C Recommendation means that the specification is
stable, that it has been reviewed by the W3C membership,
and that the specification is now a Web standard.
W3C defines XHTML as the latest version of HTML.
XHTML will gradually replace HTML.

315

XHTM
ML 6

XHTM
ML 6

We have reached a point where many pages on the WWW contain


"bad" HTML.
The following HTML code will work fine if you view it in a
browser, even if it does not follow the HTML rules:
<html>
<head>
<title>This is bad HTML</title>
<body>
<h1>Bad HTML
</body>
XML is a markup language where everything has to be marked up
correctly, which results in "well-formed" documents.
XML was designed to describe data and HTML was designed to
display data.
BTE550 Internet and Intranet Applications

XHTML is a W3C Recommendation

BTE550 Internet and Intranet Applications

316

Today's market consists of different browser technologies,


some browsers run Internet on computers, and some
browsers run Internet on mobile phones and hand helds.
Therefore - by combining HTML and XML, and their
strengths, we got a markup language that is useful now and
in the future - XHTML.
XHTML
XHTML pages can be read by all XML enabled devices
AND while waiting for the rest of the world to upgrade to
XML supported browsers, XHTML gives you the
opportunity to write "well-formed" documents now, that
work in ALL browsers and that are backward browser
compatible
BTE550 Internet and Intranet Applications

318

53

Quiz

XHTML is the next generation of HTML, but it will of


course take some time before browsers and other software
products are ready for it.
In the meantime there are some important things you can
do to prepare yourself for it.
XHTML is not very different from HTML 4.01, so
bringing your code up to 4.01 standards is a very good
start.
In addition, you should start NOW to write your HTML
code in lowercase letters, and NEVER make the bad habit
of skipping end tags like the </p>
BTE550 Internet and Intranet Applications

XHTM
ML 6

XHTM
ML 6

How To Get Ready For XHTML

319

<ul>
<li>Coffee</li>
<li>Tea
<ul>
<li>Black tea</li>
<li>Green tea</li>
</ul>
<li>Milk</li>
</ul>

XHTM
ML 6

Answer

XHTM
ML 6

XHTML elements must be properly nested


XHTML documents must be well-formed
Tag names must be in lowercase
All XHTML elements must be closed

320

<ul>
<li>Coffee</li>
<li
li>Tea
<ul>
<li>Black tea</li>
<li>Green tea</li>
</ul>
</li
li>
<li>Milk</li>
</ul>
BTE550 Internet and Intranet Applications

323

Elements Must Be Properly Nested

Documents Must Be Well-formed

In HTML some elements can be improperly nested within


each other like this:
<b
b><ii>This text is bold and italic</b
b></ii>
In XHTML all elements must be properly nested within
each other like this:
<b
b><ii>This text is bold and italic</ii></b
b>

All XHTML elements must be nested within the <html>


root element.
All other elements can have sub (children) elements.
Sub elements must be in pairs and correctly nested within
their parent element.
The basic document structure is:
<html>
<head> ... </head>
<body> ... </body>
</html>

BTE550 Internet and Intranet Applications

321

XHTM
ML 6

XHTM
ML 6

The Most Important Differences

BTE550 Internet and Intranet Applications

322

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

324

54

Important Compatibility Note

XHTML documents are XML applications.


XML is case-sensitive.
Tags like <br> and <BR> are interpreted as different tags.
This is wrong:
<BODY
BODY>
<P
P>This is a paragraph</P
P>
</BODY
BODY>
This is correct:
<body
body>
<p
p>This is a paragraph</p
p>
</body
body>

To make your XHTML compatible with today's browsers,


you should add an extra space before the "/" symbol like
this: <br />, and this: <hr />.

BTE550 Internet and Intranet Applications

XHTM
ML 6

XHTM
ML 6

Tag Names Must Be In Lower Case

325

BTE550 Internet and Intranet Applications

Some More XHTML Syntax Rules

Non-empty elements must have an end tag.


This is wrong:
<p>This is a paragraph
<p>This is another paragraph
This is correct:
<p>This is a paragraph</p>
<p>This is another paragraph</p>

BTE550 Internet and Intranet Applications

XHTM
ML 6

XHTM
ML 6

All XHTML Elements Must Be Closed

326

Attribute names must be in lower case


Attribute values must be quoted
Attribute minimization is forbidden
The id attribute replaces the name attribute
The XHTML DTD defines mandatory elements

BTE550 Internet and Intranet Applications

Empty Elements Must Also Be Closed

329

Attribute Names Must Be In Lower Case

Empty elements must either have an end tag or the start


tag must end with />.
This is wrong:
This is a break<br>
Here comes a horizontal rule:<hr>
Here's an image <img src="happy.gif" alt="Happy face">

XHTM
ML 6

XHTM
ML 6

328

This is correct:

This is wrong:
<table WIDTH
WIDTH="100%">
This is correct:
<table width="100%">

This is a break<br/>
Here comes a horizontal rule:<hr/>
Here's an image <img src="happy.gif" alt="Happy face"/>

BTE550 Internet and Intranet Applications

327

BTE550 Internet and Intranet Applications

330

55

The id Attribute Replaces The name Attribute

This is wrong:
<table width=100%
100%>
This is correct:
<table width="
"100%"
">

XHTM
ML 6

XHTM
ML 6

Attribute Values Must Be Quoted

331

BTE550 Internet and Intranet Applications

HTML 4.01 defines a name attribute for the elements a,


applet, frame, iframe, img, and map. In XHTML the name
attribute is deprecated. Use id instead.
This is wrong:
<img src="picture.gif" name
name="picture1" />
This is correct:
<img src="picture.gif" id
id="picture1" />
To interoperate with older browsers for a while, you
should use both name and id, with identical attribute
values, like this:
<img src="picture.gif" id="picture1" name="picture1" />
BTE550 Internet and Intranet Applications

The Lang Attribute

This is wrong:
<input checked
checked>
<input readonly>
<input disabled>
<option selected>
frame noresize
noresize>
<frame
This is correct:
<input checked="checked"
"checked" />
<input readonly="readonly" />
<input disabled="disabled" />
<option selected="selected" />
<frame noresize="noresize" />

XHTM
ML 6

XHTM
ML 6

Attribute Minimization Is Forbidden

332

BTE550 Internet and Intranet Applications

The lang attribute applies to almost every XHTML


element. It specifies the language of the content within an
element.
If you use the lang attribute in an element, you must add
the xml
xml:lang
lang attribute, like this:
<div lang="no" xml
xml:lang
lang="no">Heia Norge!</div>

BTE550 Internet and Intranet Applications

Here is a list of the


minimized attributes in
HTML and how they should
be written in XHTML:

BTE550 Internet and Intranet Applications

compact

compact="compact"

checked

checked="checked"

declare

declare="declare"

readonly

readonly="readonly"

disabled

disabled="disabled"

selected

selected="selected"
selected=
selected

defer

defer="defer"

ismap

ismap="ismap"

nohref

nohref="nohref"

noshade

noshade="noshade"

nowrap

nowrap="nowrap"

multiple

multiple="multiple"

noresize

noresize="noresize"

335

Mandatory XHTML Elements

XHTML

XHTM
ML 6

XHTM
ML 6

Attribute Minimization Is Forbidden


HTML

334

All XHTML documents must have a DOCTYPE declaration.


The html, head and body elements must be present, and the title
must be present inside the head element.
This is a minimum XHTML document template:
<!DOCTYPE Doctype goes here>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
h d
<title>Title goes here</title>
</head>
<body>
Body text goes here
</body>
</html>

333

BTE550 Internet and Intranet Applications

336

56

An XHTML Example

The DOCTYPE declaration is not a part of the XHTML


document itself. It is not an XHTML element, and it
should not have a closing tag.
The xmlns attribute inside the <html> tag is required in
XHTML. However, the validator on w3.org does not
complain
l i when
h this
hi attribute
ib is
i missing
i i in
i an XHTML
document.
This is because "xmlns
xmlns=http://www.w3.org/1999/xhtml"
is a fixed value and will be added to the <html> tag even
if you do not include it.

BTE550 Internet and Intranet Applications

XHTM
ML 6

XHTM
ML 6

Mandatory XHTML Elements

337

BTE550 Internet and Intranet Applications

338

341

The 3 Document Type Definitions

This is a simple (minimal) XHTML document:


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>simple document</title>
</head>
<body>
<p>a simple paragraph</p>
</body>
</html>

XHTM
ML 6

XHTM
ML 6

DTD specifies the syntax of a web page in SGML.


DTD is used by SGML applications, such as HTML, to
specify rules that apply to the markup of documents of a
particular type, including a set of element and entity
declarations.
XHTML is specified in an SGML document type
definition or 'DTD'.
An XHTML DTD describes in precise, computer-readable
language, the allowed syntax and grammar of XHTML
markup.
BTE550 Internet and Intranet Applications

An XHTML Example

BTE550 Internet and Intranet Applications

340

The 3 Document Type Definitions

XHTM
ML 6

XHTM
ML 6

The <!DOCTYPE> Is Mandatory


An XHTML document consists of three main parts:
the DOCTYPE
the Head
the Body
The basic document structure is:
<!DOCTYPE ...>
<html>
<head>
<title>... </title>
</head>
<body> ... </body>
</html>
The DOCTYPE declaration should always be the first line in an
XHTML document.
BTE550 Internet and Intranet Applications

The DOCTYPE declaration defines the document type:


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
The rest of the document looks like HTML:
<html>
<head>
<title>simple document</title>
</head>
<body>
<p>a simple paragraph</p>
</body>
</html>

339

XHTML 1.0 specifies three XML document types that


correspond to three DTDs:
Strict
Transitional
Frameset

BTE550 Internet and Intranet Applications

342

57

Why XHTML Modularization?

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1
xhtml1--strict.dtd">
strict.dtd
Use this when you want really clean markup, free of
presentational clutter.
p
Use this together with Cascading Style Sheets.

BTE550 Internet and Intranet Applications

XHTM
ML 6

XHTM
ML 6

XHTML 1.0 Strict

343

XHTML is a simple but large language, containing most


of the functionality a web developer will need.
For some purposes XHTML is too large and complex, and
for other purposes it is much too simple.
By splitting XHTML into modules, the W3C (World
Wide web Consortium) has created small and well-defined
sets of XHTML elements that can be used separately for
simple devices as well as combined with other XML
standards into larger and more complex applications.

Why XHTML Modularization?

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1
xhtml1-transitional.dtd">
transitional.dtd
Use this
when you need to take advantage of HTML's
presentational features
when you want to support browsers that don't
understand Cascading Style Sheets.

XHTM
ML 6

XHTM
ML 6

XHTML 1.0 Transitional

BTE550 Internet and Intranet Applications

344

With modular XHTML, product and application designers


can:
Choose the elements to be supported by a device using
standard XHTML building blocks.
Add extensions to XHTML, using XML, without
breaking the XHTML standard.
Simplify XHTML for devices like hand held computers,
mobile phones, TV, and home appliances.
Extend XHTML for complex applications by adding
new XML functionality (like MathML, SVG, Voice and
Multimedia).
Define XHTML profiles like XHTML Basic (a subset of
XHTML for mobile devices).

XHTML Modules

345

W3C has split the definition off XHTML into 28 modules:

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1
xhtml1--frameset.dtd">
frameset.dtd
Use this when you want to use HTML Frames to partition
the browser window into two or more frames.

XHTM
ML 6

XHTM
ML 6

347

BTE550 Internet and Intranet Applications

XHTML 1.0 Frameset

BTE550 Internet and Intranet Applications

346

BTE550 Internet and Intranet Applications

Module name

Description

Applet Module

Defines the deprecated* applet element.

Base Module

Defines the base element.

Basic Forms Module

Defines the basic forms elements.

Basic Tables Module

Defines the basic table elements.

Bi-directional
Bi
directional Text Module

Defines the bdo element.

Client Image Map Module

Defines browser side image map elements.

Edit Module

Defines the editing elements del and ins.

Forms Module

Defines all elements used in forms.

Frames Module

Defines the frameset elements.

Hypertext Module

Defines the a element.

Iframe Module

Defines the iframe element.

Image Module

Defines the img element.

BTE550 Internet and Intranet Applications

348

58

Defines the img element.

Intrinsic Events Module

Defines event attributes like onblur and onchange.

Legacy Module

Defines deprecated* elements and attributes.

Link Module

Defines the link element.

List Module

Defines the list elements ol, li, ul, dd, dt, and dl.

Metainformation Module

Defines the meta element.

Name Identification Module

Defines the deprecated* name attribute.

Object Module

Defines the object and param elements.

Presentation Module

Defines presentation elements like b and i.

Scripting Module

Defines the script and noscript elements.

Server Image Map Module

Defines server side image map elements.

Structure Module

Defines the elements html, head, title and body.

Style Attribute Module

Defines the style attribute.

Style Sheet Module

Defines the style element.

Tables Module

Defines the elements used in tables.

Target Module

Defines the target attribute.

Text Module

Defines text container elements like p and h1.

Language Attributes
Not valid in base, br, frame, frameset, hr, iframe, param,
and script elements.
XHTM
ML 6

XHTM
ML 6

Image Module

349

BTE550 Internet and Intranet Applications

Attribute
dir
lang

XHTM
ML 6

XHTM
ML 6

350

Attribute

Value

accesskey

character

tabindex

number

Description
Sets a keyboard shortcut to access
an element
Sets the tab order of an element

BTE550 Internet and Intranet Applications

Core Attributes

353

XHTML Event Attributes

XHTM
ML 6

Not valid in base, head, html, meta, param, script, style,


and title elements.
XHTM
ML 6

352

Keyboard Attributes

XHTML tags can have attributes.


The special attributes for each tag are listed under each tag
description.
The attributes listed here are the core and language
attributes that are standard for all tags (with a few
exceptions).

Attribute

Value

Description

class

class_rule or style_rule

The class of the element

id

id_name

A unique id for the element

style

style_definition

An inline style definition

title

tooltip_text

A text to display in a tool tip

BTE550 Internet and Intranet Applications

Description
Sets the text direction
Sets the language code

BTE550 Internet and Intranet Applications

XHTML Standard Attributes

BTE550 Internet and Intranet Applications

Value
ltr | rtl
language_code

351

New to HTML 4.0 was the ability to let HTML events


trigger actions in the browser, like starting a JavaScript
when a user clicks on an HTML element. Below is a list
of attributes that can be inserted into HTML tags to define
event actions.

We
will
ill learn
l
more about
b
programming
i with
i h these
h
events
in studying JavaScript and DHTML.

BTE550 Internet and Intranet Applications

354

59

Window Events

Mouse Events

XHTM
ML 6

Only valid in body and frameset elements


Attribute

Value

Description

onload

script

Script to be run when a document


loads

onunload

Script to be run when a document


unloads

script

XHTM
ML 6

Not valid in base, bdo, br, frame, frameset, head, html, iframe,
meta, param, script, style, and title elements.
Attribute

Value

onclick
ondblclick

script What to do on a mouse click


script What to do on a mouse doubleclick

onmousedown script What to do when mouse button is pressed


onmousemove script What to do when mouse pointer moves
onmouseover
onmouseout
onmouseup

355

BTE550 Internet and Intranet Applications

Description

What to do when mouse pointer moves over an


element
What to do when mouse pointer moves out of an
script
element
script

script What to do when mouse button is released


358

BTE550 Internet and Intranet Applications

Form Element Events

Attribute

Value

Description

onchange
onsubmit

script
script

Script to be run when the element changes


Script to be run when the form is submitted

onreset

script

S i to be
Script
b run when
h the
h form
f
iis reset

onselect
onblur

script
script

Script to be run when the element is selected


Script to be run when the element loses focus

onfocus

script

Script to be run when the element gets focus

XHTM
ML 6

XHTM
ML 6

Only valid in form elements.

356

BTE550 Internet and Intranet Applications

Quiz

Keyboard Events

Question # 1

XHTM
ML 6

XHTM
ML 6

Not valid in base, bdo, br, frame, frameset, head, html,


iframe, meta, param, script, style, and title elements.

Attribute

Value

onkeydown

script

What to do when key is pressed

onkeypress

script

What to do when key is pressed and


released

onkeyup

script

What to do when key is released

BTE550 Internet and Intranet Applications

359

BTE550 Internet and Intranet Applications

Description

357

a.
b.
c.
d
d.

What does XHTML stand for?


EXtensible HyperText Markup Language
EXtreme HyperText Markup Language
EXtensible HyperText Marking Language
EXtra Hyperlinks and Text Markup Language

BTE550 Internet and Intranet Applications

360

60

Question # 2

Question # 5

XHTM
ML 6

XHTM
ML 6

XHTML is a Web standard


a. True
b. False

361

BTE550 Internet and Intranet Applications

a.
b.
c.
d
d.

What is the correct XHTML for a paragraph?


</p><p>
<P></P>
<P></p>
<p></p>

Question # 3

Question # 6

XHTM
ML 6

XHTM
ML 6

XML and HTML will be replaced by XHTML


a. False
b. True

362

BTE550 Internet and Intranet Applications

a.
b.
c.

What is a correct XHTML tag for a line break?


<br>
<br />
<break/>

365

BTE550 Internet and Intranet Applications

Question # 4

Question # 7

XHTM
ML 6

XHTM
ML 6

HTML will be replaced by XHTML


a. False
b. True

BTE550 Internet and Intranet Applications

364

BTE550 Internet and Intranet Applications

363

What is the correct XHTML for an attribute and its


value?
a. WIDTH="80"
b. width=80
c. WIDTH=80
d. width="80"

BTE550 Internet and Intranet Applications

366

61

Question # 8

Question # 11

XHTM
ML 6

XHTM
ML 6

All elements in XHTML must be closed


a. True
b. False

367

BTE550 Internet and Intranet Applications

Which elements are mandatory in an XHTML


document?
a. doctype, html, head, body, and title
b. doctype, html and body
yp , html,, head,, and bodyy
c. doctype,

BTE550 Internet and Intranet Applications

Question # 9

Question # 12

Is this correct XHTML?

XHTML documents must be "well-formed"


a. True
b. False

<ul>
<li>Coffee</li>
<li>Tea
<ul>
li Black tea
tea</li>
/li
<li>Black
<li>Green tea</li>
</ul>
<li>Milk</li>
</ul>

XHTM
ML 6

XHTM
ML 6

370

a. No
b. Yes
BTE550 Internet and Intranet Applications

368

BTE550 Internet and Intranet Applications

Question # 10

Question # 13

XHTM
ML 6

XHTM
ML 6

The DOCTYPE declaration has no closing tag


a. True
b. False

BTE550 Internet and Intranet Applications

371

369

a.
b.
c.

What XHTML code is "well-formed"?


<p>A <b><i>short</i></b> paragraph</p>
<p>A <b><i>short</i></b> paragraph
<p>A <b><i>short</b></i> paragraph</p>

BTE550 Internet and Intranet Applications

372

62

Question # 17

Which of the following is the right use of the lang


attribute?
a. <div language="en">Hello World!</div>
b. <div xml:language="en">Hello World!</div>
g
xml:lang="en">Hello
g
World!</div>
c. <div lang="en"

BTE550 Internet and Intranet Applications

XHTM
ML 6

XHTM
ML 6

Question # 14

373

a.
b.
c.

BTE550 Internet and Intranet Applications

Question # 18

Which attribute replaces the name attribute?


None
The src attribute
The id attribute

BTE550 Internet and Intranet Applications

Do all XHTML documents require a doctype?


a. Yes
b. No

374

BTE550 Internet and Intranet Applications

Question # 16

377

Question # 19

XHTM
ML 6

XHTM
ML 6

Is attribute minimization allowed in XHTML?


a. Yes
b. No

BTE550 Internet and Intranet Applications

376

XHTM
ML 6

XHTM
ML 6

Question # 15

a.
b.
c.

What are the different DTDs in XHTML?


Strict, Transitional, Frameset
Strict, Transitional, Loose
Strict, Transitional, Loose, Frameset

375

a.
b.
c.
d
d.

What is the most common XHTML DTD?


Loose
Normal
Frameset
Transitional

BTE550 Internet and Intranet Applications

378

63

Question # 20

XSL

XSLT
LT 7

XHTM
ML 6

All XHTML tags and attributes must be in lower case


a. False
b. True

379

BTE550 Internet and Intranet Applications

XSL = eXtensible Stylesheet Language


the Stylesheet Language for XML
implemented in XML
includes a transformation language XSLT
includes a formatting language XSLFOXSL
XSLFO XSL
Formatting Objects
defines how a source document should be transformed
to provide an output typically in XML or HTML
XPath is a language to define XML parts or patterns

382

BTE550 Internet and Intranet Applications

XSLT

XSLT
LT 7

XSLT

BTE550 Internet and Intranet Applications

Copyleft 2005, Binnur Kurt

383

XSLT (Cont)

Define XSL and XSLT


Describe the main components of an XSL document
Write a Java program that uses the TrAX and Xalan
Transform an XML document using the Xalan XSLT
processor and a stylesheet
p
y

XSLT
LT 7

XSLT
LT 7

Content

BTE550 Internet and Intranet Applications

XSLT - XSL Transformations


XSLT Version 1.0 is a W3C Recommendation 16
November 1999
XSLT is designed for use as part of
XSL - A transformation expressed
p
in
XSLT is called a stylesheet.
XSLT processors must use the XML namespaces
mechanism to recognize elements

381

XSLT is incredibly useful in


transforming data into a viewable format in a
browser/phone/PDA (Presentation Oriented Publishing
(POP)) - data to presentation
transforming business data between content models
(Message Oriented Middleware (MOM))
Transmitting data between applications

BTE550 Internet and Intranet Applications

384

64

XSLT in MOM (Cont)

XML document separates content from presentation


Transformations can be used to style (render, present)
XML documents
A common styling technique presents XML in HTML
format

BTE550 Internet and Intranet Applications

XSLT
LT 7

XSLT
LT 7

XSLT in POP

385

BTE550 Internet and Intranet Applications

XSLT
LT 7

Transformation process

XSLT
LT 7

XSLT in POP (Cont)

BTE550 Internet and Intranet Applications

386

389

XSLT Data Transformation

Important for eCommerce, B2B/EDI, and dynamic


content generation
Different content model
Different structural relationship
Different vocabularies

XSLT
LT 7

XSLT
LT 7

XSLT uses XPath to define parts of the source document


that match one or more predefined templates.
When a match is found, XSLT will transform the
matching part of the source document into the result
document.
Transforming XML document into
Another XML document
XML
XHTML
WML
HTML document
BTE550 Internet and Intranet Applications

XSLT in MOM

BTE550 Internet and Intranet Applications

388

387

BTE550 Internet and Intranet Applications

390

65

XSLT
LT 7

XPath

XSLT
LT 7

XSLT Operational Model

BTE550 Internet and Intranet Applications

391

Used by XSLT (and by other XML technologies such as


XPointer) for referencing elements and attributes internal
to an XML document
Defines expression language (pattern) for referencing
Supports a tree structure expression
7th child element of the third person element

XPath (Cont)

Piece of software
Reads an XSLT stylesheet and input XML document
Converts the input document into an output document
According to the instruction given in the stylesheet
Called stylesheet processor sometimes

XSLT
LT 7

XSLT
LT 7

XSLT Processor

BTE550 Internet and Intranet Applications

392

XPath expression results in a node set


A node set of person elements under people
element
Various functions can be used on node sets, including:
not()
() eliminate a specific
p
node
position() return the position within a node set
count() returns the number of nodes in a node set

BTE550 Internet and Intranet Applications

395

XML Example Document


<?xml version="1.0"?>
<people>
<person born="1912" died="1954">

Built-in within a browser


IE 6
Built-in within web or application server
Apache Cocoon
Standalone
Michael Kays SAXON
Apache.orgs Xalan

XSLT
LT 7

XSLT
LT 7

Examples of XSLT Processor

<name>
<first_name>Alan</first_name>
<last_name>Turing</last_name>
</name>
<profession>computer scientist</profession>
<profession>mathematician</profession>
p
yp g p
p
<profession>cryptographer</profession>

</person>
<person born="1918" died="1988">
<name>
<first_name>Richard</first_name>
<middle_initial>M</middle_initial>
<last_name>Feynman</last_name>
</name>
<profession>physicist</profession>
<hobby>Playing the bongoes</hobby>

</person>
</people>
BTE550 Internet and Intranet Applications

394

BTE550 Internet and Intranet Applications

393

BTE550 Internet and Intranet Applications

s1.xml
396

66

Result of XSLT Processing

Genuine XML document


Root element typically is
stylesheet or transform
Both are defined in standard XSLT namespace
http://www w3 org/XSL/Transform
http://www.w3.org/XSL/Transform
xsl as customary prefix
XSLT processor should understand both

XSLT
LT 7

XSLT
LT 7

XSLT Stylesheet

397

BTE550 Internet and Intranet Applications

XSLT
LT 7

XSLT
LT 7

s1.xsl

398

401

xml-stylesheet Processing Instruction

Implements XSLT 1.0 and Xpath 1.0


Can be run from both the command line and within
application code
Support scripting extension
y
Command line syntax:
java org.apache.xalan.xslt.Process
-IN <input document>
-XSL <stylesheet>
-OUT <output document>

Included as part of XML document


Tells XML-ware browser where to find associated
stylesheet
XSLT
LT 7

XSLT
LT 7

Applying empty stylesheet to any XML document


Elements are traversed sequentially
Content of each element is put in output
Attributes are NOT traversed
Default behavior
Without any specific templates
XSLT processor falls back to default behavior
Need for templates

BTE550 Internet and Intranet Applications

Apache Xalan

BTE550 Internet and Intranet Applications

400

Result of XSLT Processing

java org.apache.xalan.xslt.Process
IN s1.xml XSL s1.xsl OUT s1.html

BTE550 Internet and Intranet Applications

s1.html

BTE550 Internet and Intranet Applications

Minimal but Complete XSLT Stylesheet


<?xml version="1.0"?>
<xsl:stylesheet version="1.0
xmlns:xsl="http://www.w3.org/1999/
XSL/Transform">
</xsl:stylesheet>

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


Alan
Turing
computer scientist
mathematician
cryptographer
Richard
M
Feynman
physicist
Playing the bongoes

399

<?xml version=1.0?>
<?xml-stylesheet
type=text/xml
href=http://www.oreilly.com/styles/people.xsl?>
<people>
.
BTE550 Internet and Intranet Applications

402

67

Result

Controls which output is created from which input


xsl:template element form
match attribute contains an Xpath expression
Xpath expression identifies input node set it
matches
For each node in the node set, the template contents
(things between xsl:template tags) are instantiated and
inserted into the output tree

BTE550 Internet and Intranet Applications

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

XSLT
LT 7

XSLT
LT 7

Templates

403

Very Simple XSLT Stylesheet # 2

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="people">
p
</xsl:template>
</xsl:stylesheet>

XSLT
LT 7

XSLT
LT 7

Very Simple XSLT Stylesheet # 1

Simplest form of Xpath pattern is a name of a single element

BTE550 Internet and Intranet Applications

404

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match=people">
Hello World!
</xsl:template>
</xsl:stylesheet>

407

BTE550 Internet and Intranet Applications

XML Example Document

Result

<people>

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


Hello World!

<person born="1912" died="1954">


<name>
<first_name>Alan</first_name>
<last_name>Turing</last_name>
</name>
<profession>computer scientist</profession>
<profession>mathematician</profession>
<profession>cryptographer</profession>

XSLT
LT 7

XSLT
LT 7

406

BTE550 Internet and Intranet Applications

</person>
<person born="1918" died="1988">
<name>
<first_name>Richard</first_name>
<middle_initial>M</middle_initial>
<last_name>Feynman</last_name>
</name>
<profession>physicist</profession>
<hobby>Playing the bongoes</hobby>

</person>

</people>
BTE550 Internet and Intranet Applications

405

BTE550 Internet and Intranet Applications

408

68

Very Simple XSLT Stylesheet # 4

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="person">
A Person
</xsl:template>
</xsl:stylesheet>
Literal data characters - text copied from the stylesheet into
the output document

XSLT
LT 7

XSLT
LT 7

Very Simple XSLT Stylesheet # 3

409

BTE550 Internet and Intranet Applications

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="person">
A Person
</xsl:template>
</xsl:stylesheet>
Same stylesheet with example 3 but with different input
XML document

XML Example Document

XSLT
LT 7

XSLT
LT 7

XML Example Document


<people>
<person born="1912" died="1954">
<name>
<first_name>Alan</first_name>
<last_name>Turing</last_name>
</name>
<profession>computer scientist</profession>
<profession>mathematician</profession>
<profession>cryptographer</profession>
</person
person>
<person born="1918" died="1988">
<name>
<first_name>Richard</first_name>
<middle_initial>M</middle_initial>
<last_name>Feynman</last_name>
</name>
<profession>physicist</profession>
<hobby>Playing the bongoes</hobby>
</person
person>
</people>
410

BTE550 Internet and Intranet Applications

<people
people>
<person
person born="1912" died="1954">
<name>
<first_name>Alan</first_name>
<last_name>Turing</last_name>
</name>
<profession>computer scientist</profession>
<profession>mathematician</profession>
<profession>cryptographer</profession>
</person>
<person
person born="1918" died="1988">
...
</person>
Some text here under people element!
<some-tag>
some information inside some-tag
</some-tag>
</people>

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

XSLT
LT 7

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


A Person
A Person
XSLT
LT 7

413

BTE550 Internet and Intranet Applications

Result

Whitespace outside of person element preserved


person element is replaced by contents of template

BTE550 Internet and Intranet Applications

412

BTE550 Internet and Intranet Applications

411

A Person

Template content

A Person

Template
p
content

Some text here under people element!

default

some information inside some-tag

default

BTE550 Internet and Intranet Applications

414

69

Example Stylesheet

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="person">
<p>A
<p>
A Person </p>
</xsl:template>
</xsl:stylesheet>
Literal data characters - text copied from the stylesheet into
the output document

XSLT
LT 7

XSLT
LT 7

Very Simple XSLT Stylesheet # 5

415

BTE550 Internet and Intranet Applications

Extract names of all the people


<?xml version="1.0"?>
<xsl:stylesheet version="1.0
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
xsl:template match
match="person">
person
<xsl:template
<p>
<xsl:value-of select="name"/>
</p>
</xsl:template>
</xsl:stylesheet>

Result

XML Example Document

Template content contains tags and character data


They have to be well-formed XML

XSLT
LT 7

XSLT
LT 7

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


<p>A Person</p>
<p>A Person</p>

BTE550 Internet and Intranet Applications

416

<people>
<person born="1912" died="1954">
<name>
<first_name>Alan
Alan</first_name>
<last_name>Turing
Turing</last_name>
</name>
<profession>computer scientist</profession>
<profession>mathematician</profession>
<profession>cryptographer</profession>
</person>
<person born="1918" died="1988">
<name>
<first_name>Richard
Richard</first_name>
<middle_initial>M
M</middle_initial>
<last_name>Feynman
Feynman</last_name>
</name>
<profession>physicist</profession>
<hobby>Playing the bongoes</hobby>
</person>
</people>

Result

Extracts the string value of an element or an attribute and


writes it to output
text content of the element after all the tags have been
removed and entity references are resolved
select attribute containing XPath expression identifies an
element or an attribute
It could be a node set, in which case, the string value of
first node is taken

417

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

XSLT
LT 7

XSLT
LT 7

419

BTE550 Internet and Intranet Applications

xsl:value--of Element
xsl:value

BTE550 Internet and Intranet Applications

418

BTE550 Internet and Intranet Applications

<p>
Alan
Turing
</p>
<p>
Richard
M
Feynman
</p>
BTE550 Internet and Intranet Applications

420

70

xsl:apply-templates Example

XSLT processor reads (traverses) the input XML


document sequentially from top to bottom
Templates are activated in the order they match elements
encountered
Template for a parent will be activated before the
children

BTE550 Internet and Intranet Applications

XSLT
LT 7

XSLT
LT 7

xsl:apply-templates

421

XSLT
LT 7

XSLT
LT 7

xsl:apply-templates Example

The order of the traversal can be changed by applytemplates


It can specify which element or elements should be
processed next
It can specify an element or elements should be
processed in the middle of processing another element
It can prevent particular elements from being processed

422

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="name">
<xsl:value-of
<xsl:value
of select
select="last
last_name
name"/>
/>,
<xsl:value-of select="first_name"/>
</xsl:template>
<!-<!
-- Something is missing here --->
>
</xsl:stylesheet>

Result

xsl:apply
xsl:apply--templates lets you make your choice of
processing order explicit
select attribute contains XPath expression telling the
XSLT processor which nodes to process in the input tree
The apply-templates with no select attribute means all
elements relative to the current element (context node)
should be matched

423

XSLT
LT 7

XSLT
LT 7

425

BTE550 Internet and Intranet Applications

xsl:apply-templates (Cont)

BTE550 Internet and Intranet Applications

424

BTE550 Internet and Intranet Applications

xsl:apply-templates (Cont)

BTE550 Internet and Intranet Applications

I would like the output to look like as following


Last name then first name
Only name not profession nor hobby
<?xml version=1.0 encoding=utf-8?>
Turing
Alan
Feyman
Richard

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


Turing
Alan
computer scientist
mathematician
cryptographer
Feyman
Richard
physicist
Playing the bongoes
BTE550 Internet and Intranet Applications

426

71

xsl:apply-templates

XSLT
LT 7

XSLT
LT 7

xsl:apply-templates Example
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="name">
<xsl:value-of select="last_name"/>,
<xsl:value-of
<xsl:value
of select="first
select first_name
name"/>
/>
</xsl:template>
<!-- Apply templates only to name children -->
<xsl:template match="person">
<xsl:apply-templates select="name"/>
</xsl:template>
</xsl:stylesheet>
427

BTE550 Internet and Intranet Applications

xsl:apply-templates (Cont)

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Apply templates only to name children -->
<xsl:template match="person">
<xsl:apply-templates select="name"/>
</xsl:template>
<xsl:template match="name">
<xsl:value-of select="last_name"/>,
<xsl:value-of select="first_name"/>
</xsl:template>
</xsl:stylesheet>

XSLT
LT 7

XSLT
LT 7

Order of templates does not matter

428

Replace every people element with html element


Process all person children of the current people element
Insert the output of any matched templates into the output
documents body element

Example

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

Alan
Turing

XSLT
LT 7

XSLT
LT 7

431

BTE550 Internet and Intranet Applications

Result

Richard
M
Feynman
BTE550 Internet and Intranet Applications

430

BTE550 Internet and Intranet Applications

xsl:apply-templates Example

BTE550 Internet and Intranet Applications

Also useful when child elements have templates of their


own
<xsl:template match="people">
<html>
<head><title>Famous Scientists</title></head>
<body>
<xsl:apply-templates select=person/>
</body>
</html>
</xsl:template>

429

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="people">
<html>
<head><title>Famous Scientists</title></head>
<body> <xsl:apply-templates/> </body>
</html>
</xsl:template>
<xsl:template match="person">
<xsl:apply-templates select="name"/>
</xsl:template>
<xsl:template match="name">
<p><xsl:value-of select="last_name"/>,
<xsl:value-of select="first_name"/></p>
</xsl:template>
</xsl:stylesheet>
BTE550 Internet and Intranet Applications

432

72

Result

<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF8">
<title>Famous Scientists</title>
</head>
<body>
<p>Alan</p>
<p>Turing</p>
<p>Richard</p>
<p>Feynman</p>
</body>
</html>

XSLT
LT 7

XSLT
LT 7

Result

433

BTE550 Internet and Intranet Applications

<html>
<head>
<META http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>Famous Scientists</title>
</head>
<body>
<dl>
<dt>
Alan
Turing
</dt>
<dd>
<ul>
<li>Born: 1912</li>
<li>Died: 1954</li>
</ul>
</dd>

XSLT
LT 7

XSLT
LT 7

436

Modes

434

Same input content needs to appear multiple times in the


output document formatted according to different template
Titles of chapters
Table of contents
p
themselves
In the chapters
mode attribute
xsl:template
xsl:apply-templates

BTE550 Internet and Intranet Applications

Attributes

437

Example with mode attribute

XSLT
LT 7

XSLT
LT 7

Default rule does not apply


apply-templates has to be present in order to output
values of attributes

BTE550 Internet and Intranet Applications

</dl>
</body>
</html>

BTE550 Internet and Intranet Applications

Attributes
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="people">
<html>
<head><title>Famous Scientists</title></head>
<body>
<dl>
<xsl:apply-templates/>
</dl>
</body>
/b d
</html>
</xsl:template>
<xsl:template match="person">
<dt><xsl:apply-templates select="name"/></dt>
<dd><ul>
<li>Born: <xsl:apply-templates select="@born"/></li>
<li>Died: <xsl:apply-templates select="@died"/></li>
</ul></dd>
</xsl:template>
</xsl:stylesheet>
BTE550 Internet and Intranet Applications

<dt>
Richard
M
Feynman
</dt>
<dd>
<ul>
<li>Born: 1918</li>
<li>Died: 1988</li>
</ul>
</dd>

435

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="people">
<html>
<head><title>Famous Scientists</title></head>
<body>
<ul><xsl:apply-templates select="person" mode="toc"/></ul>
<xsl:apply-templates select="person"/>
</body>
</html>
</xsl:template>
<!-- Table of Contents Mode Templates -->
<xsl:template match="person" mode="toc">
<xsl:apply-templates select="name" mode="toc"/>
</xsl:template>
<xsl:template match="name" mode="toc">
<li><xsl:value-of select="last_name"/>,
<xsl:value-of select="first_name"/></li>
</xsl:template>
<!-- Normal Mode Templates -->
<xsl:template match="person">
<p><xsl:apply-templates/></p>
</xsl:template>
</xsl:stylesheet>
BTE550 Internet and Intranet Applications

438

73

Result
Alan
Turing

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

computer scientist
mathematician
cryptographer
</p>
<p>

XSLT
LT 7

XSLT
LT 7

Result
<html>
<head>
<META http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>Famous Scientists</title>
</head>
<body>
<ul>
<li>Turing,
Alan</li>
<li>Feynman,
Richard</li>
</ul>
<p>

Richard
M
Feynman

Richard
M
Feynman
physicist

physicist
Playing the bongoes
</p>
</body>
</html>
439

BTE550 Internet and Intranet Applications

Alan
Turing
computer scientistmathematiciancryptographer

xsl:for-each

So far we either process all the elements relative to a node


or one element
We need a way to filter out elements as well
This is done with an XPath control structure

iterating through a node set


<xsl:for-each></xsl:for-each>
XSLT
LT 7

XSLT
LT 7

Filtering

BTE550 Internet and Intranet Applications

440

BTE550 Internet and Intranet Applications

443

Example of xsl:for-each

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="person">
<xsl:apply-templates
<xsl:apply
templates select
select="*[not
[not
not(self::hobby)]"/>
not(self::hobby)]
/>
</xsl:template>
</xsl:stylesheet>
The self keyword is needed to inform the XSLT processor
that the node following is a child of the current one

441

XSLT
LT 7

XSLT
LT 7

Filtering Example

BTE550 Internet and Intranet Applications

442

BTE550 Internet and Intranet Applications

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="people">
<xsl:for
for--each select
select="person">
person >
<xsl:value-of select="name"/>
<xsl:value-of select="@born"/>
</xsl:for
for--each>
each
</xsl:template>
</xsl:stylesheet>
BTE550 Internet and Intranet Applications

444

74

Result

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

Alan
Turing
1912
Richard
M
Feynman
1918

XSLT
LT 7

XSLT
LT 7

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

445

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

xsl:if

XSLT
LT 7

XSLT
LT 7

446

449

xsl:choose, xsl:when, xsl:otherwise

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="people">
<xsl:for-each select="person">
<xsl:value-of select="name"/>

XSLT
LT 7

XSLT
LT 7

We can also select content using:


<xsl:choose>
<xsl:when test=criteria>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
The test attribute works in the same fashion as xsl:if

BTE550 Internet and Intranet Applications

Example of xsl:if

<xsl:if test="@born='1912'">
Died in <xsl:value-of select="@died"/>

</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
BTE550 Internet and Intranet Applications

448

xsl:choose, xsl:when, xsl:otherwise

We can test content for certain values with XSL:


<xsl:if test=criteria></xsl:if>
The test attribute is required and will either be true or false

BTE550 Internet and Intranet Applications

Died in
1954
Richard
M
Feynman

447

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="people">
<xsl:for-each select="person">
<xsl:value-of select="name"/>
<xsl:choose>
@
<xsl:when test="@born='1912'">
Died in <xsl:value-of select="@died"/>
</xsl:when>
<xsl:otherwise>
Did not die in 1912
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
BTE550 Internet and Intranet Applications

450

75

Result

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

Alan
Turing
Died in 1954

XSLT
LT 7

XSLT
LT 7

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

Richard
M
Feynman

Richard
M
Feynman
Did not die in 1912

physicist
Playing the bongoes
451

BTE550 Internet and Intranet Applications

XSLT
LT 7

XSLT
LT 7

Example of xsl:sort (Descending)

XSLT provides a nice way to sort documents by element


contents
The construct to use is:
<xsl:sort select=selection></xsl:sort>
g can only
y be done in the following
g constructs:
Sorting
<xsl:apply-templates/>
<xsl:for-each />

452

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="people">
<xsl:apply-templates>
<xsl:apply
templates>
<xsl:sort select="name order="descending />
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>

Result
<?xml version="1.0" encoding="UTF-8"?>
Richard
M
Feynman

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="people">
<xsl:apply-templates>
<xsl:apply
templates>
<xsl:sort select="name"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>

XSLT
LT 7

XSLT
LT 7

455

BTE550 Internet and Intranet Applications

Example of xsl:sort (Ascending)

BTE550 Internet and Intranet Applications

454

BTE550 Internet and Intranet Applications

xsl:sort

BTE550 Internet and Intranet Applications

computer scientist
mathematician
cryptographer

physicist
Playing the bongoes
Alan
Turing
computer scientist
mathematician
cryptographer

453

BTE550 Internet and Intranet Applications

456

76

xsl:copy: Example # 2

Used for creating an XML Document


The copying is done using this construct:
<xsl:copy></xsl:copy>
We will also specify to the processor that our output
should be XML instead of HTML
<xml:output method=xml/>

XSLT
LT 7

XSLT
LT 7

xsl:copy

457

BTE550 Internet and Intranet Applications

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match=*">
< l
<xsl:copy>
>
<xsl:apply-templates>
<xsl:sort select="name"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Result

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match=people">
< l
<xsl:copy>
>
<xsl:apply-templates>
<xsl:sort select="name"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

XSLT
LT 7

XSLT
LT 7

Example of xsl:copy

458

BTE550 Internet and Intranet Applications

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


<people><person>
<name>
<first_name>Richard</first_name>
<middle_initial>M</middle_initial>
<last_name>Feynman</last_name>
</name>
<profession>physicist</profession>
y
y g the bongoes</hobby>
g
y
<hobby>Playing
</person><person>
<name>
<first_name>Alan</first_name>
<last_name>Turing</last_name>
</name>
<profession>computer scientist</profession>
<profession>mathematician</profession>
<profession>cryptographer</profession>
</person>
</people>
BTE550 Internet and Intranet Applications

Result

461

Programming API

XSLT
LT 7

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


Richard
M
Feynman

XSLT
LT 7

460

BTE550 Internet and Intranet Applications

physicist
Playing the bongoes
Alan
Turing

Input (Source tree)


File, Character stream, Byte stream
DOM
SAX input stream
Output (Result tree)
File, Character stream, Byte stream
DOM
SAX events

computer scientist
mathematician
cryptographer
BTE550 Internet and Intranet Applications

459

BTE550 Internet and Intranet Applications

462

77

XSLT
LT 7

Programming API using Xalan


//Have XSLTProcessorFactory obtain a interface to a
// new XSLTProcessor object.
XSLTProcessor processor =
XSLTProcessorFactory.getProcessor();
// Have the XSLTProcessor processor object transform
// "foo.xml" to System.out, using the XSLT instructions
// found in "foo.xsl".
processor.process(new XSLTInputSource("foo.xml"),
new XSLTInputSource("foo.xsl"),
new XSLTResultTarget(System.out));
BTE550 Internet and Intranet Applications

463

CSS

Copyleft 2005, Binnur Kurt

Content

BTE550 Internet and Intranet Applications

TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer =
tf.newTransformer(new StreamSource(foo.xsl);
transformer.transform(
new StreamSource(foo.xml),
new StreamSource(bar.xml));
St
S
(b
l))

CSS
S

XSLT
LT 7

Programming API using JAXP 1.1

464

BTE550 Internet and Intranet Applications

467

Programming is useful when you do more than


transformation
Examples
Interpreting certain elements as database queries
Inserting
g the query
q y results into output
p document
asking users questions in the middle of transformation

465

What is CSS?

CSS
S

XSLT
LT 7

XSLT vs. Programming

BTE550 Internet and Intranet Applications

What are CSS?


History of CSS
Why CSS?
Types of Style Sheets
Style Sheets Syntax
Cascading Model
Box Formatting Model
CSS Syntax
Inheritance
Classes, Pseudo Classes & IDs
Groups of elements (DIV & SPAN)
Benefits & Disadvantages

CSS stands for Cascading Style Sheets


Styles define how to display HTML elements
Styles are normally stored in Style Sheets
Styles were added to HTML 4.0 to solve a problem
External Style Sheets can save you a lot of work
External Style Sheets are stored in CSS files
Multiple style definitions will cascade into one

BTE550 Internet and Intranet Applications

468

78

Style Sheets Can Save a Lot of Work

BTE550 Internet and Intranet Applications

As a Web developer you can define a style for each


HTML element and apply it to as many Web pages as you
want.

CSS
S

CSS
S

Styles Solve a Common Problem


HTML tags were originally designed to define the content
of a document.
They were supposed to say "This is a header", "This is a
paragraph", "This is a table", by using tags like <h1>,
<p>, <table>, and so on.
The layout of the document was supposed to be taken care
of by the browser, without using any formatting tags.
As new HTML tags and attributes (like the <font> tag and
the color attribute) are added to the original HTML
specification, it became more difficult to create Web sites
where the content of HTML documents was clearly
separated from the document's presentation layout.
469

BTE550 Internet and Intranet Applications

Styles Solve a Common Problem

8
CSS
S

8
CSS
S

470

Styles are normally saved in external .css files.


External style
y sheets enable you
y to change
g the appearance
pp
and layout of all the pages in your Web, just by editing one
single CSS document!
CSS is a breakthrough in Web design because it allows
developers to control the style and layout of multiple Web
pages all at once.
471

CSS
S

473

Cascading Order

Styles sheets define HOW HTML elements are to be


displayed, just like the font tag and the color attribute in
HTML 3.2.

CSS
S

Style sheets allow style information to be specified in


many ways.
Styles can be specified inside a single HTML element,
inside the <head> element of an HTML page, or in an
external CSS file.
Even multiple external style sheets can be referenced
inside a single HTML document.

BTE550 Internet and Intranet Applications

Style Sheets Can Save a Lot of Work

BTE550 Internet and Intranet Applications

472

Multiple Styles Will Cascade Into One

To solve this problem, the World Wide Web Consortium


(W3C) - the non profit, standard setting consortium,
responsible for standardizing HTML - created STYLES in
addition to HTML 4.0.

BTE550 Internet and Intranet Applications

To make a global change, simply change the style, and all


p
automatically.
y
elements in the Web are updated

What style will be used when there is more than one style
specified for an HTML element?
All the styles will "cascade" into a new "virtual" style sheet
by the following rules, where number four has the highest
priority:
1. Browser default
2. External style sheet
3. Internal style sheet (inside the <head> tag)
4. Inline style (inside an HTML element)
Inline style (inside an HTML element) has the highest
priority, which means that it will override a style declared
inside the <head> tag, in an external style sheet, or in a
browser.
BTE550 Internet and Intranet Applications

474

79

A Brief History of HTML and CSS

BTE550 Internet and Intranet Applications

1. Build from the ground up to replace traditional Web


design methods
2. Faster download times
3. Shorter development time
yp g p y in a Web page
p g
4. Greater control over the typography
5. It's easy to write
6. Improvements in accessibility
7. Print designs as well as Web page designs
8. Better control over the placement of elements in Web
page

CSS
S

CSS
S

Top 10 Reasons for Using CSS

475

Separating Style from Structure


Mixing display instructions and structural information:
Adds to complexity of code
Inefficient mechanism for handling display
p g Web sites
characteristics of multi-page
Limits cross-platform compatibility of content
limits diversity of web devices

478

BTE550 Internet and Intranet Applications

Top 10 Reasons for Using CSS

What are they?

9. The design of Web pages is separated from the content


10. Better search engine rankings
8

CSS
S

CSS
S

CONTENT

STYLE

Web page
BTE550 Internet and Intranet Applications

476

BTE550 Internet and Intranet Applications

Status of CSS

What are they?

CONTENT
STYLE

CSS, Level 2 (1998)


Supports media-specific style sheets (visual browsers,
aural devices, printers, Braille devices)
http://www.w3.org/TR/REC-CSS2

CSS
S

CSS, Level 1 (1996)


Concerned with applying simple styles to HTML
elements
http://www.w3.org/TR/REC-CSS1
CSS
S

479

Web page

CSS, Level 3 (draft 2001)


Focused on modularization of the CSS specification
http://www.w3.org/TR/css3-roadmap/
BTE550 Internet and Intranet Applications

477

BTE550 Internet and Intranet Applications

480

80

What are they?

Types of CSS

8
CSS
S

CSS
S

CSS

External
Embedded
Imported
Inline

481

BTE550 Internet and Intranet Applications

484

BTE550 Internet and Intranet Applications

What are they?

External

8
CSS

Text

Physical layout

CSS
Physical
Layout

CSS
S

CSS
S

Connection made via the LINK tag


Use the optional TYPE attribute to specify a media type
type/css

Text

CSS

CSS

Headings

Headings

Body

Body

482

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

Devices

Internal/Embedded
CONTENT

8
CSS
CSS
CSS

Style characteristics are embedded in the HEAD section


of the webpage
Perhaps best used when a single page requires a unique
style sheet

CSS
S

CSS
S

BROWSER

CONTENT

485

MOBILE
PDA

PRINT
BTE550 Internet and Intranet Applications

483

BTE550 Internet and Intranet Applications

486

81

Inserting a CSS

Internal/embedded sheet

CSS
S

Allows for using style sheets from other sources


Must be included at the beginning of the style sheet using
the @import statement
Other CSS rules can be included

CSS
S

Imported

487

BTE550 Internet and Intranet Applications

<head>
<style type=text/css>
hr { color: navy}
body {margin-left: 20px}
</style>
</head>

BTE550 Internet and Intranet Applications

Inserting a CSS

CSS
S

Least flexible
Requires each element to be tagged if you want them to
appear differently
Looses the advantage of using CSS

CSS
S

Inline

BTE550 Internet and Intranet Applications

488

Internal/embedded sheet for older browsers


<head>
<style type=text/css>
<!-- hr { color: navy}
body {margin-left:
{margin left: 20px} -->
>
</style>
</head>

BTE550 Internet and Intranet Applications

Inserting a CSS
Inline

<head>
<link rel=stylesheet type=text/css
y y
/>
href=mystyle.css
</head>

CSS
S

8
CSS
S

491

Inserting a CSS

External sheet

BTE550 Internet and Intranet Applications

490

489

<p style=color: yellow; font-family: verdana>


This is a paragraph
</p>

BTE550 Internet and Intranet Applications

492

82

Sheet weight or Precedence

CSS
S

You can use multiple sheets to define the style of your


document
Internal styles will override external styles, if they are
duplicated

CSS
S

Using multiple sheets

BTE550 Internet and Intranet Applications

493

8
CSS
S

8
CSS
S

494

Least weight

496

Cascading
Determining rule weight by specificity
Rules with more specific selectors take precedence
over rules with less specific selectors
Determiningg rule weight
g byy order
Based on order of rule within style sheet
Those listed later take precedence over those
listed earlier in the style sheet

BTE550 Internet and Intranet Applications

Using multiple sheets

497

Understanding the Cascade

CSS
S

h3 {color: red; text-align: right; font-size: 8pt} (external CSS)


h3 {text-align: center; font-size: 20pt} (internal CSS)
will yield
h3 {color: red; text-align: center; font-size: 20pt }

CSS
S

Greatest weight

Browsers
style sheet

Understanding the Cascade

h3 {color: red; text-align: right; font-size: 8pt} (external CSS)


h3 {text-align: center; font-size: 20pt} (internal CSS)
will yield

BTE550 Internet and Intranet Applications

Users
style
sheet

BTE550 Internet and Intranet Applications

Using multiple sheets

BTE550 Internet and Intranet Applications

Authors
style
sheet

495

Inheritance
Based on hierarchical structure of documents
CSS rules inherit from parent elements to child
elements:
thus <LI> elements will inherit style
y rules from
<UL> elements unless a style rule is specifically
set for the <LI> element

BTE550 Internet and Intranet Applications

498

83

Basic CSS Syntax


selector {property: value}

property: the attribute of the selector that you wish to change


property

BTE550 Internet and Intranet Applications

CSS
S

CSS
S

Basic CSS Syntax

499

BTE550 Internet and Intranet Applications

selector {property: value}


value: the particular markup value for that attribute
value

declaration

selector {property: value}

BTE550 Internet and Intranet Applications

500

body {color : black}


black

BTE550 Internet and Intranet Applications

Basic CSS Syntax

503

Basic CSS Syntax


If the value has multiple words, put the value in quotes

selector: the basic HTML element tag you wish to define


selector

p {font-family: sans serif }


8

selector {property: value}

body

BTE550 Internet and Intranet Applications

CSS
S

CSS
S

502

Basic CSS Syntax

CSS
S

CSS
S

Basic CSS Syntax


Three parts:
selector
property
value

body {color
color

501

BTE550 Internet and Intranet Applications

504

84

Basic CSS Syntax

Basic CSS Syntax

You can specify multiple properties to a single selector.


Properties must be separated by a semicolon.

Selectors can be descendants

8
CSS
S

CSS
S

P B { color: yellow }

P { text-align: left;; color: red }

In this example, only those <B> elements within a <P> element would
b yellow
be
ll
<p><b> This would be yellow </b></p>
<p> This would not be yellow </p>

BTE550 Internet and Intranet Applications

505

CSS Syntax - class


The class selector allows you to create different styles for
the same HTML element.

p { text-align: center;
y;
color: navy;
font-family: arial
}

p.right { text-align: right }


pp.center { text-align:
g center }

BTE550 Internet and Intranet Applications

To make properties more readable, put each on a separate


line.

CSS
S

CSS
S

Basic CSS Syntax

506

CSS Syntax - class


p.right { text-align: right }
<p class=right>
This paragraph will be right aligned.

h1,h2,h3,h4,h5,h6
{ color: yellow
y
}

CSS
S

Selectors can be grouped so that a common property can be


specified

<h1> This is a level 1 heading </h1>


<h2> This is a level 2 heading </h2>

BTE550 Internet and Intranet Applications

509

BTE550 Internet and Intranet Applications

Basic CSS Syntax

CSS
S

508

BTE550 Internet and Intranet Applications

</p>
Note:
the class name must be in quotes inside the opening tag

507

BTE550 Internet and Intranet Applications

510

85

CSS Syntax - class

CSS Syntax - id

While the class selector can apply to several different


elements, the id selector can only apply to one, unique
element.

CSS
S

<p class=right class=center>


This paragraph will be right aligned.
</p>

CSS
S

This is improper use of the class selector


selector:

p#para1 { text-align: center;


color: green }

Only one class selector can be included inside the tag

BTE550 Internet and Intranet Applications

511

BTE550 Internet and Intranet Applications

CSS Syntax - class

CSS Syntax - id

You can also create a class selector free of a tag name if you
want all tags that have that class to be formatted the same.

p#para1 { text-align: center;


color: green }
8

A tag with
Any
i h a center class
l will
ill be
b aligned
li d center

CSS
S

CSS
S

.center { text-align: center }

BTE550 Internet and Intranet Applications

512

515

CSS Syntax - comment


You can insert comments to help you describe the particular style

<h1 class=center>
This heading will be centered
</h1>

Comments open with /* and are closed with */

.center { text-align: center }

CSS
S

<p id=para1>
This text would be centered and green
</p>

BTE550 Internet and Intranet Applications

CSS Syntax - class

CSS
S

514

<p class=center>
So will this text

/* This is a comment */
P { color:
co o : red;
ed;
/* This is another comment */
Font-family: verdana }

</p>

BTE550 Internet and Intranet Applications

513

BTE550 Internet and Intranet Applications

516

86

Background properties

BTE550 Internet and Intranet Applications

Define the background effects of an element


Effects include color, using an image for a background,
repeating an image and positioning an image
8

<DIV> can be used with the CLASS attribute to create


customized block-level elements
Declare it in the style rule:
DIV.introduction {font-size: 14pt; margin: 24 pt;}
Apply
pp y the style
y rule in the document:
<DIV CLASS=introduction>This is the
introduction to the document</DIV>

CSS
S

CSS
S

CSS syntax - <div>

517

BTE550 Internet and Intranet Applications

<SPAN> can be used with the CLASS attribute to create


customized inline elements
Declare it in the style rule:
SPAN.logo {color: white; background-color: black;}
Apply
pp y the style
y rule in the document:
<P>Welcome to the <SPAN CLASS=logo>
Wonder Software</SPAN>Web site</P>

BTE550 Internet and Intranet Applications

Background properties

CSS
S

CSS
S

CSS syntax - <span>

520

518

Basic syntax
background
background-color
background-image
background
background-repeat
repeat
background-attachment
background-position

BTE550 Internet and Intranet Applications

521

Background properties

Background Properties

BTE550 Internet and Intranet Applications

background: #000000 url(psumark.gif) no-repeat fixed


center

CSS
S

CSS
S

All attributes can be set in a single declaration:

519

BTE550 Internet and Intranet Applications

522

87

Background properties

CSS
S

body { background: #000000 url(psumark.gif) no-repeat


fixed center }

CSS
S

Setting the body background (internal CSS)

BTE550 Internet and Intranet Applications

523

BTE550 Internet and Intranet Applications

Background properties

8
CSS
S

CSS
S

Controls the appearance of text in the web page

body: { background: #000000 url(psumark.gif) no-repeat


fixed center }

BTE550 Internet and Intranet Applications

524

BTE550 Internet and Intranet Applications

Background properties

CSS
S

body
{ background-image: url(psumark.gif);
background-color:
background
color: navy }

BTE550 Internet and Intranet Applications

527

Text properties

Elements can also be set separately

526

Text properties

Setting the body background (external CSS)

CSS
S

Text Properties

525

Commonly used attributes


color
direction
text-align
text
text-decoration
decoration
text-indent

BTE550 Internet and Intranet Applications

528

88

color
sets the color of the text
color can be represented by the color name (red), an
rgb value (rgb(255,0,0)), or by a hexadecimal number
(#ff0000)
Syntax
body {color: #ff0000}

BTE550 Internet and Intranet Applications

Text properties

CSS
S

CSS
S

Text properties

529

BTE550 Internet and Intranet Applications

532

Text properties

direction
sets the direction of the text
can be set as left to right (ltr) or right to left (rtl)
Syntax
body {direction: rtl}

CSS
S

CSS
S

Text properties

BTE550 Internet and Intranet Applications

text
text--decoration
adds certain decoration elements to the text
possible values are none, underline, overline, linethrough and blink
y
Syntax
p {text-decoration: underline}

530

text
text--indent
indents the first line of text inside an element
possible values are length (defines a fixed value) and
% (defines a % of the parent element)
y
Syntax
p {text-indent: 20px}

BTE550 Internet and Intranet Applications

533

BTE550 Internet and Intranet Applications

text
text--align
aligns the text in an element
possible values are left, right, center and justify
Syntax
p {text-align:
{text align: center}

CSS
S

CSS
S

Text properties

531

Font Properties

BTE550 Internet and Intranet Applications

534

89

Font properties

Font properties
normal
font displays as is
small-caps
font displays in all capitals,
with lower case letters in
smaller size

font
font--variant

CSS
S

CSS
S

Define the look of the font in text areas


One of the broader sets of properties in CSS

Syntax: body {font-variant: small-caps}

535

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

Font properties

Font properties
font
font--weight

CSS
S

CSS
S

font-style
font-variant
font-weight
font-size/line-height
font-family
font family

font

538

normal
bold
bolder
lighter
weighted values

Syntax: body {font-weight: bold}

536

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

Font properties

Weighted values
range from 100 900
400 is the same as normal weight
700 is the same as bold weight
CSS
S

CSS
S

normal
italic
oblique

font
font--style

539

Syntax: body {font-style: italic}

BTE550 Internet and Intranet Applications

537

BTE550 Internet and Intranet Applications

540

90

Font properties
xx-small to xx-large
smaller
smaller than parent
larger
larger
g than parent
p
%
% of the parent

Allows you to specify the style, color and width of an


elements border
Many different properties can be applied

CSS
S

CSS
S

font
font--size

Border properties

Syntax: body {font-size: 20px}


{font-size: x-large}
{font-size: 125%}
541

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

Font properties

Border properties

family-name
times, arial, courier,
verdana
generic-family
serif,, sans-serif,,
monospace

You can specify the width, style, color, thickness and on


which sides the border appears

CSS
S

CSS
S

font
font--family

544

Syntax: body {font-family: verdana, sans-serif}

542

545

BTE550 Internet and Intranet Applications

Border Properties

BTE550 Internet and Intranet Applications

CSS
S

CSS
S

BTE550 Internet and Intranet Applications

543

Margin Properties

BTE550 Internet and Intranet Applications

546

91

Margin properties

Can be set in one declaration


Think clock face
top, right, bottom, left
CSS
S

Define the space around elements


You can use negative values to overlap content
Margins can be set independently or collectively
Can be set to auto, a fixed length or a % of the total height
of the document

CSS
S

Margin properties

547

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

Properties
margin
margin-top
margin-right
margin-bottom
g
margin-left

All margins can be set the same

548

BTE550 Internet and Intranet Applications

551

Margin properties

auto
set by the browser
length
fixed
%

Margin settings can be paired (left and right, top and


bottom)

CSS
S

8
CSS
S

h1 {margin: 40px}

BTE550 Internet and Intranet Applications

Margin properties

h1 {margin: 40px 5%}

In this example, the top and bottom margins would be 40 pixels,


While the left and right margins would be 5% of the total height of
the document.

Syntax: h1 {margin-bottom: 20px}

BTE550 Internet and Intranet Applications

550

Margin properties

CSS
S

CSS
S

Margin properties

margin-bottom

h1 {margin: 10px 20px 30px 40px}

549

BTE550 Internet and Intranet Applications

552

92

Margin properties

CSS
S

0 size margins do not need to be specified. 0px, 0pt and 0


are all equivalent.

h1 {margin: 40px 0 5% 0}
In this example, the top margin would be 40 pixels, the left and
right margins would be 0, and the bottom margin would
be 5% of the total height of the document.

Multimedia Standards

Copyleft 2005, Binnur Kurt

556

BTE550 Internet and Intranet Applications

557

Image Compression Standards

Image Compression Standards: JPEG, JPEG2000


Video Compression Standards: MPEG-1,2,4
Image and Video Indexing Standards: MPEG-7

Multimedia S
Standards 9

Multimedia S
Standards 9

Content

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

Multimedia S
Standards 9

553

BTE550 Internet and Intranet Applications

Multimedia S
Standards 9

Benefits of Compressing Data

555

Facilitates the exchange of compressed data between


various devices and applications.
Economy of scale: permits common hardware/software to
be used for a wide range of products, thus lowering the cost
and shortening the development time.
Provides reference points for the expected quality of
compressed images.

BTE550 Internet and Intranet Applications

558

93

JPEG Summary

ISO/IEC
International Organization for Standardization
Deals with information processing, e.g., image storage
and retrieval
ITU
ITU-T
T
International Telecommunication Union
Formerly known as CCITT
Deals with information transmission

The JPEG committee has published the following


standards:
ISO/IEC 10918-1 | ITU-T Rec. T.81: Digital
compression and Coding of Continuous-Tone Still
Images: Requirements and guidelines
ISO/IEC 10918-2
10918 2 | ITU
ITU-T
T Rec.
Rec T
T.83:
83: Compliance testing
ISO/IEC 10918-3 | ITU-T Rec. T.84: Extensions
ISO/IEC 14495-1 | ITU-T Rec. T.87: Lossless and NearLossless Compression of Continuous-Tone Still Images
Baseline
ISO/IEC 15444-1 | ITU-T Rec. T.800: JPEG2000 Image
Coding System.

BTE550 Internet and Intranet Applications

Multimedia S
Standards 9

Multimedia S
Standards 9

International Standard Organizations

559

BTE550 Internet and Intranet Applications

Examples of JPEG Applications

Binary (bi-level) images:


Group 3 & 4 (1980); JBIG (1994); JBIG2
Continuous-tone still images:
JPEG (1992); JPEG-2000 (2000)
Image sequences (moving pictures):
H.261 (1990); H.263 (1995);
H.263+ (1997), H.263L
MPEG1 (1994); MPEG2 (1995)
MPEG4 (1997)
MPEG7 (2001)
MPEG21
BTE550 Internet and Intranet Applications

Multimedia S
Standards 9

Multimedia S
Standards 9

Image Compression Standards

560

563

Baseline JPEG Encoder Block Diagram

The JPEG (Joint Photographic Experts Group) committee,


formed in 1986, has been chartered with the
Digital compression and coding of continuous-tone still
images
Joint between ISO and ITU-T
Has developed standards for the compression of lossy,
lossless, and nearly lossless of still images in the past
decade
Web site: www.jpeg.org

561

Multimedia S
Standards 9

Multimedia S
Standards 9

Consumer imaging (digital cameras, picture disk, etc.)


Professional imaging (desktop publishing, graphic arts,
digital cameras, etc.)
Medical imaging
g g
Internet imaging
Scanning and printing
Image databases
Mobile

BTE550 Internet and Intranet Applications

What Is JPEG?

BTE550 Internet and Intranet Applications

562

BTE550 Internet and Intranet Applications

564

94

Multimedia S
Standards 9

Multimedia S
Standards 9

Baseline JPEG Decoder Block Diagram

565

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

Disadvantages
Single resolution
Single quality
No target bit rate
No lossless capability
No tiling
No ROI
Blocking artifacts
Poor error resilience

Multimedia S
Standards 9

Advantages
Memory Efficient
Low complexity
Compression efficiency
Visual model utilization
Robustness
R b t

JPEG2000 Compression Paradigm

566

BTE550 Internet and Intranet Applications

567

BTE550 Internet and Intranet Applications

569

BTE550 Internet and Intranet Applications

570

Multimedia S
Standards 9

BTE550 Internet and Intranet Applications

Multimedia S
Standards 9

Multimedia S
Standards 9

Baseline JPEG Pros and Cons

568

95

Multimedia S
Standards 9

Multimedia S
Standards 9

0.125 bpp, CR=64:1


571

BTE550 Internet and Intranet Applications

574

Multimedia S
Standards 9

Multimedia S
Standards 9

BTE550 Internet and Intranet Applications

0.25 bpp, CR=32:1


572

BTE550 Internet and Intranet Applications

575

Multimedia S
Standards 9

Multimedia S
Standards 9

BTE550 Internet and Intranet Applications

0.5 bpp, CR=16:1


BTE550 Internet and Intranet Applications

573

BTE550 Internet and Intranet Applications

576

96

Multimedia S
Standards 9

Multimedia S
Standards 9

The JPEG 2000 Standard


Part 1: Core Image Coding System (royalty and fee free)
Part 2: Extensions
Part 3: Motion JPEG 2000
Part 4: Conformance Testing
Part 5: Reference Software
Part 6: Compound Image File Format

1 bpp, CR=8:1
BTE550 Internet and Intranet Applications

577

BTE550 Internet and Intranet Applications

580

Multimedia S
Standards 9

Multimedia S
Standards 9

JPEG 2000 Fundamental Building Blocks

overall 0.25 bpp, ROI 0.75 bpp, BG 0.1 bpp

578

BTE550 Internet and Intranet Applications

581

JPEG 2000 Objectives

Preprocessing

Advanced standardization image coding system to serve


applications into the next millennium
Address areas where current standards fail to produce the
best quality or performance
Provide capabilities to markets that currently do not use
compression
Provide an open system approach to imaging applications

The input image is partitioned into rectangular and nonoverlapping tiles of equal size (except possibly for those
tiles at the image borders) that are compressed
independently using their own set of specified compression
parameters.
The unsigned sample values in each component are level
shifted (DC offset) by subtracting a fixed value from each
sample to make its value symmetric around zero.
The level-shifted values can be subjected to a forward
point-wise inter-component transformation to decorrelate
the color data. A restriction is that components must have
identical bit-depths and dimension

BTE550 Internet and Intranet Applications

579

Multimedia S
Standards 9

Multimedia S
Standards 9

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

582

97

Multimedia S
Standards 9

Multimedia S
Standards 9

Reversible Color Transform

BTE550 Internet and Intranet Applications

583

BTE550 Internet and Intranet Applications

586

BTE550 Internet and Intranet Applications

587

BTE550 Internet and Intranet Applications

588

Multimedia S
Standards 9

Multimedia S
Standards 9

Irreversible Color Transform (ICT)

BTE550 Internet and Intranet Applications

585

Multimedia S
Standards 9

584

Multimedia S
Standards 9

BTE550 Internet and Intranet Applications

98

Multimedia S
Standards 9

Multimedia S
Standards 9
BTE550 Internet and Intranet Applications

590

BTE550 Internet and Intranet Applications

591

BTE550 Internet and Intranet Applications

592

BTE550 Internet and Intranet Applications

593

BTE550 Internet and Intranet Applications

594

Multimedia S
Standards 9

Multimedia S
Standards 9

Multimedia S
Standards 9

589

Multimedia S
Standards 9

BTE550 Internet and Intranet Applications

99

Multimedia S
Standards 9

Multimedia S
Standards 9

Example of Analysis Filter Bank

Discrete Wavelet Transform


(DWT)

BTE550 Internet and Intranet Applications

595

BTE550 Internet and Intranet Applications

598

BTE550 Internet and Intranet Applications

599

BTE550 Internet and Intranet Applications

600

Multimedia S
Standards 9

Multimedia S
Standards 9

Transformations in JPEG 2000

BTE550 Internet and Intranet Applications

596

Multimedia S
Standards 9

Multimedia S
Standards 9

1D Two-Band DWT

BTE550 Internet and Intranet Applications

597

100

Multimedia S
Standards 9

Multimedia S
Standards 9

The 1D Two Band DWT

BTE550 Internet and Intranet Applications

601

BTE550 Internet and Intranet Applications

604

Multimedia S
Standards 9

Multimedia S
Standards 9

1 Level, 2D Wavelet Decomposition (DC=1,AC=4)

BTE550 Internet and Intranet Applications

602

BTE550 Internet and Intranet Applications

Inverse DWT

605

Multimedia S
Standards 9

Multimedia S
Standards 9

2 Level, 2D Wavelet Decomposition (DC=1,AC=4)

BTE550 Internet and Intranet Applications

603

BTE550 Internet and Intranet Applications

606

101

Bi-Orthogonal DWT
Multimedia S
Standards 9

Multimedia S
Standards 9

3 Level, 2D Wavelet Decomposition (DC=1,AC=4)

BTE550 Internet and Intranet Applications

607

BTE550 Internet and Intranet Applications

610

Multimedia S
Standards 9

Multimedia S
Standards 9

2D Wavelet Decomposition

BTE550 Internet and Intranet Applications

608

BTE550 Internet and Intranet Applications

611

ROI Example

Most wavelet based image compression systems use a class of


analysis/synthesis filters knowns as bi-orthogonal filters:
The basis functions corresponding to h0(n) and g1(n) are
orthogonal; and the basis functions for h1(n) and g0(n) are
orthogonal.
Linear
Linear-phase
phase (symmetrical) and perfect reconstruction
Unequal length; odd-length filters differ by an odd multiple
of two (e.g., 7/9), while even-length filters differ by an even
multiple of two (e.g., 6/10)
Symmetric boundary extension.

609

Multimedia S
Standards 9

Multimedia S
Standards 9

Bi-Orthogonal Filter Banks

BTE550 Internet and Intranet Applications

Region Of Interest Coding

BTE550 Internet and Intranet Applications

612

102

Multimedia S
Standards 9

Error Effects

Multimedia S
Standards 9

ROI Example (contd)

BTE550 Internet and Intranet Applications

613

616

BTE550 Internet and Intranet Applications

Multimedia S
Standards 9

Multimedia S
Standards 9

Protecting Code-block Data

Error Resilience

BTE550 Internet and Intranet Applications

614

617

BTE550 Internet and Intranet Applications

Error-prone Channels

Error-Resilience Visual Results


Multimedia S
Standards 9

Multimedia S
Standards 9

BER = 10-5

JPEG CR=16:1

BTE550 Internet and Intranet Applications

615

BTE550 Internet and Intranet Applications

JPEG 2000 CR=16:1

618

103

Error-Resilience Visual Results

Test Images
Multimedia S
Standards 9

Multimedia S
Standards 9

BER = 10-4

JPEG CR=16:1

JPEG 2000 CR=16:1


619

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

Multimedia S
Standards 9

Test Images (Contd)

Multimedia S
Standards 9

JPEG-LS

622

620

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

623

BTE550 Internet and Intranet Applications

624

Multimedia S
Standards 9

Multimedia S
Standards 9

PNG

BTE550 Internet and Intranet Applications

621

104

Multimedia S
Standards 9

Multimedia S
Standards 9
BTE550 Internet and Intranet Applications

625

BTE550 Internet and Intranet Applications

628

Multimedia S
Standards 9

Multimedia S
Standards 9

Non-progressive Results

BTE550 Internet and Intranet Applications

626

BTE550 Internet and Intranet Applications

629

BTE550 Internet and Intranet Applications

630

Multimedia S
Standards 9

Multimedia S
Standards 9

Lossless Results

BTE550 Internet and Intranet Applications

627

105

Multimedia S
Standards 9

Multimedia S
Standards 9
BTE550 Internet and Intranet Applications

632

BTE550 Internet and Intranet Applications

634

BTE550 Internet and Intranet Applications

635

Multimedia S
Standards 9

631

Multimedia S
Standards 9

BTE550 Internet and Intranet Applications

Multimedia S
Standards 9

Multimedia S
Standards 9

Video Compression Standards

BTE550 Internet and Intranet Applications

633

MPEG-1 and MPEG-2 provide interoperable ways of


representing audiovisual content, commonly used on digital
media and on the air
MPEG-4 defines how to represent content
MPEG-77 specifies how to describe content
MPEG
MPEG-21 provides a truly interoperable multimedia
framework

BTE550 Internet and Intranet Applications

636

106

MPEG-7 ISO/IEC 15938

Coding of moving pictures and associated audio for digital


storage media
Video and audio at 1.5M bit/s for CD-ROM
Five parts
Part 1 ((systems):
y
) multiplexing
p
g & synchronization
y
Part 2 (video): ~VHS quality at 1.15M bit/s
Part 3 (audio): stereo at 384K, 256K, 192K bit/s
Part 4 (conformance testing): references for decoder
Part 5 (reference software): C implementation
Applications: Video CD, MP3
BTE550 Internet and Intranet Applications

Multimedia S
Standards 9

Multimedia S
Standards 9

MPEG-1 ISO/IEC 11172

637

BTE550 Internet and Intranet Applications

640

MPEG-21 ISO/IEC (18034) 21000

Generic coding of moving pictures and associated audio


Digital Storage Media Command and Control (DSM-CC)
for session set up and remote control of a server, used in
set top boxes for satellite and cable TV
Advanced Audio Coding (AAC) for multi-channel audio
4:2:2 profile for TV production studios
Provisions for Intellectual Property Management and
Protection (IPMP)
Applications: digital TV set top boxes, DVD
Transport Stream version
Patent issues

Multimedia S
Standards 9

Multimedia S
Standards 9

MPEG-2 ISO/IEC 13818

BTE550 Internet and Intranet Applications

Multimedia content description interface


MPEG-7 specifies how to describe content
describe content way beyond metadata
facilitate content management, in particular searching

638

Define the technology needed to support Users to


exchange, access, consume, trade and otherwise
manipulate Digital Items in an efficient, transparent and
interoperable way

BTE550 Internet and Intranet Applications

641

BTE550 Internet and Intranet Applications

642

Coding of audiovisual objects


MPEG-4 defines how to represent content
ancestry: VRML
interoperability of content structure
AFX Animation Framework eXtension
XMT textual XML format for SMIL,
SMIL Web3D,
Web3D
etc.
adapt transparently to device capabilities
FSG Fine Granularity Scalability
Extensions of AAC and IPMP, Studio Profile
MP4 and AVC file formats, multi-user environment
Patent issues
BTE550 Internet and Intranet Applications

Multimedia S
Standards 9

Multimedia S
Standards 9

MPEG-4 ISO/IEC 14496

639

107

Multimedia S
Standards 9

Multimedia S
Standards 9
BTE550 Internet and Intranet Applications

644

BTE550 Internet and Intranet Applications

645

BTE550 Internet and Intranet Applications

646

BTE550 Internet and Intranet Applications

647

BTE550 Internet and Intranet Applications

648

Multimedia S
Standards 9

Multimedia S
Standards 9

Multimedia S
Standards 9

643

Multimedia S
Standards 9

BTE550 Internet and Intranet Applications

108

Multimedia S
Standards 9

Multimedia S
Standards 9
BTE550 Internet and Intranet Applications

650

BTE550 Internet and Intranet Applications

651

BTE550 Internet and Intranet Applications

652

BTE550 Internet and Intranet Applications

653

BTE550 Internet and Intranet Applications

654

Multimedia S
Standards 9

Multimedia S
Standards 9

Multimedia S
Standards 9

649

Multimedia S
Standards 9

BTE550 Internet and Intranet Applications

109

Multimedia S
Standards 9

Multimedia S
Standards 9
BTE550 Internet and Intranet Applications

656

BTE550 Internet and Intranet Applications

657

BTE550 Internet and Intranet Applications

658

BTE550 Internet and Intranet Applications

659

BTE550 Internet and Intranet Applications

660

Multimedia S
Standards 9

Multimedia S
Standards 9

Multimedia S
Standards 9

655

Multimedia S
Standards 9

BTE550 Internet and Intranet Applications

110

Multimedia S
Standards 9

Multimedia S
Standards 9
BTE550 Internet and Intranet Applications

662

BTE550 Internet and Intranet Applications

663

BTE550 Internet and Intranet Applications

664

BTE550 Internet and Intranet Applications

665

BTE550 Internet and Intranet Applications

666

Multimedia S
Standards 9

Multimedia S
Standards 9

Multimedia S
Standards 9

661

Multimedia S
Standards 9

BTE550 Internet and Intranet Applications

111

Multimedia S
Standards 9

Multimedia S
Standards 9
BTE550 Internet and Intranet Applications

668

BTE550 Internet and Intranet Applications

669

BTE550 Internet and Intranet Applications

670

BTE550 Internet and Intranet Applications

671

BTE550 Internet and Intranet Applications

672

Multimedia S
Standards 9

Multimedia S
Standards 9

Multimedia S
Standards 9

667

Multimedia S
Standards 9

BTE550 Internet and Intranet Applications

112

Multimedia S
Standards 9

Multimedia S
Standards 9
BTE550 Internet and Intranet Applications

674

BTE550 Internet and Intranet Applications

675

BTE550 Internet and Intranet Applications

676

BTE550 Internet and Intranet Applications

677

BTE550 Internet and Intranet Applications

678

Multimedia S
Standards 9

Multimedia S
Standards 9

Multimedia S
Standards 9

673

Multimedia S
Standards 9

BTE550 Internet and Intranet Applications

113

Multimedia S
Standards 9

Multimedia S
Standards 9

679

BTE550 Internet and Intranet Applications

Multimedia S
Standards 9

MPEG-21

Multimedia S
Standards 9

MPEG-4

680

BTE550 Internet and Intranet Applications

683

MPEG-21 Scope

MPEG-7 is not about compression; it is about metadata


bits about the bits
Metadata is digital information that describes the
content of other digital data

681

Multimedia S
Standards 9

Multimedia S
Standards 9

An open framework for multimedia delivery and


consumption
Focal points:
Content creators
Content consumers

BTE550 Internet and Intranet Applications

MPEG-7

BTE550 Internet and Intranet Applications

682

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

684

114

Multimedia S
Standards 9

Structured digital objects, including a standard


representation and identification, and metadata
Fundamental unit of distribution and transaction within
the MPEG-21 framework
No further technical meaning

BTE550 Internet and Intranet Applications

Multimedia S
Standards 9

The parts of MPEG-21

685

1. Vision, technologies and strategies


2. Digital Item Declaration
3. Digital Item Identification
4. Intellectual Property Management and Protection
((IPMP))
5. Rights Expression Language
6. Rights Data Dictionary
7. Digital Item Adaptation
8. Reference Software
9. File Format
BTE550 Internet and Intranet Applications

688

MPEG-21 User

MPEG-21Part 1 Vision

A User is any entity that interacts in the MPEG-21


environment or makes use of a Digital Item
Users include individuals, consumers, communities,
organizations, corporations, consortia, governments and
other standards bodies and initiatives around the world.
Users are identified specifically by their relationship to
another User for a certain interaction
MPEG-21 makes no distinction between a content
provider and a consumer both are Users
A single entity may use content in many ways
however, a User may assume specific or even unique
rights and responsibilities according to their interaction
with other Users within MPEG-21

Define a multimedia framework to enable transparent


and augmented use of multimedia resources across a
wide range of networks and devices
1. Provide a vision
2. Facilitate integration and harmonization of
technologies
3. Provide a strategy for achieving a framework through
collaboration

BTE550 Internet and Intranet Applications

Multimedia S
Standards 9

Multimedia S
Standards 9

MPEG-21 Digital Item

686

BTE550 Internet and Intranet Applications

Multimedia S
Standards 9

Users and content

Multimedia S
Standards 9

MPEG-21 CE Testbed

689

BTE550 Internet and Intranet Applications

687

BTE550 Internet and Intranet Applications

690

115

Digital Item Declaration in detail

World Wide Webs phases


1990 (info.cern.ch)
scientific exchange
1995 (tidal wave)
free content
2000 (dot bomb)
ubiquitous fast network
Users are starting to recognize the value of their digital
asset resources
Markets must be efficient
BTE550 Internet and Intranet Applications

Multimedia S
Standards 9

Multimedia S
Standards 9

Digital assets

691

BTE550 Internet and Intranet Applications

Multimedia S
Standards 9

Digital Item Declaration example

Multimedia S
Standards 9

The need for harmonization

BTE550 Internet and Intranet Applications

692

BTE550 Internet and Intranet Applications

Index a Digital Item


Purpose: describe a set of abstract terms and concepts to
form a useful model for defining Digital Items
Three normative sections:
Model
set of abstract terms and concepts
Representation
normative description of syntax & semantics of DID
elements
Schema
normative XML schema comprising the entire
grammar of DID
BTE550 Internet and Intranet Applications

693

695

MPEG-21 Part 3 Digital Item Identification


Multimedia S
Standards 9

MPEG-21 Part 2 Digital Item Declaration


Multimedia S
Standards 9

694

The scope of the Digital Item Identification (DII)


specification includes:
How to uniquely identify Digital Items and parts thereof
(including resources)
How to uniquely identify IP related to the Digital Items
(and parts thereof), for example abstractions
How to uniquely identify Description Schemes
How to use identifiers to link Digital Items with related
information such as descriptive metadata
How to identify different types if Digital Items
BTE550 Internet and Intranet Applications

696

116

Multimedia S
Standards 9

MPEG-21 Part 5 REL

Multimedia S
Standards 9

DII example: MPEG-21 music album

BTE550 Internet and Intranet Applications

697

BTE550 Internet and Intranet Applications

Improvements over MPEG-4 IPMP:


Internetworking
IPMP tool retrieval & authentication
Integration of Rights Expressions (RDD & REL)
Intellectual Property Management and Protection involves
the enforcement of REL permissions
IPMP shall consult REL before any actions are taken in
the Users system
REL: What is protected? What right applies?
IPMP: How is it protected?
698

701

MPEG-21 Part 6 Rights Data Dictionary

Rights Expression Language


A machine-readable language
Can declare rights and permissions
Uses terms defined in the Rights Data Dictionary

Multimedia S
Standards 9

Multimedia S
Standards 9

REL grant consist of


principal to whom grant is issued
rights the grant specifies
resource to which right in grant applies
condition to be met before grant can be exercised
BTE550 Internet and Intranet Applications

MPEG-21 Part 5 REL

BTE550 Internet and Intranet Applications

700

REL data model


Multimedia S
Standards 9

Multimedia S
Standards 9

MPEG-21 Part 4 IPMP

BTE550 Internet and Intranet Applications

The Rights Expression Language consists of licenses and


grants that give specific permissions to Users to perform
certain actions on certain resources, given that certain
conditions are met
Grants can also allow Users to delegate authority to
others
Users system shall parse and validate the RE
Users system shall check permissions before any further
action is done
DID parser is responsible for discovering and identifying
where to gather licenses
REL licenses are wrapped in Digital Items

699

Set of clear, consistent, structured, integrated and uniquely


identified Terms to support REL
Specification of dictionary structure and methodology to
create dictionary
Dictionary is prescriptive, inclusive, and has audit
pprovisions
Legal definitions are mapped from other Authorities
Supports mapping & transformation of metadata from
terminology of one namespace (or Authority) into that of
another namespace in automated or partially automated
way
Dictionary is based on a logical model, the Context
Model, which is the basis of the dictionary ontology
BTE550 Internet and Intranet Applications

702

117

Goal: achieve transparent interoperable


access to distributed multimedia content
Enable ad hoc formation of User
communities in which contents is shared
with agreed or contracted
Quality
Reliability
Flexibility
Diversity
Guaranteed user experience
BTE550 Internet and Intranet Applications

Scope of standardization
Multimedia S
Standards 9

Multimedia S
Standards 9

MPEG-21 Part 7 Digital Item Adaptation

703

User Characteristics
Terminal Capabilities
Network Characteristics
Natural Environment Characteristics
Resource Adaptability
p
y
Session Mobility

BTE550 Internet and Intranet Applications

Multimedia S
Standards 9

Overview of DIA Tools

Multimedia S
Standards 9

Concept of Digital Item Adaptation

706

BTE550 Internet and Intranet Applications

704

BTE550 Internet and Intranet Applications

Bitstream Syntax Description


Multimedia S
Standards 9

Multimedia S
Standards 9

Relation between DIA and other MPEG-21parts

BTE550 Internet and Intranet Applications

705

707

A BSD describes the syntax (high level structure) of a


binary media resource
BSDL: XML schema based language to design specific
bitstream syntax schemas for particular media formats
gBS schema: generic schema enabling the construction of
resource format independent bitstream syntax descriptions

BTE550 Internet and Intranet Applications

708

118

Multimedia S
Standards 9

AdaptationQoS BSD

Multimedia S
Standards 9

Adaptation architecture

BTE550 Internet and Intranet Applications

709

BTE550 Internet and Intranet Applications

(g)BSD, AdaptationQoS, and Link

AdaptationQoS specifies the relationship between constraints and


feasible adaptation operations
Constraints: BandwidthInkbps, ComputationTimeInMillisecs
Utilities (qualities): PSNRIndB
Adaptation Methods:
frameDroppingAndOrCoefficientDropping, requantization,
fineGranularScalability, waveletReduction, spatialSizeReduction
UtilityFunction:
Utilit F ti
describes possible adaptation operators and associated qualities
using a set of constraint points as indexes
Linear interpolation is assumed between constraint points
LookUpTable:
additional multi-dimensional sets of data to support more
elaborate adaptation scenarios
StackFunction
tool for describing the data in numerical function format
710

BTE550 Internet and Intranet Applications

713

MPEG-21 Part 8 Reference Software

MetadataAdaptationHint describes adaptation hint information


pertaining to metadata within a digital item
Hint: a set of syntactical elements with prior knowledge about the
metadata that is useful for reducing the complexity of the metadata
adaptation process

711

Multimedia S
Standards 9

Multimedia S
Standards 9

Metadata Adaptability

BTE550 Internet and Intranet Applications

712

Multimedia S
Standards 9

Multimedia S
Standards 9

Terminal and Network Quality of Service

BTE550 Internet and Intranet Applications

Link In some cases it is convenient to specify intrinsic


operations based on a universal model for scalable bitstreams
A specified operation can be seen as a link that is
composed of a mapping condition between identified
parameters and an operation, which is conducted if this
mapping is fulfilled

Success of a standard depends on the availability of


reference software
Plan to use the software developed in Core Experiments
(CE) as a basis
Platform independence
Future repository (requires membership)
http://mpeg.nist.gov/cvsweb/MPEG-21/
Temporary repository
http://www.titr.uow.edu.au/cgi-bin/mpeg-ref-sw.pl
Current main issue: parsing DID
BTE550 Internet and Intranet Applications

714

119

Digital Item Processing

Digital Items act as a structure for organizing resources


and its descriptions
Need a mechanism for defining a set of operations by
which a terminal can process a DI or DID
Currently considering to specify a set of operations that
can be used to process DIs: Digital Item Method
A DIM defines an intended method for configuring,
manipulating and/or validating a DI

BTE550 Internet and Intranet Applications

Multimedia S
Standards 9

Multimedia S
Standards 9

Digital Item Processing

715

BTE550 Internet and Intranet Applications

DIP flow control

Interoperability of Digital Items means that terminals must


handle the DIs in a consistent manner
Digital Item Methods provide a way to specify a selection
of preferred procedures by which the DI should be
handled at the DI level
a menu of user interaction possibilities
Digital Item Processing encompasses all aspects of
processing a DI from an application perspective
Applications build DIP environments around a
fundamental DIME
BTE550 Internet and Intranet Applications

Multimedia S
Standards 9

Multimedia S
Standards 9

Methods vs. processing

716

BTE550 Internet and Intranet Applications

719

MPEG-21 Part 9 File Format

CDI Content Digital Item


a DID containing the actual content
DIBO Digital Item Base Operation
DIM Digital Item Method
method that can by applied to a DID
DIME DIM E
Engine
i
part of the terminal responsible for executing the DIM
DIML DIM Language
DIP Digital Item Processing
MI Method Item
PI Processing Item
XDI Context Digital Item

Multimedia S
Standards 9

Multimedia S
Standards 9

Digital Item processing terminology

BTE550 Internet and Intranet Applications

718

717

An MPEG-21 file format shall be capable of storing


MPEG-21 Digital Items
all components of the DI within a single file
The MPEG-21 file format will inherit several concepts
from MP4, in order to make multi-purpose files possible

BTE550 Internet and Intranet Applications

720

120

What is JavaScript?

10

JavaScrript

10

Dynamic HTML:
JavaScript

BTE550 Internet and Intranet Applications

Copyleft 2005, Binnur Kurt

724

Are Java and JavaScript the Same?

10

What is JavaScript?
What can a JavaScript Do?
How to include JavaScript in html?
JavaScript Variables
JavaScript Expressions
Control Statements
DOM and Events

JavaScrript

JavaScrript

10

Content

JavaScript was designed to add interactivity to HTML


pages
JavaScript is a scripting language (a scripting language is
a lightweight programming language)
A JavaScript consists of lines of executable computer
code
A JavaScript is usually embedded directly into HTML
pages
JavaScript is an interpreted language (means that scripts
execute without preliminary compilation)
Everyone can use JavaScript without purchasing a license

NO!!
Java and JavaScript are two completely different
languages in both concept and design!
Java (developed by Sun Microsystems) is a powerful and
much more complex programming language - in the same
category as C# and C++.

Javascript and XSLT


Setting and Reading Cookies

722

BTE550 Internet and Intranet Applications

725

What can a JavaScript Do?

Language developed by Netscape


Primary purpose is for "client-end" processing of HTML
documents
JavaScript code is embedded within the html of a
document
An interpreter in the browser interprets the JavaScript
code when appropriate
Code typically allows for "preprocessing" of forms and
can add "dynamic content" to a Web page

JavaScript gives HTML designers a programming tool


HTML authors are normally not programmers, but
JavaScript is a scripting language with a very simple
syntax! Almost anyone can put small "snippets" of code
into their HTML pages
p can p
put dynamic
y
text into an HTML p
page
g
JavaScript
A JavaScript statement like this: document.write("<h1>" +
name + "</h1>") can write a variable text into an HTML
page
JavaScript can react to events: A JavaScript can be set
to execute when something happens, like when a page has
finished loading or when a user clicks on an HTML
element

BTE550 Internet and Intranet Applications

723

10

What is JavaScript?

JavaScrript

JavaScrript

10

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

726

121

JavaScript can read and write HTML elements: A


JavaScript can read and change the content of an HTML
element
JavaScript can be used to validate data: A JavaScript
can be used to validate form data before it is submitted to
a server, this will save the server from extra processing
JavaScript can be used to detect the visitor's browser:
A JavaScript can be used to detect the visitor's browser,
and - depending on the browser - load another page
specifically designed for that browser
JavaScript can be used to create cookies: A JavaScript
can be used to store and retrieve information on the
visitor's computer
BTE550 Internet and Intranet Applications

10

How to include JavaScript in html?

JavaScrript

JavaScrript

10

What can a JavaScript Do?

727

However, if we want to prevent the script itself


from being seen, we can upload our JavaScript
from a file
This will show only the upload tag in our final
p from the file
document,, not the JavaScript
Use the src option in the tag
<SCRIPT
SCRIPT type = "text/javascript" src
src="bogus.js"></SCRIPT
SCRIPT>

BTE550 Internet and Intranet Applications

How to include JavaScript in html?

Simple Example

10

<script type = "text/javascript">


ACTUAL JavaScript code here
</script>

JavaScrript

JavaScrript

10

JavaScript programs require the <SCRIPT> tag in


.html files

These can appear in either the <HEAD> or


<BODY> section of an html document
Functions and code that may execute multiple times is
typically placed in the <HEAD>
These are only interpreted when the relevant
function or event-handler are called
BTE550 Internet and Intranet Applications

728

<HTML>
<HEAD>
<TITLE>First JavaScript Example</TITLE>
</HEAD>
<BODY>
<H2>This line is straight HTML</H2>
<H3>
<SCRIPT type = "text/javascript">
document write("These
document.write(
These lines are produced by<br/>");
by<br/> );
document.write("the JavaScript program<br/>");
alert("Hey, JavaScript is fun!");
</SCRIPT>
</H3>
<H2>More straight HTML</H2>
<SCRIPT type = "text/javascript src="bogus.js"></script>
</BODY>
</HTML>

BTE550 Internet and Intranet Applications

How to include JavaScript in html?

Some browsers may not support scripting


To be safe, you can put your scripts in html comments
This way browsers that do not recognize JavaScript
will look at the programs as comments

Note that, unlike PHP scripts, JavaScripts are


visible in the client browser
Since they are typically acting only on the client, this is
not a problem
729

10

JavaScript variables have no types

JavaScrript

10
JavaScrript

731

JavaScript Variables

Code that needs to be executed only once, when the


document is first loaded is placed in the <BODY>

BTE550 Internet and Intranet Applications

730

Type is determined dynamically, based on the value stored


This is becoming familiar!
The typeof operator can be used to check type of a
variable

D l i
Declarations
are made
d using
i the
h var keyword
k
d

Can be implicitly declared, but not advisable


Declarations outside of any function are global
Declarations within a function are local to that function
Variables declared but not initialized have the value
undefined

BTE550 Internet and Intranet Applications

732

122

A big difference between JavaScript and other languages


like JAVA and C is that JavaScript is untyped.
This means that a JavaScript variable can hold a value of
any data type, and its data type does not have to be set
when declaring the variable.
This allows you to change the data type of a variable
during the execution of your program, for example:
var x = 10;
x = "ten";
In this example the variable x is first assigned the integer
value of 10, and then the string value of the word ten.
BTE550 Internet and Intranet Applications

10

JavaScript Expressions

JavaScrript

JavaScrript

10

JavaScript Variables

733

BTE550 Internet and Intranet Applications

JavaScript Variables

10
JavaScrript

10
JavaScrript

Cannot use a keyword


Must begin with a letter, $, or _
Followed by any sequence of letters, $, _ or digits
Case sensitive
When you declare a variable within a function,
function the variable can
only be accessed within that function. When you exit the
function, the variable is destroyed.
These variables are called local variables. You can have local
variables with the same name in different functions
If you declare a variable outside a function, all the functions on
your page can access it. The lifetime of these variables starts
when they are declared, and ends when the page is closed.
734

737

Control Statements

10

Numeric operators in JavaScript are similar to those in


most languages
+, , *, /, %, ++, - Precedence and associativity are also fairly standard
g
Strings
Have the + operator for concatenation
Have a number of methods to do typical string
operations
charAt, indexOf, toLowerCase, substring
Looks kind of like Java intentionally

JavaScrript

10
JavaScrript

Relational operators:
==, !=, <, >, <=, >=
The above allow for type coercion. To prevent
coercion there is also
===,, !==
Similar to PHP
Boolean operators
&&, ||, !
&&, || are short-circuited (as in Java and PHP)

BTE550 Internet and Intranet Applications

JavaScript Expressions

BTE550 Internet and Intranet Applications

736

Control Statements

Variable identifiers are similar to those in other languages

BTE550 Internet and Intranet Applications

Similar to PHP, with mixed number/string type


expressions, JavaScript will coerce if it can
If operator is + and an operand is string, it will always
coerce other to string
If operator is arithmetic, and string value can be
coerced to a number it will do so
If string is non-numeric, result is NaN
(NotaNumber)
We can also explicitly convert the string to a number
using parseInt and parseFloat
Again looks like Java

Control statements similar to Java


if, while, do, for, switch
Variables declared in for loop header are global to
the rest of the script
Functions
Similar to C++ functions, but
Header is somewhat different
function name(param_list)
Return type not specified (since JS has dynamic typing)
Param types also not specified

735

BTE550 Internet and Intranet Applications

738

123

Array Objects

10

No parameter type-checking
Numbers of formal and actual parameters do not have to
correspond
Extra actual parameters are ignored
Extra formal parameters are undefined
All actual parameters can be accessed regardless of formal
parameters by using the arguments array
BTE550 Internet and Intranet Applications

JavaScrript

JavaScrript

10

Functions
Functions execute when they are called, just as in any
language
To allow this, function code should be in the <HEAD>
section of the .html file
Variables declared in a function are local to the function
Parameters are all value

739

BTE550 Internet and Intranet Applications

Array Objects

10

Creating arrays
Using the new operator and a constructor with multiple
arguments
var A = new Array("hello", 2, "you");
Using the new operator and a constructor with a single
numeric argument
var B = new Array(50);
Using square brackets to make a literal
var C = ["we", "can", 50, "mix", 3.5, "types"];

JavaScrript

10
JavaScrript

Size can be changed and data can be mixed


Cannot use arbitrary keys as with PHP arrays

740

743

Array Objects

JavaScrript

Like in Java, length is an attribute of all array objects


However, like in Javascript it does not necessarily
represent the number of items or even mem. locations
in the array
Unlike Java, length can be changed by the programmer
Actual memory allocation is dynamic and occurs when
necessary

10

Array Length
10

sort
Sort by default compares using alphabetical order
To sort using numbers we pass in a comparison
function defining how the numbers will be
compared
reverse
Reverse the items in an array

BTE550 Internet and Intranet Applications

Array Objects

JavaScrript

742

Array Objects

More relaxed version of Java arrays

BTE550 Internet and Intranet Applications

Array Methods
There are a number of predefined operations that you
can do with arrays
concat two arrays into one
jjoin arrayy items into a single
g string
g ((commas
between)
push, pop, shift, unshift
Push and pop are a "right stack"
Shift and unshift are a "left stack"

These operations are invoked via method calls, in an


object-based way
Also many, such as sort and reverse are mutators,
affecting the array itself
JavaScript also has 2-dimensional arrays
Created as arrays of arrays, but references are not
needed

An array with length = 1234 may in fact have memory


allocated for only a few elements
When accessed, empty elements are undefined
BTE550 Internet and Intranet Applications

741

BTE550 Internet and Intranet Applications

744

124

JavaScript Objects

BTE550 Internet and Intranet Applications

10

JavaScript is an object-based language


It is NOT object-oriented
It has and uses objects, but does not support some
features necessary for object-oriented languages
Class inheritance and p
polymorphism
y
p
not supported
pp
They can be faked but are not really there

JavaScrript

JavaScrript

10

JavaScript Objects

745

Once an object is constructed, I can change its properties if I want


to
Lets say I want to add a method to my TV called
show_properties
function show_properties() {
document.write("Here is your TV: <BR/>");
document.write("Brand: ", this.brand,"<BR/>");
document.write("Size: ", this.size, "<BR/>");
document.write("Input Jacks: ");
document.write(this.jacks.input, "<BR/>");
document.write("Output Jacks: ");
document.write(this.jacks.output, "<BR/>");
}

my_tv.show = show_properties;

JavaScript objects are actually represented as property-value pairs


Very much like Perl hashes
The object is analogous to the hash name, and the properties are
analogous to the data stored in the hash
Properties can be data or functions (methods)
Ex:
var my_tv = new Object();
my_tv.brand = "Sony";
my_tv.size = 27;
my_tv.jacks = new Object();
my_tv.jacks.input = 3;
my_tv.jacks.output = 2;

10

Regular Expressions

JavaScrript

JavaScrript

10

JavaScript Objects

BTE550 Internet and Intranet Applications

JavaScript regular expression handling is also based on


that in Perl
The patterns and matching procedures are the same as
in Perl
However, now the functions are methods within a string
object
var
var
var
var
s =

s = "a
a man, a plan, a canal: panama";
panama ;
loc = s.search(/plan/);
matches1 = s.match(/an/g);
matches2 = s.match(/\w+/g);
s.replace(/\W/g, "-");

Note that match is a sort of complement to split


Returns the matched pieces as opposed to the
non-matched pieces
Similar to PHP when using an argument for the
result

746

DOM

Note that the objects can be created and their properties can
be changed dynamically
Also, objects all have the same data type object
We can write constructor functions for objects if we'd like,
but these do not create new data types just easy ways of
uniformly initializing objects

10

The Document Object Model

JavaScrript

10
JavaScrript

749

BTE550 Internet and Intranet Applications

JavaScript Objects

function TV(brand
TV(brand, size
size, injacks
injacks, outjacks)
{
this.brand = brand;
this.size = size;
this.jacks = new Object();
this.jacks.input = injacks;
this.jacks.output = outjacks;
}

var my_tv = new TV("Sony", 27, 3, 2);


BTE550 Internet and Intranet Applications

748

BTE550 Internet and Intranet Applications

747

Developed by W3C (World-Wide Web Consortium)


http://www.w3c.org/DOM/
Specifies the contents of Web documents in an object-oriented way
Allows programming languages to access and manipulate the
components of documents
Defined at a high level so that a variety of languages can be
used with it
It is still being updated / revised
We are not even scratching the surface here

BTE550 Internet and Intranet Applications

750

125

Example: Pre-processing a Form

BTE550 Internet and Intranet Applications

10

With documents DOM specifies events and event


handlers
Event model is similar to the one used in Java
Different parts of a document have different events
associated with them
We can define handlers to react to these events
These allow us to "interact" with and add "dynamic
content" to web documents
Ex: Can preprocess form elements
Ex: Can load / update / change what is displayed in
response to an event

JavaScrript

JavaScrript

10

Events

751

BTE550 Internet and Intranet Applications

DOM and Events

10
JavaScrript

10
JavaScrript

Each document has access to its properties and to the components


that are declared within it
Ex: title, URL, forms[], applets[], images[]
Attributes with IDs can also be specified by ID
Once we know the components,
components events and event-handlers,
event handlers we
can write JavaScript programs to process Web pages on the
client-side
Client computers are typically less busy than servers, so
whatever we can do at the client will be helpful overall
Ex: Checking form correctness before it is submitted

752

755

Javascript and XSLT

10

In HTML documents events are specified through


tag attributes
Within the tag identifying an HTML component,
we can specify in an attribute how the component
reacts to various events
Similar in idea to Java, we assign event handling
code to the tag attributes, and the code executes
when the event occurs

JavaScrript

10
JavaScrript

We can check individual components as they are


entered as well
Ex: <input type = "text"> has the onchange
attribute
Triggered when contents are changed and
focus changes
Ex: <input type = "radio"> has the onclick
attribute
Triggered when the radio button is clicked
with the mouse
BTE550 Internet and Intranet Applications

DOM and Events

BTE550 Internet and Intranet Applications

754

Example: Pre-processing a form

document refers to the top-level document

BTE550 Internet and Intranet Applications

A very common client-side operation is pre-processing a


form
Ensure that fields are filled and formatted correctly, so
server does not have to
Saves load on the server, saves time and saves
b d id h
bandwidth
We can check a form overall by using the attribute
onsubmit
We can put it right into the form as an attribute
Or we can assign the attribute through the
document object

753

function doTransform(source){
var objXSL= new
ActiveXObject("Msxml2.DOMDocument");
objXSL.load("query1.xslt
query1.xslt");
str=source.transformNode(objXSL);
return (str);
}

BTE550 Internet and Intranet Applications

756

126

10

BTE550 Internet and Intranet Applications

758

761

Complicated Cookie Reading

10

Create a string and then set the document.cookie property to that.


Cookie values must never have spaces, commas, or semicolons.
a pair of functions will code and decode your properties
escape()
unescape().

JavaScrript

10
JavaScrript

760

If you want your cookie to contain more than just one


piece of information, you can make the value of the
cookie as long as you want (up to the 4000 character
limit).
Say you're looking to store a person's name, age, and
phone
h
number.
b
You do something like this:
var the_cookie = "username:binnur/age:32/phone:2853608";
document.cookie= "my_cookie=" + escape(the_cookie);

BTE550 Internet and Intranet Applications

Setting Cookies

ffunction
ti setCookie(){
tC ki (){
var the_name = prompt("What's your name?","");
var the_cookie = "wm_javascript=" + escape("username:" +
the_name);
document.cookie = the_cookie;
alert("Thanks, now go to the next page.");
}
BTE550 Internet and Intranet Applications

Once you save a cookie to someone's hard disk, it's easy to


read.
Here's the code that reads the example cookie
function readCookie(){
var the_cookie = document.cookie;;
var the_cookie = unescape(the_cookie);
var broken_cookie = the_cookie.split(":");
var the_name = broken_cookie[1];
alert("Your name is: " + the_name);
}

Complicated Cookie Reading

<html>
<head>
<title>Javascript and XSLT</title>
</head>
<body>
<script type = "text/javascript">
function doTransform(source){
var objXSL= new ActiveXObject("Msxml2.DOMDocument");
objXSL.load("query1.xslt");
str=source.transformNode(objXSL);
return (str);
}
var src= new ActiveXObject("Msxml2.DOMDocument");
src.load("imdb.xml");
var str=doTransform(src);
document.write(str);
</script>
</body>
</html>
BTE550 Internet and Intranet Applications

JavaScrript

757

10

10

BTE550 Internet and Intranet Applications

JavaScrript

Reading Cookies

JavaScrript

JavaScrript

10

query1.xslt
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="MovieDataBase">
<html>
<head>
<title>Query</title>
</head>
<body>
s : o e c select="Movie">
se ec
ov e
<xsl:for-each
<xsl:for-each select="Genre">
<xsl:if test="node()='Drama'">
<li><xsl:value-of select="..//Title"/></li>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

759

I use a slash to separate property names and a colon to


distinguish the property name from the property value.
The slash and colon are arbitrary choices they could be
anything, like say, this:
var the_cookie="username:binnur&age:32&phone:2853608";
document.cookie= "my_cookie="+escape(the_cookie);
The delimiters you choose are up to you. Just remember
what you used so you can decode the cookie later.

BTE550 Internet and Intranet Applications

762

127

BTE550 Internet and Intranet Applications

10

More About Cookies

JavaScrript

10
JavaScrript

function readTheCookie(the_info) {
// load the cookie into a variable and unescape it
var the_cookie = document.cookie;
var the_cookie = unescape(the_cookie);
// separate the values from the cookie name
var broken_cookie = the_cookie.split("=");
var the_values = broken_cookie[1];
// break each name:value pair into an array
var separated_values = the_values.split("/");
// loop through the list of name:values and load up the associate array
var property_value
l = "";
""
for (var loop = 0; loop < separated_values.length; loop++) {
property_value = separated_values[loop];
var broken_info = property_value.split(":");
var the_property = broken_info[0];
var the_value = broken_info[1];
the_info[the_property] = the_value;
}
}
763

If you do want to save cookies on users' hard drives, you


have to set an expiration date, which has to be in a special
format called GMT. For example:
Mon, 27-Apr-1998 00:00:00 GMT
To set an expiration date to some distant time in the future
var the_date = new Date("December 31, 2023");
var the_cookie_date = the_date.toGMTString();

BTE550 Internet and Intranet Applications

10

764

function readCookie(name) {
if (document.cookie == '') {
// there's no cookie, so go no further
return false;
} else {
var firstChar, lastChar;
var theBigCookie = document.cookie;
firstChar = theBigCookie.indexOf(name);
// find the start of 'name'
if(firstChar != -1) {
// if you found the cookie
firstChar += name.length + 1;
// skip
ki 'name'
'
' andd '='
' '
lastChar = theBigCookie.indexOf(';', firstChar);
// Find the end of the value string (i.e. the next ';').
if(lastChar == -1) lastChar = theBigCookie.length;
return unescape(theBigCookie.substring(firstChar, lastChar));
} else {
// If there was no cookie of that name, return false.
return false;
}
}
}
BTE550 Internet and Intranet Applications

10

To save multiple cookies, just give each cookie a different


name. If you're adding a new cookie, setting
document.cookie doesn't delete cookies that are already
there:
var the_cookie = "my_happy_cookie=happiness_and_joy";
document.cookie = the_cookie;
var another_cookie=
"my_other_cookie=more_joy_more_happiness";
document.cookie = another_cookie;
You have access to both cookies.
BTE550 Internet and Intranet Applications

JavaScrript

More About Cookies

JavaScrript

JavaScrript

10

Reading and Writing Multiple Cookies

766

Here's how to build a cookie that will last until the end of the Mayan
calendar:
function setCookie() {
// get the information
var the_name = prompt("What's your name?","");
var the_date = new Date("December 31, 2023");
var the_cookie_date = the_date.toGMTString();
// build and save the cookie
var the_cookie = "my_cookie=" + escape(the_name);
the_cookie = the_cookie + ";expires=" + the_cookie_date;
document.cookie = the_cookie;
}
BTE550 Internet and Intranet Applications

11
765

767

Forms, JSP, and PHP

Copyleft 2005, Binnur Kurt

128

HTML Forms
Form Elements
Submission Methods: GET, POST
Form Processing: Server Side Processing
JSP
PHP

BTE550 Internet and Intranet Applications

In Netscape, a textfield component looks like this:


11

Textfield Component

Forms, JSP
P, PHP

Forms, JSP
P, PHP

11

Content

769

The HTML content for this component is:


16 <p>
17 This form allows you to create a new soccer league.
18 </p>
19
20 <form action=add_league.do method=POST>
21 Year: <input type=text name=year /> <br/><br/>
BTE550 Internet and Intranet Applications

Creating an HTML Form

772

Drop-Down List Component

11

11

Forms, JSP
P, PHP

Forms, JSP
P, PHP

In Netscape, a drop-down list component looks like this:

BTE550 Internet and Intranet Applications

770

BTE550 Internet and Intranet Applications

773

Submit Button

The following is a partial structure of an HTML form:


<form action='URL TO CONTROLLER' method='GET or POST'>
<!-- PUT FORM COMPONENT TAGS HERE -->
</form>
For example:
<fform
<form
f
action=add_league.do
ti
dd l
d method=POST>
th d POST>
Year: [textfield tag]
Season: [drop-down list tag]
Title: [textfield tag]
[submit button tag]
</form
form>
A single web page can contain many forms.

11

In Netscape, a submit button component might look like


this:
Forms, JSP
P, PHP

Forms, JSP
P, PHP

11

The FORM Tag

BTE550 Internet and Intranet Applications

The HTML content for this component is:


22 Season: <select name=season>
23 <option value=UNKNOWN>select...</option>
24 <option value=Spring>Spring</option>
25 <option value=Summer>Summer</option>
26 <option value=Fall>Fall</option>
27 <option value=Winter>Winter</option>
28 </select> <br/><br/>

771

p
is:
The HTML content for this component
29 Title: <input type=text name=title /> <br/><br/>
30 <input type=submit value=Add League />
31 </form>

BTE550 Internet and Intranet Applications

774

129

Forms, JSP
P, PHP

11

The hello.jsp Page

JSP
JS
P

<%! private static final String DEFAULT_NAME = World; %>


<html>
<head>
<title>Hello JavaServer Page</title>
</head>
<%-- Determine the specified name (or use default) --%>
<%
String name = request.getParameter(name);
if ( (name == null) || (name.length() == 0) ) {
name = DEFAULT_NAME;
}
%>
<body bgcolor=white>
<b>Hello, <%= name %></b>

775

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

Steps of JSP Page Processing

BTE550 Internet and Intranet Applications

11

Describe JSP technology


Write JSP code using scripting elements
Write JSP code using the page directive
Write JSP code using standard tags

Forms, JSP
P, PHP

Forms, JSP
P, PHP

11

Objectives

776

BTE550 Internet and Intranet Applications

779

JSP Processing: Translation Step

11

JavaServer Pages enable you to write standard HTML


pages containing tags that run powerful programs based
on Java technology.
The goal of JSP technology is to support separation of
presentation and business logic:
Web designers can design and update pages without
learning the Java programming language.
Programmers for Java platform can write code without
dealing with web page design.

The first time a JSP page is requested, Web container converts the JSP
file into a servlet that can respond to the HTTP request.
Web container translates the JSP file into a Java source file that
contains a servlet class definition.

Forms, JSP
P, PHP

Forms, JSP
P, PHP

11

JavaServer Pages Technology

BTE550 Internet and Intranet Applications

778

777

BTE550 Internet and Intranet Applications

780

130

BTE550 Internet and Intranet Applications

11

Developing and Deploying JSP Pages

Forms, JSP
P, PHP

Forms, JSP
P, PHP

11

JSP Processing: Compilation Step


Web container compiles the servlet source code into a Java
class file.
This servlet class bytecode is then loaded into the Web
containers JVM

781

11
Forms, JSP
P, PHP

Forms, JSP
P, PHP

11

JSP Scripting Elements

Web container creates an instance of the servlet class and


performs the initialization life cycle step by calling the
special jspInit method.

782

JSP scripting elements are embedded with the <% %>


tags and are processed by the JSP engine during
translation of the JSP page.
There are five types of scripting elements:
Comments <%-- comment --%>
Directive tag <%@ directive %>
Declaration tag <%! decl %>
Scriptlet tag <% code %>
Expression tag <%= expr %>

Forms, JSP
P, PHP

Finally, the Web container can call the _jspService method


for the converted JSP page so that it can respond to client
HTTP requests.

11

Comments

Forms, JSP
P, PHP

11

785

BTE550 Internet and Intranet Applications

JSP Processing: Service Step

BTE550 Internet and Intranet Applications

784

BTE550 Internet and Intranet Applications

JSP Processing: Initialization Step

BTE550 Internet and Intranet Applications

Deploying JSP pages is as easy as deploying static pages.


JSP pages are placed in the same directory hierarchy as
HTML pages.
In the development environment, JSP pages are in the web
directory.
In the deployment environment, JSP pages are placed at the
top-level directory of the Web application.

783

HTML comments
<!-- This is an HTML comment. It will show up in the response. -->
JSP page comments
<%-- This is a JSP comment. It will only be seen in the JSP code.
It will not show up in either the servlet code or the response.
--%>
Java comments
Java comments can be embedded with scriptlet and declaration
tags.
These comments are included in the servlet source code during
the translation phase, but do not appear in the HTTP response.
<%
/* This is a Java comment. It will show up in the servlet code.
It will not show up in the response. */
%>
BTE550 Internet and Intranet Applications

786

131

A directive tag provides information that will affect the


overall translation of the JSP page.
The syntax for a directive tag is:
<%@ DirectiveName [attr=value]* %>
yp of directives are currentlyy specified
p
in the JSP
Three types
specification: page, include, and taglib.
Here are a couple of examples:
<%@ page session=false %>
<%@ include file=incl/copyright.html %>

BTE550 Internet and Intranet Applications

11

Declaration Tag

Forms, JSP
P, PHP

Forms, JSP
P, PHP

11

Directive Tag

787

You can use a declaration tag to override the jspInit and jspDestory
life cycle methods.
The signature of these methods has no arguments and returns void.
For example:
<%!
public void jspInit() {
/* initialization code here */
}
public void jspDestroy() {
/* clean up code here */
}
%>

A declaration tag allows you to include members in the JSP servlet


class, either attributes or methods.
The syntax for a declaration tag is:
<%! JavaClassDeclaration %>
Here are a couple of examples:
<%! public static final String DEFAULT_NAME
DEFAULT NAME = World;
World ; %>
<%! public String getName(HttpServletRequest request) {
return request.getParameter(name);
}
%>
<%! int counter = 0; %>

11

Scriptlet Tag

Forms, JSP
P, PHP

Forms, JSP
P, PHP

11

Declaration Tag

BTE550 Internet and Intranet Applications

788

A scriptlet tag allows the JSP page developer to include arbitrary


Java technology code in the jspService method.
The syntax for a declaration tag is:
<% JavaCode %>
Here are a couple of examples:
<% int i = 0; %>
In this example, the local variable i is declared with an initial value
of 0.
<% if ( i > 10 ) { %>
I is a big number.
<% } else { %>
I is a small number
<% } %>

You can think of one JSP page as being equivalent to one


servlet class.
The declaration tags become declarations in the servlet
class. You can create any type of declaration that is
permissible in a regular Java technology class: instance
variables,
i bl instance
i
methods,
h d class
l (or
( static)
i ) variables,
i bl
class methods, inner classes, and so on.
Be careful with using instance and class variables due to
concurrency issues.
A JSP page is multithreaded like a servlet.
789

11

Scriptlet Tag

Forms, JSP
P, PHP

11
Forms, JSP
P, PHP

791

BTE550 Internet and Intranet Applications

Declaration Tag

BTE550 Internet and Intranet Applications

790

BTE550 Internet and Intranet Applications

<TABLE BORDER=1 CELLSPACING=0


CELLPADDING=5>
<TR><TH>number</TH><TH>squared</TH></TR>
<% for ( int i=0; i<10; i++ ) { %>
<TR><TD><%= i %></TD><TD><%= ((i * i))
%></TD></TR>
<% } %>
</TABLE>

BTE550 Internet and Intranet Applications

792

132

Implicit Variables

BTE550 Internet and Intranet Applications

11

An expression tag holds a Java language expression that is


evaluated during an HTTP request.
The result of the expression is included in the HTTP
response stream.
The syntax for a declaration tag is:
<%= JavaExpression %>

Forms, JSP
P, PHP

Forms, JSP
P, PHP

11

Expression Tag

793

<B>Ten is <%= (2 * 5) %></B>


This example shows an arithmetic expression. When this
is evaluated, the number 10 is the result. The string
<B>Ten is 10</B> is sent back in the HTTP response
stream.
Thank you
you, <I><%=
<I><% name %></I>
%></I>, for registering for the
soccer league.
This example shows that you can access local variables
declared in the JSP page. If the name variable holds a
reference to a String object, then that string is sent back in
the HTTP response stream.
The current day and time is: <%= new java.util.Date() %>
794

11

The page Directive

Forms, JSP
P, PHP

Forms, JSP
P, PHP

11

Expression Tag

BTE550 Internet and Intranet Applications

796

BTE550 Internet and Intranet Applications

The page directive is used to modify the overall


translation of the JSP page.
For example, you can declare that the servlet code
generated from a JSP page requires the use of the Date
class:
<%@
%@ page import=java.util.Date
i
j
il D %>
%
The import attribute defines the set of classes and
packages that must be imported in the servlet class
definition. The value of this attribute is a commadelimited list of fully-qualified class names or packages:
import=java.sql.Date,java.util.*,java.text.*
797

BTE550 Internet and Intranet Applications

Forms, JSP
P, PHP

11

Implicit Variables
The Web container gives the JSP technology developer
access to the following variables in scriptlet and
expression tags.
These variables represent commonly used objects for
servlets that JSP developers might need to use.
For example, you can retrieve HTML form parameter data
by using the request variable, which represents the
HttpServletRequest object.

BTE550 Internet and Intranet Applications

795

PHP

BTE550 Internet and Intranet Applications

798

133

PHP stands for PHP: Hypertext Preprocessor


PHP is a server-side scripting language, like ASP
PHP scripts are executed on the server
PHP supports many databases (MySQL, Informix,
y
, Solid,, PostgreSQL,
g Q , Generic ODBC,, etc.))
Oracle,, Sybase,
PHP is an open source software (OSS)
PHP is free to download and use

As of August 2004, PHP is used on 16,946,328 Domains,


1,348,793 IP Addresses http://www.php.net/usage.php
This is roughly 32% of all domains on the web.

BTE550 Internet and Intranet Applications

11

Brief History of PHP

Forms, JSP
P, PHP

11
Forms, JSP
P, PHP

What is PHP?

799

BTE550 Internet and Intranet Applications

PHP (PHP: Hypertext Preprocessor) was created by


Rasmus Lerdorf in 1994. It was initially developed for
HTTP usage logging and server-side form generation in
Unix.
PHP 2 (1995) transformed the language into a Server-side
embedded
b dd d scripting
i ti language.
l
Added
Add d database
d t b
support,
t file
fil
uploads, variables, arrays, recursive functions,
conditionals, iteration, regular expressions, etc.
PHP 3 (1998) added support for ODBC data sources,
multiple platform support, email protocols (SNMP,IMAP),
and new parser written by Zeev Suraski and Andi Gutmans
BTE550 Internet and Intranet Applications

11

What is a PHP File?

Forms, JSP
P, PHP

Forms, JSP
P, PHP

11

Brief History of PHP

800

803

PHP 4 (2000) became an independent component of the


web server for added efficiency. The parser was renamed
the Zend Engine. Many security features were added.
PHP 5 (2004) adds Zend Engine II with object oriented
programming, robust XML support using the libxml2
library, SOAP extension for interoperability with Web
Services SQLite has been bundled with PHP
Services,

801

11

What is MySQL?

Forms, JSP
P, PHP

11
Forms, JSP
P, PHP

PHP files may contain text, HTML tags and scripts


PHP files are returned to the browser as plain HTML
PHP files have a file extension of ".php", ".php3", or
".phtml"

BTE550 Internet and Intranet Applications

Brief History of PHP

BTE550 Internet and Intranet Applications

802

MySQL is a small database server


MySQL is ideal for small and medium applications
MySQL supports standard SQL
MySQL compiles on a number of platforms
MySQL is free to download and use

BTE550 Internet and Intranet Applications

804

134

PHP

Forms, JSP
P, PHP

11

PHP combined with MySQL are cross-platform (means


that you can develop in Windows and serve on a Unix
platform)

Forms, JSP
P, PHP

805

Forms, JSP
P, PHP

808

Why PHP?

PHP Syntax

PHP runs on different platforms (Windows, Linux, Unix,


etc.)
PHP is compatible with almost all servers used today
(Apache, IIS, etc.)
PHP is FREE to download from the official PHP resource:
www.php.net
PHP is easy to learn and runs efficiently on the server side

You cannot view the PHP source code by selecting "View


source" in the browser - you will only see the output from
the PHP file, which is plain HTML.
This is because the scripts are executed on the server
before the result is sent back to the browser.

BTE550 Internet and Intranet Applications

806

BTE550 Internet and Intranet Applications

Forms, JSP
P, PHP

Forms, JSP
P, PHP

Install an Apache server on a Windows or Linux machine


Install PHP on a Windows or Linux machine
Install MySQL on a Windows or Linux machine

BTE550 Internet and Intranet Applications

809

Basic PHP Syntax

11

Where to Start?

11

PHP is another HUGE language


It is a fully functional language
It has an incredible amount of built-in features
Form processing
Output / generate various types of data (not just text)
Database access
Allows for various DBs and DB formats
Object-oriented features
We will look at only a TINY part of PHP
BTE550 Internet and Intranet Applications

11

11

BTE550 Internet and Intranet Applications

Forms, JSP
P, PHP

11

PHP + MySQL

807

A PHP file normally contains HTML tags, just like an HTML file,
and some PHP scripting code.
A simple PHP script which sends the text "Hello World" to the
browser:

<html>
<body><?php echo "Hello World"; ?></body>
</html>
A PHP scripting block always starts with <?php and ends with ?>.
A PHP scripting block can be placed anywhere in the document.
Each code line in PHP must end with a semicolon. The semicolon is
a separator and is used to distinguish one set of instructions from
another.
There are two basic statements to output text with PHP: echo and
print. In the example above we have used the echo statement to
output the text "Hello World".
BTE550 Internet and Intranet Applications

810

135

Variables in PHP
PHP variables
11

Simple (scalar) types


boolean
TRUE or FALSE
integer
Platform dependent size of one machine word
32 bits on most machines
float
Double precision
We could call it a double, but since we don't declare
variables float works

Forms, JSP
P, PHP

Forms, JSP
P, PHP

11

Scalar Types

811

BTE550 Internet and Intranet Applications

BTE550 Internet and Intranet Applications

Scalar Types

11
Forms, JSP
P, PHP

11
Forms, JSP
P, PHP

We have single-quoted and double-quoted string literals


Double quoted allows for more escape sequences and
allows variables to be interpolated into the string
What does that mean?
Rather
R th than
th outputting
t tti the
th name off the
th variable,
i bl we
output its contents, even within a quote
We'll see an example once we define variables
Note that this is NOT done in Java
Length can be arbitrary
Grows as necessary
812

815

Variables in PHP

813

PHP programs have access to a large number of


predefined variables
11

Easy conversion back and forth between strings and


numbers
In Web applications these are mixed a lot, so
PHP will cast between the types when
appropriate
Can be indexed the preferred way is using curly
braces
$mystring = "hello";
echo $mystring{1};
Output here is 'e'

Forms, JSP
P, PHP

11
Forms, JSP
P, PHP

Below, the PHP script assigns the string "Hello World" to


a variable called $txt:
<html>
<body><?php
$txt="Hello World";
echo $txt;
?></body>
</html>

BTE550 Internet and Intranet Applications

Scalar Types

BTE550 Internet and Intranet Applications

814

Variables in PHP

string

BTE550 Internet and Intranet Applications

All PHP variables begin with the $


Variable names can begin with an underscore
Otherwise rules are similar to most other languages
Variables are dynamically typed
No type declarations
Variables are BOUND or UNBOUND
Unbound variables have the value NULL
Type information is obtained from the current bound
value

These variables allow the script access to server information,


form parameters, environment information, etc.
Ex:
$_SERVER
$ SERVER is an array
arra containing much
m ch information about
abo t
the server
$_POST is an array containing variables passed to a script via
HTTP POST
$_ENV is an array containing environment information

BTE550 Internet and Intranet Applications

816

136

PHP Operators Assignment Operators

817

BTE550 Internet and Intranet Applications

11

11
Forms, JSP
P, PHP

818

BTE550 Internet and Intranet Applications

Operator
==
!=
>
<
>=
<=

Result

x=2
x+2

Subtraction

x=2
5-x

Multiplication

x=4
x*5

20

Division

15/5
5/2

3
2.5

Modulus (division remainder) 5%2

10%2

1
0

++

Increment

x=5
x++

x=6

Decrement

x=5
x--

x=4

Operator
11

Example

Description
is equal to
is not equal
is greater than
is less than
is greater than or equal to
is less than or equal to

Example
5==8 returns false
5!=8 returns true
5>8 returns false
5<8 returns true
5>=8 returns false
5<=8 returns true

821

PHP Operators Logical Operators

Forms, JSP
P, PHP

11
Forms, JSP
P, PHP

Description
Addition

--

820

BTE550 Internet and Intranet Applications

PHP Operators Arithmetic Operators

BTE550 Internet and Intranet Applications

Is The Same As
x=y
x=x+y
xx=x-y
xy
x=x*y
x=x/y
x=x%y

PHP Operators Comparison Operators

In PHP, we use // to make a single-line comment or /* and


*/ to make a large comment block.
<html>
<body><?php//This is a comment/*
This is
a comment
block
*/?></body>
</html>

Operator
+

Example
x=y
x+=y
xx-=yy
x*=y
x/=y
x%=y

BTE550 Internet and Intranet Applications

Comments in PHP

Forms, JSP
P, PHP

Operator
=
+=
-=
*=
/=
%=

11

To concatenate two or more variables together, use the dot


(.) operator:
<html>
<body><?php
$txt1="Hello World";
$
$txt2="1234";
2 123
echo $txt1 . " " . $txt2 ;
?></body>
</html>
The output of the script above will be: "Hello World
1234".

Forms, JSP
P, PHP

Forms, JSP
P, PHP

11

Variables in PHP

&&

||

819

Description Example
x=6
and
y=3
(x < 10 && y > 1) returns true
x=6
or
y=3
(x==5 || y==5) returns false
x=6
not
y=3
!(x==y) returns true

BTE550 Internet and Intranet Applications

822

137

Conditional Statements

11

Similar to those in C++ / Java / Perl


Be careful with a few operators
/ in PHP is always floating point division
To get integer division, we must cast to int
$x = 15;
$y = 6;
echo ($x/$y), (int) ($x/$y), "<BR />";
Output is 2.5 2

Forms, JSP
P, PHP

Forms, JSP
P, PHP

11

PHP Operators

Very often when you write code, you want to perform


different actions for different decisions. You can use
conditional statements in your code to do this.
In PHP we have two conditional statements:
1. if (...else) statement - use this statement if you want
to execute a set of code when a condition is true (and
another if the condition is not true)
2. switch statement - use this statement if you want to
select one of many sets of lines to execute

Inequality operators do not compare strings


Will cast strings into numbers before comparing
BTE550 Internet and Intranet Applications

823

11

if Statement

Forms, JSP
P, PHP

Forms, JSP
P, PHP

11

PHP Operators
To compare strings, use the C-like string comparison function,
strcmp()
Even the == operator has odd behavior in PHP when operands are
mixed
Ex: Consider comparing a string and an int
Any non-numeric PHP string value will equal 0
Any numeric PHP string will equal the number it represents
Ex: Consider comparing a string and a boolean
Regular PHP string value will equal true
0 string will equal false
This behavior is consistent but confusing to the programmer and
is probably best avoided
BTE550 Internet and Intranet Applications

824

If you want to execute some code if a condition is true and


another code if a condition is false, use the if....else
statement.
Syntax
if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;

An additional equality operator and inequality operator


are defined
=== returns true only if the variables have the same
value and are of the same type
If casting occurred to compare, the result is false
!== returns true if the operands differ in value or in
type
Precedence and associativity are similar to C++/Java

825

11

Example

Forms, JSP
P, PHP

11
Forms, JSP
P, PHP

827

BTE550 Internet and Intranet Applications

PHP Operators

BTE550 Internet and Intranet Applications

826

BTE550 Internet and Intranet Applications

The following example will output "Have a nice weekend!" if the


current day is Friday, otherwise it will output "Have a nice day!":
<html>
<body><?php
$d=date("D");
if ($d=="Fri")
($d== Fri )
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?></body>
</html>
BTE550 Internet and Intranet Applications

828

138

832

BTE550 Internet and Intranet Applications

If you want to select one of many blocks of code to be executed, use


the Switch statement.
switch (expression) {
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed
if expression is different
from both label1 and label2;
}

The while statement will execute a block of code if and as


long as a condition is true.
Syntax
while (condition)
code to be executed;

Forms, JSP
P, PHP

11

while Statement

830

833

BTE550 Internet and Intranet Applications

Example

Example

<html>
This is how it works: First we have a single
<body><?php
expression (most often a variable), that is evaluated
switch ($x)
once. The value of the expression is then compared
{
with the values for each case in the structure.
case 1:
If there is a match, the block of code associated
echo "Number 1";
break;
break
with that case is executed.
case 2:
Use break to prevent the code from running into
echo "Number
Number 2";
2 ;
th nextt case automatically.
the
t
ti ll
break;
break
The default statement is used if none of the cases
case 3:
are
true.
echo "Number 3";
break;
break
default:
default
echo "No number between 1 and 3";
}
?></body>
</html>

The following example demonstrates a loop that will continue to run


as long as the variable i is less than, or equal to 5.
i will increase by 1 each time the loop runs:
<html>
<body><?php
$ii=1;
while($ii<=5) {
echo "The number is " . $ii . "<br />";
$i++;
}
?></body>
</html>

BTE550 Internet and Intranet Applications

831

11

11
Forms, JSP
P, PHP

1. while - loops through a block of code if and as long


as a specified condition is true
2 do...while
2.
d
hil - loops
l
through
th
h a block
bl k off code
d once, andd
then repeats the loop as long as a special condition is
true
3. for - loops through a block of code a specified
number of times
4. foreach - loops through a block of code for each
element in an array

switch Statement

BTE550 Internet and Intranet Applications

11

Forms, JSP
P, PHP

829

BTE550 Internet and Intranet Applications

Forms, JSP
P, PHP

11

Looping
Very often when you write code, you want the same block of code
to run a number of times. You can use looping statements in your
code to perform this.
In PHP we have the following looping statements:

Forms, JSP
P, PHP

11
Forms, JSP
P, PHP

if Statement
If more than one line should be executed when a condition
is true, the lines should be enclosed within curly braces:
<html>
<body><?php
$x=10;
if ($x==10)
{
echo "Hello<br />";
echo "Good morning<br />";
}
?></body>
</html>

BTE550 Internet and Intranet Applications

834

139

for Statement

11

The do...while statement will execute a block of code at


least once - it then will repeat the loop as long as a
condition is true.
Syntax
do
{
code to be executed;
}
while (condition);

Forms, JSP
P, PHP

Forms, JSP
P, PHP

11

do...while
do
while Statement

835

BTE550 Internet and Intranet Applications

The for statement has three parameters.


The first parameter is for initializing variables,
The second parameter holds the condition,
The third parameter contains any increments required to
p
the loop.
p
implement
If more than one variable is included in either the
initialization or the increment section, then they should be
separated by commas.
The condition must evaluate to true or false.

The following example will increment the value of i at least once,


and it will continue incrementing the variable i while it has a value
of less than 5:
<html>
<body><?php
$i=0;
do
{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<5);
?></body>
</html>

11

Example

Forms, JSP
P, PHP

Forms, JSP
P, PHP

11

Example

BTE550 Internet and Intranet Applications

836

839

foreach Statement

11

The for statement is used when you know how many


times you want to execute a statement or a list of
statements.
Syntax

Forms, JSP
P, PHP

11
Forms, JSP
P, PHP

The following example prints the text "Hello World!" five


times:
<html>
<body><?php
for ($i=1; $i<=5; $i++)
{
echo "Hello World!<br />";
}
?></body>
</html>
BTE550 Internet and Intranet Applications

for Statement

for (initialization; condition; increment)


{
code to be executed;
}

BTE550 Internet and Intranet Applications

838

BTE550 Internet and Intranet Applications

837

Loops over the array given by the parameter. On each


loop, the value of the current element is assigned to $value
and the array pointer is advanced by one - so on the next
loop, you'll be looking at the next element.
Syntax
foreach (array as value)
{
code to be executed;
}

BTE550 Internet and Intranet Applications

840

140

PHP Arrays

$b = $array;
$b[5] = 'Only in b';
No numerical index

foreach ($array as $elem)


{
echo "Element is $elem<br>";
}
?>

Accessing Arrays can be done in many ways


11

<?
$array[0] = 1;
$array[5] = "This is element 5";
$array[200] = 300;
$array['one'] = "One";
$b does not overwrite $array

Forms, JSP
P, PHP

Forms, JSP
P, PHP

11

PHP Arrays

Gives both the key and value at each iteration


foreach ($arrayvar as $value)
Gives just the next value at each iteration

841

BTE550 Internet and Intranet Applications

We can use direct access to obtain a desired item


Good if we are using the array as a hash table or if we need
direct access for some other reason
For sequential access, the foreach loop was designed to work
with arrays
Iterates through the items in two different ways
foreach ($arrayvar as $key => $value)

Arrays in PHP are quite versatile


We can use them as we use traditional arrays, indexing
on integer values
We can use them as hashes, associating a key with a
value in an arbitrary index of the array
In either case we access the data via subscripts
In the first case the subscript is the integer index
In the second case the subscript is the key value
We can even mix the two if we'd like

11

PHP Arrays

Forms, JSP
P, PHP

Forms, JSP
P, PHP

11

PHP Arrays

842

BTE550 Internet and Intranet Applications

Be careful in both cases data is iterated by the order it has


been generated, not by index order
Items accessed in the arrays using foreach are copies of the
data, not references to the data
So changing the loop control variable in the foreach loop
in PHP does NOT change
g the data in the original
g
arrayy
To do this we must change the value using indexing
A regular for loop can also be used, but due to the non-sequential
requirement for keys, this does not often give the best results

PHP Arrays

843

11

Explicitly using the array() construct


Implicitly by indexing a variable
Since PHP has dynamic typing, you cannot identify a variable
as an array except by assigning an actual array to it
If the variable is already set to a scalar,
scalar indexing will have
undesirable results indexes the string!
However, we can unset() it and then index it
We can test a variable to see if it is set (isset() and if it is
an array (is_array()) among other things
Size will increase dynamically as needed

Forms, JSP
P, PHP

11

PHP Arrays can be created in a number of ways

Forms, JSP
P, PHP

845

BTE550 Internet and Intranet Applications

PHP Arrays

BTE550 Internet and Intranet Applications

844

BTE550 Internet and Intranet Applications

The data in the array is not contiguous, so incrementing a


counter for the next access will not work correctly unless the
array index values are used in the "traditional" way
We can also use other iterators such as next and each to access the
array elements
gives us the next value with each call
next g
It moves to the next item, then returns it, so we must get the
first item with a separate call (ex: use current())
$curr = current($a1);
while ($curr):
echo "\$curr is $curr <BR />\n";
$curr = next($a1);
endwhile;

BTE550 Internet and Intranet Applications

846

141

PHP Control Structures

11

Again, these are similar to those in C++ / Java

Forms, JSP
P, PHP

Forms, JSP
P, PHP

11

PHP Arrays
each returns an array of two items:
A key field for the current key
A value field for the current value
It returns the next (key,value) pair, then moves, so the first
item is no longer a special case
while ($curr = each($a1)):
$k = $curr["key"];
$v = $curr["value"];
echo "key is $k and value is $v <BR />\n";
endwhile;

This function is preferable to next() if it is possible that


FALSE or an empty string or 0 could be in the array
The loop on the previous slide will stop for any of those
values
847

BTE550 Internet and Intranet Applications

11

11
Forms, JSP
P, PHP

Forms, JSP
P, PHP

848

849

851

<?
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

11
Forms, JSP
P, PHP

11

The following example demonstrates a loop that will print


the values of the given array:
<html>
<body><?php
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
echo "Value: " . $value . "<br />";
}
?></body>
</html>

<form method="post" action="sendmail.php">


Email: <input type="text" name="email" /><br />
Message:<br />
<textarea name="message" rows="15" cols="40">
</textarea><br />
<input type="submit" />
</form>

BTE550 Internet and Intranet Applications

Example

Forms, JSP
P, PHP

850

Form Validation

Both of these iteration functions operate similar to the Iterator


interface in Java
Iterate through the data in the collection without requiring us
to know how that data is actually organized
Allow multiple iterations to proceed concurrently on the same
underlying
y g collection
However, unlike in Java, if the array is changed during the
iteration process, the current iteration continues
Since new items are always added at the "end" of the
array adding a new item during an iteration does not
cause any data validity problems

BTE550 Internet and Intranet Applications

Open the block with


ith : rather than {
Close the block with endif, endwhile, endfor, endswitch
Advantage to this syntax is readability
Now instead of seeing a number of close braces, we see
different keywords to close different types of control
structures

BTE550 Internet and Intranet Applications

PHP Arrays

BTE550 Internet and Intranet Applications

if, while, do, for, switch are virtually identical to those in C++
and Java
PHP allows for an alternative syntax to designate a block in the
if, while, for and switch statements

if (!isset($_REQUEST['email'])) {
header( "Location: form1.html" );
}
elseif (empty($email) || empty($message)) {
?>
<html>
<head><title>Error</title></head>
<body>
<h1>Error</h1>
<p>
Oops, it appears you forgot to enter either your
email address or your message. Please press the BACK
button in your browser and try again.
</p>
</body>
</html>
BTE550 Internet and Intranet Applications

852

142

Forms, JSP
P, PHP

11

Form Validation
<?
}
else {
mail( "kurt@ce.itu.edu.tr", "Feedback Form Results",
$message "From:
$message,
From: $email
$email" );
header( "Location: form1.html" );
}
?>

BTE550 Internet and Intranet Applications

853

Preventing the Browser From Caching

Forms, JSP
P, PHP

11

[...preceding code...]
elseif (empty($email) || empty($message)) {
header( "Expires: Mon, 20 Dec 1998 01:00:00 GMT" );
header( "Last-Modified: " . gmdate("D, d M Y H:i:s") . "
GMT" );
header( "Cache-Control: no-cache, must-revalidate" );
header( "Pragma: no-cache" );
?>
BTE550 Internet and Intranet Applications

854

143

You might also like