Web Dev Final Book Page Num
Web Dev Final Book Page Num
Event-based (SAX)
Chapter 1: XML
XML Introduction
XML Documents
XML Examples
XML Parsers
1
own document markups. It is a method
1.XML for putting structured data into a text
file; these files are easy to read,
unambiguous, extensible, and platform-
independent.
XML stands for EXtensible Markup HTML uses tags and attributes; content
Language. It is a markup language and formatting can be placed together,
much like HTML and was designed to such as
carry data, not to display data. XML <p><font="Arial">text</font>, with
tags are not predefined; users must predetermined and rigid tags and
define their own tags. XML is designed attributes. In contrast, XML uses tags
to be self-descriptive and is a W3C and attributes where content and
Recommendation. format are separate; formatting is
contained in a stylesheet, allowing
users to specify what each tag and
What is XML? attribute means.
<article>
xml <author>Gerhard
Weikum</author>
<root>
<title>The Web in Ten Years</title>
<child>
<text>In order to evolve</text>
<subchild>.....</subchild>
</article>
</child>
3
XML Components There are no isolated markup
characters in the text (i.e., < > & ]]>).
There are four components for XML
content: the XML document, DTD If there is no DTD, all attributes are of
(Document Type Declaration), XML type CDATA by default.
Schema, and XSL (Extensible
A Valid XML Document
Stylesheet Language). The DTD,
schema, and XSL do not need to be A valid XML document has an
present in all cases. associated DTD and schema and
complies with the constraints in the
DTD and schema.
Types of XML Document
xml
A Well-formed XML Document <?xml version="1.0" encoding="UTF-
8"?>
Elements have an open and close tag
unless it is an empty element. <?xml version="1.0"
standalone="yes"?>
Attribute values are quoted.
XML Basics
If a tag is an empty element, it has a
closing / before the end of the tag. To specify a DTD for the document,
use:
Open and close tags are nested
correctly. xml
4
<!DOCTYPE …> Namespaces
There are two forms: Namespaces are not mandatory but are
useful in giving uniqueness to an
xml
element. They help avoid element
<!DOCTYPE root-element SYSTEM collision and are declared using the
"URIofDTD"> xmlns:name=value attribute; a URI is
recommended for the value.
<!DOCTYPE root-element PUBLIC Namespaces can be an attribute of any
"name" "URIofDTD"> element, and their scope is inside the
XML Basics element’s tags.
5
Dependence on a single, inflexible A valid document conforms to the
document type (HTML). regular-expression grammar.
The complexity of full SGML, whose The attribute types must be correct.
syntax allows many powerful but hard-
The constraints on references must be
to-program options.
satisfied.
6
<name>Homer Simpson</name> xml
<!-- Exactly one name -->
<?xml version="1.0"?>
<greet>Dr. H. Simpson</greet> <!-
<!DOCTYPE db [<!ELEMENT ...> …
- At most one greeting -->
]>
<addr>1234 Springwater
<db> ... </db>
Road</addr> <!-- As many address
lines as needed (in order) --> A DTD from the local file system:
<addr>Springfield USA, xml
98765</addr>
<!DOCTYPE db SYSTEM
<tel>(321) 786 2543</tel> <!-- "schema.dtd">
Mixed telephones and faxes -->
A DTD from a remote file system:
<fax>(321) 786 2544</fax>
xml
<tel>(321) 786 2544</tel>
<!DOCTYPE db SYSTEM
"http://www.schemaauthority.com/sche
<email>[email protected] ma.dtd">
</email> <!-- As many as needed -->
Specifying the Structure
</person>
name
Adding a DTD to the Document
greet?
A DTD can be:
name, greet?
Internal: The DTD is part of the
document file. addr*
7
email*: 0 or more email elements Inside the group, no regular
expressions are allowed—only element
Full Structure of a Person Entry
names.
The whole structure is specified by:
#PCDATA must be first, followed by 0
name, greet?, addr*, (tel | fax)*, email* or more element names, separated by |.
]>
8
<addressbook> The accuracy attribute is optional.
9
▪ XML Schema documents are used to Limitations of DTD
define and
• No constraints on character
validate the content and structure of
data
XML data.
• Not using XML syntax
▪ XML Schema was originally
proposed by Microsoft, • No support for namespace
but became an official W3C • Very limited for reusability and
recommendation in May
extensibility
2001
Advantages of Schema
Why Schema? (1)
• Syntax in XML Style
•Information
• Supporting Namespace and
•Structure
import/include
•Format
• More data types
•Traditional Document:
• Able to create complex data type by
•Everything is clumped together
inheritance
•Information
• Inheritance by extension or
•Structure restriction
•Format XML Schema
•“Fashionable” Document: A An XML Schema:
document
• defines elements that can appear in a
is broken into discrete parts, which document
can be treated separately • defines attributes that can appear
within elements
•Separating Information from
Structure and Format • defines which elements are child
elements
Why Schema? (2)
• defines the sequence in which the
•Schema Workflow
child elements can appear
DTD versus Schema
• defines the number of child elements
10
• defines whether an element is empty 13 Kinds of Schema Components
or can include text
• Simple type definitions
• defines default values for attributes
• Complex type definitions
The purpose of a Schema is to define
• Attribute declarations
the legal building blocks of an
• Element declarations
XML document, just like a DTD.
• Attribute group definitions
XML Schema – Better than DTDs
• Identity-constraint definitions
XML Schemas
• Model group definitions
➢ are easier to learn than DTD
• Notation declarations
➢ are extensible to future additions
◼ Annotations
➢ are richer and more useful than
DTDs ◼ Model groups
◼ Particles
➢ are written in XML
◼ Wildcards
➢ support data types
◼ Attribute Uses
XML Schema Components
•XML Schema
◼Abstract Data Model
•(.xsd)
◼ Simple and Complex Type
Definitions XML document & XML Schema
◼ Declarations •Information
11
•(.xml) • Example
• Simple Type Definition VS. Complex Each complex type definition is either
Type Definition – a restriction of a complex type
definition– an extension of a simple or
•Simple Type complex type definition– a restriction
of the ur-type definition.
•Definition
• Example
•Complex Type
•<xs:complexType
• Attributes & Elements
name="personName">
without element children
• <xs:sequence>
•Definition
• <xs:element name="title"
• Elements
minOccurs="0"/>
Simple Type Definition
•… …
• Simple Type Definition can be:– a
• </xs:sequence>
restriction of some other simple type;–
a list or union of simple type •</xs:complexType>
definition;or– a built-in primitive
datatypes.
12
•<xs:complexType <xs:complexType>
name="extendedName">
<xs:sequence>
• <xs:complexContent>
<xs:element name="title"
• <xs:extension base="personName"> type="xs:string"/>
<xs:element name="book">
13
• xs:include– Similar to a copy and • <xs:sequence>
paste
• <xs:element name="to"
of the included schema– The calling type="xs:string"/>
schema doesn't
• <xs:element name="from"
allow to override the type="xs:string"/>
14
• Object-based: DOM •What is XML Software Development
15
•Programming they can be displayed)
16
<library> ◼ <xsl:for-each select="//book">
loops through every
<book>
book element, everywhere in the
<title>XML</title>
document
<author>Gregory Brill</author>
◼ <xsl:value-of select="title"/>
</book> chooses the content of
</book> </xsl:for-each>
17
• Java and XML ◼ <?xml version="1.0"?>
18
</ul> <xsl:for-each select="//book">
</ul> </ul>
know how much data ▪ Notice that XSL can rearrange the
data; the HTML result can present
we will have
information in a different order than
XSL outline the XML
19
<author>Brett McLaughlin</author> </xsl:for-each>
</book> </ul>
</xsl:for-each>
xmlns:xsl="http://www.w3.org/1999/
</ul>
XSL/Transform">
</body>
<xsl:template match="/">
</html>
<html>
</xsl:template>
<head>
</xsl:stylesheet>
<title>Book Titles and
Authors</title> How to use it
</head> ◼ In a modern browser you can just
open the XML
<body>
file
<h2>Book titles:</h2>
◼ Older browsers will ignore the XSL
<ul>
and just show you
<xsl:for-each select="//book">
the XML contents as continuous text
<li>
◼ You can use a program such as
<xsl:value-of select="title"/> Xalan, MSXML,
20
◼ This can be done on the server side,
so that all the client
21
Index String Functions
strlen()
Chapter 2: PHP
str_replace()
Array Functions
strpos()
2.1 array_chunk
substr()
2.2 array_change_key_case
2.3 array_combine
Server-Side Basics
2.4 array_count_values
URL Structure and Web Servers
2.5 array_diff_assoc
PHP Basics (Syntax, Variables, and
2.6 array_diff_key Data Types)
2.8 array_intersect
PHP Functions
Defining Functions
Function Parameters
$_REQUEST Variable
22
array_change_key_case()
2. PHP • The array_change_key_case()
function changes
Output WER));
24
?> • <?php
array2 and returns the difference. This • Computes the difference of arrays
function is like
Description
array_diff
• array array_diff ( array $array1 ,
instead of the values. array $array2 [,
25
array1 that are not present in any of • <?php
the other
$array1 = array("a" => "green",
arrays. "red", "blue");
27
[2] => apple ) Array (
28
array by whatever type is in the second $preserve_keys = false ] )
array.
• Takes an input array and returns a
• <?php new array
29
• —Searches the array for a given • array_shift() shifts the first value of
value and the array
returns the corresponding key if off and returns it, shortening the array
successful by one
; Array (
30
int $length = NULL [, bool array_splice
$preserve_keys =
• —Remove a portion of the array and
false ]] ) replace
[3] => d )
31
array_splice($input, 1, count($input), print_r($queue);
"orange");
?>
// $input is now array("red", "orange")
• The above example will output:
array_unshift
• Array ( [0] => apple [1] =>
• —Prependone or more elements to raspberry [2] =>
the
orange [3] => banana )
beginning of an array
array_values
Description
• —Return all the values of an array
• int array_unshift ( array &$array ,
Description
mixed
• array array_values ( array $input )
mixed
• array_values() returns all the values
$... ] )
from the
$var [,
input array and indexes numerically
• array_unshift() prepends passed the array.
elements to the
• <?php
front of the array. Note that the list of
$array = array("size" => "XL",
elements
"color" => "gold"
is prepended as a whole, so that the
);
prepended
print_r(array_values($array));
elements stay in the same order. All
numerical ?>
array keys will be modified to start • The above example will output:
counting from
• Array ( [0] => XL [1] => gold )
zero while literal keys won't be
touched. arsort
32
• bool arsort ( array &$array [, int • This function sorts an array such that
$sort_flags = array
• <?php • <?php
$fruits = array("d" => "lemon", "a" $fruits = array("d" => "lemon", "a"
=> "orange", " => "orange", "
b" => "banana", "c" => "apple"); b" => "banana", "c" => "apple");
arsort($fruits); asort($fruits);
foreach ($fruits as $key => $val) { foreach ($fruits as $key => $val) {
} }
?> ?>
• The above example will output: • The above example will output:
asort count
33
COUNT_NORMAL ] ) $varname [, mixed
$a[1] = 3; • <?php
• Like array()
34
• bool sort ( array &$array [, int number
$sort_flags =
$start , mixed
SORT_REGULAR ] )
$step = 1 ] )
• This function sorts an array.
$limit [,
Elements will be
• Create an array containing a range of
arranged from lowest to highest when
this elements.
function has completed. • <?php
• <?php // array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12)
$fruits = array("lemon", "orange",
"banana", "apple"); foreach (range(0, 12) as $number) {
sort($fruits); }
foreach ($fruits as $key => $val) { echo $number;
} // The step parameter was introduced
in 5.0.0
echo "fruits[" . $key . "] = " . $val .
"\n"; // array(0, 10, 20, 30, 40, 50, 60, 70,
80, 90, 100)
?>
foreach (range(0, 100, 10) as
• The above example will output:
$number) {
• fruits[0] = apple fruits[1] = banana
}
fruits[2] = lemon
echo $number;
fruits[3] = orange
// Use of character sequences
range
introduced in 4.1.0
• —Create an array containing a
// array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
range of
'i');
elements
foreach (range('a', 'i') as $letter) {
• Description
}
• array range ( mixed
echo $letter;
35
// array('c', 'b', 'a'); • Note: If the search parameter is a
string and the type parameter is
foreach (range('c', 'a') as $letter) {
set to TRUE, the search is case-
}
sensitive.
echo $letter;
• Syntax
?>
• in_array(search,array,type)
shuffle
•
• —Shuffle an array
Parameter Description
• Description
• search-Specifies the what to search
• bool shuffle ( array &$array ) for
36
?>
html
Copy code
echo "Hello";
A Function That Does Not Return a
Value }
</body>
php
</html>
Copy code
A Function That Returns a Value
function function_name()
When you want to get the value
{ generated from a function to be used
// statements elsewhere in your code, you need to
define a function that can return a
} value. The return keyword is used to
return the value of the function to
Note: The name of the function must
another function or code block that
start with a letter or an underscore; it
calls it.
cannot start with a number.
Syntax:
Example:
37
</body>
php </html>
return value;
} Example:
Example:
html
<html> <body>
<body> <?php
function sum() {
$s = 10 + 20; }
} printName("Khorn Channa");
38
There are two ways the browser client http://www.test.com/index.htm?name1
can send information to the web server: =value1&name2=value2
<?php
arduino
if ($_GET["name"] || $_GET["age"]) {
Copy code
echo "Welcome " . $_GET['name'] .
"<br />";
39
echo "You are " . $_GET['age'] . " The POST method can be used to send
years old."; ASCII as well as binary data.
if (preg_match("/[^A-Za-z'-]/",
$_POST['name'])) {
The POST Method
die("Invalid name; name should
The POST method transfers be alphabetical.");
information via HTTP headers. The
information is encoded as described in }
the case of the GET method and put echo "Welcome " . $_POST['name'] .
into a header called QUERY_STRING. "<br />";
40
} php
?> <?php
<html> if ($_REQUEST["name"] ||
$_REQUEST["age"]) {
<body>
echo "Welcome " .
<form action="<?php $_PHP_SELF
$_REQUEST['name'] . "<br />";
?>" method="POST">
echo "You are " .
Name: <input type="text"
$_REQUEST['age'] . " years old.";
name="name" />
exit();
Age: <input type="text"
name="age" /> }
</form> <html>
</body> <body>
41
PHP Form Introduction $gender =
test_input($_POST["gender"]);
Example
}
Below is an example that shows the
form with some specific actions using
the POST method.
function test_input($data) {
Html
$data = trim($data);
$data = stripslashes($data);
<html>
$data =
<head> htmlspecialchars($data);
</head> }
<body> ?>
<?php
<table>
if
<tr>
($_SERVER["REQUEST_METHOD"]
== "POST") { <td>Name:</td>
$name = <td><input type="text"
test_input($_POST["name"]); name="name"></td>
$email = </tr>
test_input($_POST["email"]);
<tr>
$website =
test_input($_POST["website"]); <td>E-mail:</td>
42
</tr> </tr>
<tr> </table>
<td><input type="text"
name="website"></td>
<?php
</tr>
echo "<h2>Your Given Details
<tr> Are:</h2>";
</td> </html>
</tr>
<tr>
<td>
43
PHP provides a wide range of built-in echo $newText; // Outputs: 'I love
string functions. coding'
44
php Description: Converts the first
character of a string to lowercase.
Copy code
Example:
$text = 'Hello World';
php
echo strtoupper($text); // Outputs:
'HELLO WORLD' Copy code
45
Description: Splits a string into an $text = ' Hello World ';
array by a delimiter.
echo trim($text); // Outputs: 'Hello
Example: World'
46
php Definition
47
Provides different content depending Syntax: $variable_name = value;
on the context.
Example:
Interfaces with services such as
php
databases and email.
Copy code
Authenticates users and processes form
information. $user_name = "ram80";
PHP code can be embedded within $age = 16;
HTML code.
$drinking_age = $age + 5;
Lifecycle of a PHP Web Request
$this_class_rocks = TRUE;
User's computer requests a .php file.
Variable names are case-sensitive,
The server processes the request and always begin with $, and are implicitly
returns the output. declared by assignment.
Basic PHP Syntax Data Types
PHP Syntax Template Basic types include: int, float, boolean,
string, array, object, NULL.
Code between <?php and ?> is
executed as PHP. PHP automatically converts between
types in many cases.
Everything outside these tags is output
as pure HTML.
48
Comments Object Data Type
49
$length = strlen($name); // 15 // statements;
Control Structures do {
50
Copy code $name[] = value; # Append value
$a = 3; Example:
$b = 4; php
51
Arrays in PHP replace many other php
collections in Java (list, stack, queue,
Copy code
set, map).
$fellowship = array("Frodo", "Sam",
php
"Gandalf", "Strider", "Gimli",
Copy code "Legolas", "Boromir");
} print "$fellow\n";
$morgan = array_shift($tas); }
array_reverse($tas); php
} );
Example:
for ($row = 0; $row < 3; $row++) {
52
echo "<p> | " . print strpos($test, "o", 5); // Output:
$AmazonProducts[$row]["Code"] . " | position of "o" after index 5
".
Regular Expressions
Patterns in Text:
$AmazonProducts[$row]["Description
"] . " | " . PHP supports POSIX and Perl regular
expressions.
$AmazonProducts[$row]["Price"]
. " </p>"; Examples:
} regex
String Comparison Functions Copy code
Common Functions: [a-z]at # Matches: cat, rat, bat…
strcmp: Compare strings [aeiou] # Matches vowels
strstr, strchr: Find string/char within a [a-zA-Z] # Matches all letters
string
[^a-z] # Not a-z
strpos: Find numerical position of
string [[:alnum:]]+ # At least one
alphanumeric character
str_replace, substr_replace: Replace
strings (very)*large # Matches: large, very
large, very very large…
Example:
(very){1,3} # Counting "very" up to 3
php
^bob # Matches "bob" at the
Copy code beginning
$offensive = array("offensive word1", com$ # Matches "com" at the end
"offensive word2");
Printing HTML Tags in PHP
$feedback = str_replace($offensive,
"%!@*", $feedback); Best practice is to minimize print/echo
statements.
53
php Unclosed Braces: Results in an error
about 'unexpected $end'.
Copy code
Missing '=' Sign: In <?=, the
print "<!DOCTYPE html PUBLIC \"-
expression does not produce any
//W3C//DTD XHTML 1.1//EN\"\n";
output.
print "
Example:
\"http://www.w3.org/TR/xhtml11/DTD/
xhtml11.dtd\">\n"; php
Example: <body>
php <?php
54
<?php for ($i = 1; $i < strlen($str);
$i++) {
}
print $separator . $str[$i];
?>
}
</body>
}
Functions
}
Parameter types and return types are
not explicitly defined.
Copy code
Example:
php
Copy code
function print_separated($str,
$separator = ", ") {
if (strlen($str) > 0) {
print $str[0];
55