CS8651 Notes 005-4 Edubuzz360
CS8651 Notes 005-4 Edubuzz360
com
PHP: An introduction to PHP – Using PHP – Variables – Program control – Built-in functions –
Form Validation – Regular Expressions – File handling – Cookies – Connecting to Database; XML:
Basic XML – Document Type Definition – XML Schema DOM and Presenting XML, XML Parsers
and Validation, XSL and XSLT Transformation, News Feed (RSS and ATOM).
Internet Programming – UNIT-IV
Q.No Questions
Define PHP. List the features.
What is PHP?
PHP is an acronym for "PHP: Hypertext Preprocessor"
PHP is a widely-used, open source scripting language
PHP scripts are executed on the server
PHP is free to download and use
With PHP you are not limited to output HTML. You can output images, PDF
files, and even Flash movies. You can also output any text, such as XHTML
and XML.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
PHP Variables
A variable can have a short name (like x and y) or a more descriptive
name (age, carname, total_volume).
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
22.
23. }
24.
25.
26. ?>
In the above code, we are checking each possible browser that may be and
return the browser name. Here we haven’t checked the Mozilla because of
most of the browser using this as the user agent string.
Below is how to display the browser name on our web page:
Echo get_the_browser();
PHP Reference
The PHP reference contains different categories of all PHP functions and
constants, along with examples.
4.
system Filter FTP Libxml Mail Math
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION
Which super global array in PHP would contain a HTML form’s POST data?
Super global variables are built-in variables that are always available in
all scopes.
PHP $_POST
PHP $_POST is a PHP super global variable which is used to collect form
data after submitting an HTML form with method="post". $_POST is also
widely used to pass variables.
The example below shows a form with an input field and a submit button.
When a user submits the data by clicking on "Submit", the form data is
sent to the file specified in the action attribute of the <form> tag. In this
example, we point to the file itself for processing form data. If you wish to
use another PHP file to process form data, replace that with the filename
of your choice. Then, we can use the super global variable $_POST to
collect the value of the input field:
Example
<html>
<body>
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_POST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
echo and print are more or less the same. They are both used to output data to the screen.
Example-1 Example-1
<?php <?php
echo "<h2>PHP is Fun!</h2>"; print "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>"; print "Hello world!<br>";
echo "I'm about to learn PHP!<br>"; print "I'm about to learn PHP!";
echo "This ", "string ", "was ", "made ", "with ?>
multiple parameters.";
?>
PHP is Fun! PHP is Fun!
Hello world! Hello world!
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Example-2 Example-2
<?php <?php
$txt1 = "Learn PHP"; $txt1 = "Learn PHP";
$txt2 = "W3Schools.com"; $txt2 = "W3Schools.com";
$x = 5; $x = 5;
$y = 4; $y = 4;
Give the difference between DTD and XML schema for defining XML
document structure with appropriate examples.
DTD vs XSD
There are many differences between DTD (Document Type Definition) and XSD (XML
Schema Definition). In short, DTD provides less control on XML structure whereas
XSD (XML schema) provides more control.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Query string
The information can be sent across the web pages. This information is
called query string. This query string can be passed from one page to
another by appending it to the address of the page. You can pass more
than one query string by inserting the & sign between the query strings.
A query string can contain two things: the query string ID and its value.
The query string passed across the web pages is stored in
$_REQUEST, $_GET, or $_POST variable.
9.
Query string handling in PHP
Query strings
To access the data in a query string you can use the $_GET global array. Each element in
this array has a key which is the name of the query string variable and a value which is the
value of that variable.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
This link loads the page mypage.php with two variables variable1 and variable2 with
values value1 and value2 respectively.
echo $_GET['variable1'];
echo $_GET['variable2'];
// outputs:
//value1
//value2
Form data
The get method of forms sends the data to a page via a query string.
This form passes the value of the two text boxes to the page myform.php.
print_r($_GET);
// outputs:
// Array (
// [textbox] => value1
// [textbox2] => value2
// [submitbutton] => Submit
// )
echo $_GET['textbox'];
//outputs: value1
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Namespace Declaration
A Namespace is declared using reserved attributes. Such an attribute name must
either be xmlns or begin with xmlns: shown as below −
<element xmlns:name = "URL">
Syntax
The Namespace starts with the keyword xmlns.
The word name is the Namespace prefix.
The URL is the Namespace identifier.
Example
Namespace affects only a limited area in the document. An element containing the
declaration and all of its descendants are in the scope of the Namespace.
Following is a simple example of XML Namespace −
<?xml version = "1.0" encoding = "UTF-8"?>
<cont:contact xmlns:cont = "www.tutorialspoint.com/profile">
<cont:name>Tanmay Patil</cont:name>
<cont:company>TutorialsPoint</cont:company>
<cont:phone>(011) 123-4567</cont:phone>
</cont:contact>
Here, the Namespace prefix is cont, and the Namespace identifier (URI)
as www.tutorialspoint.com/profile. This means, the element names and attribute
names with the cont prefix (including the contact element), all belong to
the www.tutorialspoint.com/profile namespace.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
<LastName>Patil</LastName>
<ContactNo>1234567890</ContactNo>
<Email>[email protected]</Email>
<Address>
<City>Bangalore</City>
<State>Karnataka</State>
<Zip>560212</Zip>
</Address>
</Employee>
</Company>
Following tree structure represents the above XML document −
In the above diagram, there is a root element named as <company>. Inside that, there is
one more element <Employee>. Inside the employee element, there are five branches
named <FirstName>, <LastName>, <ContactNo>, <Email>, and <Address>. Inside the
<Address> element, there are three sub-branches, named <City> <State> and <Zip>.
What is XSLT
XSLT, Extensible Stylesheet Language Transformations, provides the ability to
transform XML data from one format to another automatically.
12.
How XSLT Works
An XSLT stylesheet is used to define the transformation rules to be applied on the
target XML document. XSLT stylesheet is written in XML format. XSLT Processor
takes the XSLT stylesheet and applies the transformation rules on the target XML
document and then it generates a formatted document in the form of XML, HTML,
or text format. This formatted document is then utilized by XSLT formatter to
generate the actual output which is to be displayed to the end-user.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Advantages
Here are the advantages of using XSLT −
Independent of programming. Transformations are written in a separate xsl file which
is again an XML document.
Output can be altered by simply modifying the transformations in xsl file. No need to
change any code. So Web designers can edit the stylesheet and can see the change
in the output quickly.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
</xs:complexType>
</xs:element>
14. 1. people_list is a valid element name, and an instance of such an element contains
any number of person elements. The * denotes there can be 0 or
more person elements within the people_list element.
2. person is a valid element name, and an instance of such an element contains one
element named name , followed by one named birthdate (optional),
then gender (also optional) and socialsecuritynumber (also optional).
The ? indicates that an element is optional. The reference to the name element
name has no ? , so a person element must contain a name element.
3. name is a valid element name, and an instance of such an element contains
"parsed character data" (#PCDATA).
4. birthdate is a valid element name, and an instance of such an element contains
parsed character data.
5. gender is a valid element name, and an instance of such an element contains
parsed character data.
6. socialsecuritynumber is a valid element name, and an instance of such an element
contains parsed character data.
An example of an XML file that uses and conforms to this DTD follows. The DTD is
referenced here as an external subset, via the SYSTEM specifier and a URI. It assumes
that we can identify the DTD with the relative URI reference "example.dtd"; the
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
"people_list" after "!DOCTYPE" tells us that the root tags, or the first element defined in
the DTD, is called "people_list":
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
</books>
In the above example, Books.xml is linked with Rule.css which contains the
corresponding style sheet rules.
CSS FILE :
books {
color: white;
background-color : gray;
width: 100%;
}
heading {
color: green;
font-size : 40px;
background-color : powderblue;
}
heading, title, author, publisher, edition, price {
display : block;
}
title {
font-size : 25px;
font-weight : bold;
}
Output :
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
XML namespaces are used for providing uniquely named elements and attributes in
an XML document. They are defined in a W3C recommendation.[1][2] An XML instance may
contain element or attribute names from more than one XML vocabulary. If each vocabulary
is given a namespace, the ambiguity between identically named elements or attributes can be
resolved.
You can easily avoid the XML namespace by using a name prefix.
1. <h:table>
2. <h:tr>
3. <h:td>Aries</h:td>
4. <h:td>Bingo</h:td>
5. </h:tr>
6. </h:table>
7. <f:table>
8. <f:name>Computer table</f:name>
9. <f:width>80</f:width>
10. <f:length>120</f:length>
11. </f:table>
Note: In this example, you will get no conflict because both the tables have
specific names.
You can use xmlns attribute to define namespace with the following syntax:
1. <root>
2. <h:table xmlns:h="http://www.abc.com/TR/html4/">
3. <h:tr>
4. <h:td>Aries</h:td>
5. <h:td>Bingo</h:td>
6. </h:tr>
7. </h:table>
8.
9. <f:table xmlns:f="http://www.xyz.com/furniture">
10. <f:name>Computer table</f:name>
11. <f:width>80</f:width>
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
12. <f:length>120</f:length>
13. </f:table>
14. </root>
In the above example, the <table> element defines a namespace and when a
namespace is defined for an element, the child elements with the same prefixes are
associated with the same namespace.
<author>
<name>...</name>
</author>
<id>...</id>
<entry>
<title>...</title>
<link>...</link>
<id>...</id>
<updated>...</updated>
<summary>...</summary>
</entry>
</feed>
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
An Atom 1.0 Feed Document will be constructed of the following two elements:
<feed> Elements
<entry> Elements
RSS - Advantages
RSS is taking off so quickly because people are liking it. RSS is easy to use and
it has advantages for a publisher as well as for a subscriber. Here we have listed
out a few advantages of RSS for subscribers as well as for publishers.
18. Get the news you want: RSS Feed comes in the form of headlines and a brief
description so that you can easily scan the headlines and click only those stories that
interest you.
Freedom from e-mail overload: You are not going to get any email for any news or
blog update. You just go to your reader and you will find updated news or blog
automatically whenever there is a change on the RSS server.
Easy republishing: You may be both a subscriber and a publisher. For example, you
may have a web-site that collects news from various other sites and then republishes
it. RSS allows you to easily capture that news and display it on your site.
RSS publishers are the people who publish their content through RSS feed. We
would suggest you to use RSS:
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Easier publishing: RSS is really simple publishing. You don't have to maintain a
database of subscribers to send your information to them, instead they will access
your Feed using a reader and will get updated content automatically.
A simpler writing process: If you have a new content on your web site, you only need
to write an RSS Feed in the form of titles and short descriptions, and link back to your
site.
An improved relationship with your subscribers: Because people subscribe from
their side, they don't feel as if you are pushing your content on them.
The assurance of reaching your subscribers: RSS is not subject to spam filters,
your subscribers get the Feeds, which they subscribe to and nothing more.
Links back to your site: RSS Feeds always include links back to a website. It directs
a lot of traffic towards your website.
Relevance and timeliness: Your subscribers always have the latest information from
your site.
How would you prepare the steps to get the RSS file on web?
Here are the simple steps to put your RSS Feed on the web.
First decide which version of RSS Feed you are going to use for your site. We would
recommend you to use the latest version available.
Create your RSS Feed in a text file with extension either .xml or .rdf. Upload this file
on your web server.
You should validate your RSS Feed before making it live. Check the next chapter on
RSS Feed Validation.
Create a link on your Web Pages for the RSS Feed file. You will use a small yellow
button for the link that says either or .
20. Now, your RSS Feed is online and people can start using it. But there are ways to
promote your RSS Feed so that more number of people can use your RSS Feed.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
o Google - http://www.google.com/webmasters/add.html
o Bing - http://www.bing.com/toolbox/submit-site-url
As we have explained earlier, RSS Feed makes sense for the site which are
changing their content very frequently, for example, any news or blogging sites.
So now, you have got RSS Feed buttons from Google, Yahoo, and MSN. You
must make sure to update your content frequently and that your RSS Feed is
constantly available.
PART-B
(i) Describe about the introduction and installation of PHP.
Introduction to PHP
PHP is one of the most widely used server side scripting language for web
development. Popular websites like Facebook, Yahoo, Wikipedia etc are
developed using PHP.
PHP is so popular because it's very simple to learn, code and deploy on server,
hence it has been the first choice for beginners since decades.
Uses of PHP
To further fortify your trust in PHP, here are a few applications of this amazing
scripting language:
2. Common Line Scripting. You can write PHP scripts to perform different
operations on any machine, all you need is a PHP parser for this.
website, using Facebook's PHP SDK. Check this link for more information.
5. Wordpress is one of the most used blogging(CMS) platform in the World, and if
you know PHP, you can try a hand in Wordpress plugin development.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Manual Installation
1. Step 1: Download the files. Download the latest PHP 5 ZIP package from
www.php.net/downloads.php. ...
2. Step 2: Extract the files. ...
3. Step 3: Configure php. ...
4. Step 4: Add C:\php to the path environment variable. ...
5. Step 5: Configure PHP as an Apache module. ...
6. Step 6: Test a PHP file.
<link
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"
rel="stylesheet">
</head>
<body>
<?php
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
}
if($_POST['operation'] == 'divided by')
{
$total = $_POST['number1'] / $_POST['number2'];
}
} else {
}
}
// end PHP. Code by webdevtrick.com
?>
</div>
</body>
</html>
?>
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Like any other languages, PHP is built out of a series of control statements. The control statement can
be an assignment, a function call, a loop, a conditional statement or even a statement that does nothing
or an empty statement.
In PHP we have the following conditional statements:
if statement – We use this control statement to execute some code only if a specified condition is true.
if…else statement – We use this control statement to execute some code if a condition is true and
another code if the condition is false.
if…elseif….else statement – We use this control statement to select one of several blocks of code to
be executed
switch statement – We use this control statement to select one of many blocks of code to be executed
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Use the if….elseif…else statement to select you may want to compare the same variable (or
one of several blocks of code to be executed. expression) with many different values, and
execute a different piece of code depending on
if (condition) which value it equals to. This is exactly what
code to be executed if condition is true; the switch statement is for.
elseif (condition) switch ( )
code to be executed if condition is true; {
else case condition1
code to be executed if condition is false; break;
case condition2
Note: Note that elseif and else if will only be
considered exactly the same when using break;
curly brackets as in the above example. }
When using a colon to define your if/elseif
conditions, you must not separate else if into For example, the following code would display
two words, or PHP will fail with a parse $i matched value as 0 or 1 or 2:
error.
<?php
switch ($i) {
case 0:
echo "i equals 0";
case 1:
echo "i equals 1";
case 2:
echo "i equals 2";
}
?>
(i) Create an XML document that marks up various sports and their
descriptions. Use XSLT to tabulate neatly the elements and attributes
of the document.
<?xml version="1.0"?>
3.
-<event id="866683" status="Finished" round="8001 - 1/1 (Final)" date="2011-03-20
13:15:00" name="10 km Freestyle Handicap Pursuit">
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
</results>
</results>
</event></events>
(ii) Illustrate a JSP page that enables the user to input the first name
and in response outputs the last name.
<body>
<center>
<h1>Using POST Method to Read Form Data</h1>
<ul>
<li><p><b>First Name:</b>
<%= request.getParameter("first_name")%>
</p></li>
<li><p><b>Last Name:</b>
<%= request.getParameter("last_name")%>
</p></li>
</ul>
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
</body>
</html>
Following is the content of the Hello.htm file −
<html>
<body>
</body>
</html>
Let us now keep main.jsp and hello.htm in <Tomcat-
installationdirectory>/webapps/ROOT directory. When you
access http://localhost:8080/Hello.htm, you will receive the following output.
First Name:
Last Name:
Try to enter the First and the Last Name and then click the submit button to see the result
on your local machine where tomcat is running.
(i) Write a PHP program that tests whether an email address is input
correctly. Test your program with both valid and invalid email
addresses.
5.
PHP - Validate Name, E-mail, and URL
Example
<?php
// define variables and set to empty values
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
// check if URL address syntax is valid (this regular expression
also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-
9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
}
?>
OUTPUT
Name: *
E-mail: *
Website:
Comment:
Submit
Your Input:
Anand M
[email protected]
www.ibm.com
Yhis is a comment
male
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Cookies in PHP
Cookies are used to store the information of a web page in a remote browser, so that when
the same user comes back to that page, that information can be retrieved from the browser
itself.
Uses of cookie
7.
Cookies are often used to perform following tasks:
Session management: Cookies are widely used to manage user sessions. For
example, when you use an online shopping cart, you keep adding items in the cart
and finally when you checkout, all of those items are added to the list of items you
have purchased. This can be achieved using cookies.
User identification: Once a user visits a webpage, using cookies, that user can be
remembered. And later on, depending upon the search/visit pattern of the user,
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
content which the user likely to be visited are served. A good example of this is
'Retargetting'. A concept used in online marketing, where depending upon the
user's choice of content, advertisements of the relevant product, which the user
may buy, are served.
Tracking / Analytics: Cookies are used to track the user. Which, in turn, is used
to analyze and serve various kind of data of great value, like location, technologies
(e.g. browser, OS) form where the user visited, how long (s)he stayed on various
pages etc.
PHP has a setcookie() function to send a cookie. We will discuss this function in detail
now.
Example:
<?php
if (isset($_COOKIE['cookie']))
echo $_COOKIE["w3resource"];
?>
Summarize in detail the XML schema, built in and user defined data
types.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
XSD Example
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The text can be of many different types like boolean, string, date, etc.), or
it can be a custom type that you can define yourself.
You can also add restrictions (facets) to a data type in order to limit its
content, or you can require the data to match a specific pattern.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
where xxx is the name of the element and yyy is the data type of the
element.
XML Schema has a lot of built-in data types. The most common types are:
xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time
Example
simple element definitions:
<xs:element name="lastname" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="dateborn" type="xs:date"/>
<lastname>Ronald</lastname>
<age>36</age>
<dateborn>1970-03-27</dateborn>
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
2. The "employee" element can have a type attribute that refers to the
name of the complex type to use:
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
If you use the method described above, several elements can refer to the
same complex type, like this:
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
XML DTD
An XML document with correct syntax is called "Well Formed".
A DTD defines the structure and the legal elements and attributes of an
XML document.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
The purpose of a DTD is to define the structure and the legal elements and
attributes of an XML document:
Note.dtd:
<!DOCTYPE note
[
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
How do you infer the significant differences between DID and XML
schema for defining XML document structures with appropriate examples.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Summary:
XML Schema data types can be generally categorized a "simple type" (including
embedded simple type) and "complex type." The "embedded simple type" is already
defined, but can be used to create a new type through restriction or extension.
A simple type is a type that only contains text data. This type can be used with element
declarations and attribute declarations. On the other hand, a complex data type is a type
that has a child element or attribute structure.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Here, the section described together with "xs:string" is an embedded simple type
according to XML Schema. In this example, we have established the definition that the
data type for the element called "Department" is a text string.
<xs:complexType name="EmployeeType">
<xs:sequence maxOccurs="unbounded">
<xs:element ref="Name" />
<xs:element ref="Department" />
</xs:sequence>
</xs:complexType>
<xs:element name="Name" type="xs:string" />
<xs:element name="Department" type="xs:string" />
<person gender="female">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
<person>
<gender>female</gender>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
There are no rules about when to use attributes or when to use elements
in XML.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
XML Parsers
An XML parser is a software library or package that provides interfaces for client
applications to work with an XML document. The XML Parser is designed to read the
XML and create a way for programs to use XML.
XML parser validates the document and check that the document is well formatted.
Let's understand the working of XML parser by the figure given below:
1. DOM
2. SAX
A DOM document is an object which contains all the information of an XML document.
It is composed like a tree structure. The DOM Parser implements a DOM API. This API is
very simple to use.
A DOM Parser creates an internal structure in memory which is a DOM document object
and the client applications get information of the original XML document by invoking
methods on this document object.
Advantages
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
1) It supports both read and write operations and the API is very simple to use.
Disadvantages
1) It is memory inefficient. (consumes more memory because the whole XML document
needs to loaded into memory).
A SAX Parser implements SAX API. This API is an event based API and less intuitive.
Clients does not know what methods to call, they just overrides the methods of the API
and place his own code inside method.
Advantages
Disadvantages
2) Clients never know the full information because the data is broken into pieces.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
RSS vs ATOM
Really Simple Syndication or RSS has been the
standard for web feeds for a considerable time.
Web feeds contains either a summary or the full
text content of a web page.
The problem with RSS is the often confusing and
non standard conventions used by RSS due in part
to its scattered development.
The advent of the ATOM syndication standard
was a response to the design flaws of the RSS
standard.
The primary advantage of the ATOM is its
adaptation as the IETF standard.
Being an IETF standard, each atom feed contains an
explicit declaration of the format of the content
along with what language is used.
RSS feeds do not declare its content, but since it only
contains plain text or escaped HTML, it is rather
easy for the browser to distinguish which is which.
A major flaw of RSS is in its code. RSS code isn’t really very
usable in other XML vocabularies since it wasn’t really
intended to do so at the very beginning. ATOM code has been
built from the ground with modularity in mind. Therefore, a
great majority of its code is reusable even with other XML
vocabularies like RSS.
Summary:
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
XSLT <xsl:element>
Definition and Usage
The <xsl:element> element is used to create an element node in the output document.
Syntax
<xsl:element
name="name"
namespace="URI"
use-attribute-sets="namelist">
</xsl:element>
Attributes
Example 1
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Create a "singer" element that contains the value of each artist element:
<xsl:template match="/">
<xsl:for-each select="catalog/cd">
<xsl:element name="singer">
<xsl:value-of select="artist" />
</xsl:element>
<br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
<xsl:element name="singer">
<xsl:value-of select="artist"/>
</xsl:element>
<br/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
RESULT
My CD Collection
Bob Dylan
Bonnie Tyler
Dolly Parton
Gary Moore
XSLT - Transformation
What is XSLT?
XSL Transformations (XSLT 2.0) is a language for transforming XML documents into other
XML documents, text documents or HTML documents. You might want to format a chapter of
a book using XSL-FO, or you might want to take a database query and format it as HTML.
14.
Wildly Popular
XSLT has become the language of choice for a very wide range of XML applications. It is of
course still used to produce XSL-FO documents for printing, but it is also used to integrate
back-end software for Web sites. We can find XSLT inside most modern Web browsers, so
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
that XML can be transformed on the fly without the user even noticing; you will find XSLT on
the desktop, in servers, in network appliances.
Then you create an XSL Style Sheet ("cdcatalog.xsl") with a transformation template:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
3) Link the XSL Style Sheet to the XML Document
Add the XSL style sheet reference to your XML document ("cdcatalog.xml"):
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
.</catalog>
If you have an XSLT compliant browser it will nicely transform your XML into
XHTML.
Sample OUTPUT
My CD Collection
Title Artist
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
XML parser validates the document and check that the document is well formatted.
Let's understand the working of XML parser by the figure given below:
3. DOM
4. SAX
A DOM document is an object which contains all the information of an XML document.
It is composed like a tree structure. The DOM Parser implements a DOM API. This API is
very simple to use.
A DOM Parser creates an internal structure in memory which is a DOM document object
and the client applications get information of the original XML document by invoking
methods on this document object.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Advantages
1) It supports both read and write operations and the API is very simple to use.
Disadvantages
1) It is memory inefficient. (consumes more memory because the whole XML document
needs to loaded into memory).
A SAX Parser implements SAX API. This API is an event based API and less intuitive.
Clients does not know what methods to call, they just overrides the methods of the API
and place his own code inside method.
Advantages
Disadvantages
2) Clients never know the full information because the data is broken into pieces.
PART-C
Explain how you shall carry out String Manipulations using a PHP
1.
Program.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Example
Run this code »
<?php
$my_str = 'Welcome to Tutorial Republic';
// Outputs: 28
echo strlen($my_str);
?>
Example
Run this code »
<?php
$my_str = 'The quick brown fox jumps over the lazy dog.';
// Outputs: 9
echo str_word_count($my_str);
?>
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Example
Run this code »
<?php
$my_str = 'If the facts do not fit the theory, change the facts.';
Example
Run this code »
<?php
$my_str = 'If the facts do not fit the theory, change the facts.';
Reversing a String
The strrev() function reverses a string.
Example
Run this code »
<?php
$my_str = 'You can do anything, but not everything.';
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Design a PHP application for College Management System with appropriate built-in
functions and database.
2.
<?php
// the message
$msg = "First line of text\nSecond line of text";
// send email
mail("[email protected]","My subject",$msg);
?>
3.
Syntax
mail(to,subject,message,headers,parameters);
Parameter Values
Parameter Description
subject Required. Specifies the subject of the email. Note: This parameter
cannot contain any newline characters
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
headers Optional. Specifies additional headers, like From, Cc, and Bcc. The
additional headers should be separated with a CRLF (\r\n).
Note: When sending an email, it must contain a From header. This can be
set with this parameter or in the php.ini file.
Technical Details
Return Value: Returns the hash value of the address parameter, or FALSE
on failure. Note: Keep in mind that even if the email was
accepted for delivery, it does NOT mean the email is
actually sent and received!
PHP Version: 4+
PHP Changelog: PHP 7.2: The headers parameter also accepts an array
PHP 5.4: Added header injection protection for
the headers parameter.
PHP 4.3.0: (Windows only) All custom headers (like From,
Cc, Bcc and Date) are supported, and are not case-sensitive.
PHP 4.2.3: The parameter parameter is disabled in safe
mode
PHP 4.0.5: The parameter parameter was added
More Examples
Send an email with extra headers:
<?php
$to = "[email protected]";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: [email protected]" . "\r\n" .
"CC: [email protected]";
mail($to,$subject,$txt,$headers);
?>
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
<?php
$to = "[email protected], [email protected]";
$subject = "HTML email";
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
</tr>
</table>
</body>
</html>
";
// More headers
$headers .= 'From: <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
mail($to,$subject,$message,$headers);
?>
XML Schema
4.
What is an XML Schema?
An XML Schema describes the structure of an XML document.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
XSD Example
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML Parser
All major browsers have a built-in XML parser to access and manipulate XML.
The XML DOM (Document Object Model) defines the properties and methods for
accessing and editing XML.
However, before an XML document can be accessed, it must be loaded into an XML
DOM object.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
All modern browsers have a built-in XML parser that can convert text into an XML DOM
object.
Example
<html>
<body>
<p id="demo"></p>
<script>
var text, parser, xmlDoc;
text = "<bookstore><book>" +
"<title>Everyday Italian</title>" +
"<author>Giada De Laurentiis</author>" +
"<year>2005</year>" +
"</book></bookstore>";
document.getElementById("demo").innerHTML =
xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
</script>
</body>
</html>
OUTPUT
Everyday Italian
XML - Validation
Validation is a process by which an XML document is validated. An XML document is
said to be valid if its contents match with the elements, attributes and associated document
type declaration(DTD), and if the document complies with the constraints expressed in it.
Validation is dealt in two ways by the XML parser. They are −
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Example
<address>
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
</address>
The above example is said to be well-formed as −
It defines the type of document. Here, the document type is element type.
It includes a root element named as address.
Each of the child elements among name, company and phone is enclosed in its self
explanatory tag.
Order of the tags is maintained.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
https://play.google.com/store/apps/details?id=com.sss.edubuzz360