module -4
XML: Introduction; Syntax; Document structure; Document
Type definitions; Namespaces; XML
schemas; Displaying raw XML documents;
Displaying XML documents with CSS; XSLT style
sheets; XML processors; Web services. PHP: Origins
and uses of PHP, Overview of PHP, General
syntactic characteristics, Primitives, Operations and
Activity 2:
Design a Database using
XML concepts
expressions, Output, Control statements, Arrays, Functions,
Pattern matching, Form handling, Files,
Cookies, Session tracking
• XML - Introduction
• Content:
• Definition: XML (eXtensible Markup Language) is
a markup language for storing and transporting
data.
• Key Features:
• Human-readable.
• Platform-independent.
• Self-descriptive.
• Applications: Config files, web services, data
exchange.
• ?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
• XML Syntax
• Syntax Rules:
• Must have a single root element.
• Tags are case-sensitive.
• Attribute values must be quoted.
• Properly nested tags.
• <note>
• <to>Tove</to>
• <from>Jani</from>
• <message>Remember the meeting at 10 AM.</message>
• </note>
• XML Documents Must Have a Root Element
• XML documents must contain one root element that is the parent of all
other elements
• All XML Elements Must Have a Closing Tag
• ML tags are case sensitive. The tag <Letter> is different from the tag
<letter>.
• Opening and closing tags must be written with the same case:
• XML Elements Must be Properly Nested
• XML Attribute Values Must Always be Quoted
• XML elements can have attributes in name/value pairs just like in HTML.
• Entity References
• Some characters have a special meaning in XML.
• If you place a character like "<" inside an XML element, it will generate an
error because the parser interprets it as the start of a new element.
: XML Document Structure
•Content:
•Components:
•Prolog (<?xml version="1.0"?>).
•Elements (tags).
•Attributes.
•Text content.
Document Structure
Document Structure and Document Type Definitions (DTDs) are essential concepts in document
management, particularly in markup languages like XML or HTML. Below is an overview of each:
The document structure refers to the logical organization of content in a document. It defines how
various components of a document are arranged hierarchically or sequentially.
Key Components of Document Structure:
1.Header/Metadata
•Provides information about the document (e.g., title, author, date).
•In XML/HTML: Defined using <head> or <metadata> tags.
2.Body
•The main content of the document.
•Structured into sections, paragraphs, or other elements depending on the format.
3.Sections
•Logical divisions like chapters, articles, or sub-sections.
4.Hierarchical Arrangement
•Often represented using a tree structure in XML/HTML:
•<root> element at the top.
•Child and nested elements representing subsections or content.
Document Type Definitions (DTDs)
A Document Type Definition (DTD) specifies the structure and rules for a document in markup
languages like XML. It defines what elements, attributes, and nesting are allowed.
Components of DTD:
1.Element Declarations
•Define elements and their relationships.
•Syntax: <!ELEMENT element-name (child-elements)>
Example:
xml
<!ELEMENT book (title, author, content)>
2.Attribute Declarations
•Specify attributes for elements and their types.
•Syntax: <!ATTLIST element-name attribute-name attribute-type default-value>
Example:
xml
<!ATTLIST book id ID #REQUIRED>
3.Entity Declarations
•Define reusable content or special characters.
•Syntax: <!ENTITY name "value">
Example:
xml
<!ENTITY copyright "© 2024">
4.Types of DTD
•Internal DTD: Defined within the document.
Example:
xml
<!DOCTYPE book [ <!ELEMENT book (title, author, content)> ]>
•External DTD: Referenced as an external file.
Example:
xml
<!DOCTYPE book SYSTEM "book.dtd">
• Importance of Document Structure and DTDs
• Consistency: Ensures all documents follow a uniform structure.
• Validation: DTDs help validate the correctness of document content.
• Interoperability: Facilitates sharing and interpretation of documents across systems.
• Maintainability: Simplifies updates by enforcing a predefined structure.
Namespaces in XML
Namespaces in XML are used to avoid naming conflicts by qualifying element and attribute names.
This is especially useful when combining XML documents from different sources.
Key Points About Namespaces:
1.Definition:
•Declared using xmlns (XML namespace) attribute in the root or any element.
•A namespace is identified by a URI (Uniform Resource Identifier).
2.Syntax:
xml
<root xmlns:prefix="http://example.com/namespace"> <prefix:element>Value</prefix:element> </root>
3.Default Namespace:
•When no prefix is specified, a default namespace applies to all child elements.
xml
<root xmlns="http://example.com/namespace"> <element>Value</element> </root>
4.Using Namespaces:
•Elements and attributes can belong to different namespaces.
•Namespaces differentiate identical tags in multi-source XML documents.
XML Schemas (XSDs)
An XML Schema defines the structure, content, and data types of XML documents, providing a more powerful
alternative to DTDs.
Features of XML Schemas:
1.Data Type Support:
•Offers built-in data types (e.g., string, integer, date) and allows user-defined types.
2.Namespace Aware:
•Allows XML documents to validate against schemas using namespaces.
3.Extensible and Flexible:
•Enables element nesting, choice, and sequence specifications.
Basic Syntax Example:
Schema (XSD file):
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Displaying Raw XML Documents
Raw XML Viewing:
1.In a Browser:
•Most modern web browsers can render XML directly, showing it as a collapsible tree structure.
•Example: Open an .xml file in Chrome or Firefox to view the raw structure.
2.Plain Text:
•Open with any text editor to see raw tags and data. Rendered Display: Using XSLT:
•Transform XML into HTML for human-readable rendering.
•Example:
XSLT File (style.xsl):
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<h2>XML Data</h2>
<xsl:for-each select="data/item">
<p><xsl:value-of select="."/></p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
• Displaying XML Documents with CSS
• CSS (Cascading Style Sheets) can style XML documents for basic rendering in web browsers,
providing a visual representation without requiring transformation.
• Key Points:
1.Syntax:
1. CSS styles are applied directly to XML elements using a linked or embedded stylesheet.
note {
2. Example:
font-family: Arial, sans-
• <?xml version="1.0"?>
serif;
• <?xml-stylesheet type="text/css" href="style.css"?>
}
• <note>
to, from {
• <to>John</to> color: blue;
• <from>Jane</from> font-weight: bold;
• <message>Hello, world!</message> }
• </note> message {
color: green;
• Limitations: }
• CSS can style but cannot rearrange or transform XML content (unlike XSLT).
XSLT Style Sheets
XSLT (Extensible Stylesheet Language Transformations) transforms
XML documents into other formats like HTML, plain text, or another
XML structure. <xsl:stylesheet
Key Features: xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
1.Syntax: version="1.0">
•Written in XML format. <xsl:template match="/">
•Templates match XML elements and specify their <html>
transformation. <body>
2.Basic Example: <h2>Book List</h2>
XML Document: <xsl:for-each select="books/book">
Xml <p>
<?xml version="1.0"?> <b>Title:</b> <xsl:value-of
<?xml-stylesheet type="text/xsl" href="style.xsl"?> select="title"/><br/>
<books> <b>Author:</b> <xsl:value-of
<book> select="author"/>
<title>XML Basics</title> </p>
<author>John Doe</author> </xsl:for-each>
</book> </body>
</books> </html>
</xsl:template>
</xsl:stylesheet>
• XML Processors
• XML processors (parsers) are software tools that read and interpret XML
documents, ensuring they are well-formed and optionally valid against a DTD or
schema.
• Types of XML Processors:
1.Validating Processors:
1. Check the document’s structure and content against a DTD or XML Schema.
2. Example: Xerces, MSXML.
2.Non-Validating Processors:
1. Ensure the document is well-formed but do not validate it.
3.APIs for Processing:
1. DOM (Document Object Model): Reads the entire document into memory as a tree
structure.
2. SAX (Simple API for XML): Processes the document sequentially, triggering events for each
element.
from xml.dom import minidom
doc = minidom.parse('example.xml')
• Web Services
• Web services are software systems designed to support interoperable machine-to-machine
communication over a network. They enable applications to exchange data and perform
operations regardless of their underlying platforms or programming languages.
• Key Concepts:
1. Types of Web Services:
1. SOAP (Simple Object Access Protocol):
1. Uses XML for messaging.
2. Standardized protocol but complex and heavyweight.
2. REST (Representational State Transfer):
1. Lightweight, uses HTTP for communication (GET, POST, PUT, DELETE).
2. Commonly uses JSON or XML for data exchange.
2. Uses:
1. APIs for web and mobile apps.
2. Integration of heterogeneous systems.
3. Cloud services, e-commerce, and IoT communication.
• Key Features:
1. Platform-Independent: Enable interaction between applications written in different languages or
running on different platforms.
2. Communication Protocols: Commonly use HTTP, SOAP, or REST.
3. Data Formats: Use XML or JSON for data exchange.
• Types of Web Services:
1. SOAP (Simple Object Access Protocol):
1. XML-based protocol for structured message exchange.
2. Often used in enterprise applications.
2. REST (Representational State Transfer):
1. Lightweight and stateless.
2. Uses HTTP methods (GET, POST, PUT, DELETE).
3. Widely adopted for APIs.
• PHP: Origins and Uses
• PHP (Hypertext Preprocessor) is a server-side scripting language widely used for web development.
• Origins of PHP:
1. Creator: Rasmus Lerdorf in 1994.
2. Initial Purpose:
1. Personal Home Page Tools to manage a personal website.
2. Later evolved to include a scripting language.
• Uses of PHP:
1. Dynamic Website Development:
1. Integrates with HTML to create dynamic and interactive web pages.
2. Server-Side Logic:
1. Handles user input, database operations, and session management.
3. API Development:
1. Enables back-end services for mobile and web applications.
4. Popular Applications:
1. Content Management Systems (e.g., WordPress, Drupal).
2. E-commerce platforms (e.g., Magento).
1.Web Development:
1. Embeds seamlessly into HTML.
2. Dynamic content generation, session management, and form handling.
2.Server-Side Scripting:
1. Handles server-side logic, such as database interactions and user
authentication.
3.Command-Line Scripting:
1. Automates repetitive server tasks using PHP scripts in CLI mode.
4.Database Interaction:
1. Easily integrates with databases like MySQL, PostgreSQL, and SQLite.
5.API Development:
1. Builds RESTful or SOAP-based APIs for web and mobile applications.
General Syntactic Characteristics of PHP
1.Syntax Basics:
•Code is enclosed within <?php and ?> tags.
•Statements end with a semicolon (;).
•Variables begin with a $ symbol.
Example:
php
<?php $name = "Alice"; echo "Hello, $name!"; ?>
2.Control Structures:
•Conditional Statements (if, else, switch).
•Loops (for, while, foreach).
Example:
php
<?php for ($i = 0; $i < 5; $i++) { echo $i . "<br>"; } ?>
3.Functions:
•Defined using function keyword.
Example:
php
<?php function greet($name) { return "Hello, $name!"; } echo greet("Alice"); ?>
Primitives in PHP
Primitives are basic data types in PHP.
1.Scalar Types:
•Integer: Whole numbers (e.g., 42, -10).
•Float: Decimal numbers (e.g., 3.14).
•String: Text data (e.g., "Hello").
•Boolean: true or false.
2.Compound Types:
•Array: Collection of values.
Example:
php
$fruits = array("apple", "banana", "cherry");
•Object: Instances of classes.
3.Special Types:
•NULL: Represents a variable with no value.
•Resource: Holds references to external resources (e.g., database connections).
Data Types:
PHP is loosely typed, meaning variables can hold values of any type without explicit declaration.
1.Scalar Types: Integer: Whole numbers.
php
$num = 42; Float: Decimal numbers.
php
$pi = 3.14; String: Sequence of characters.
php
$name = "Alice"; Boolean: true or false.
php
$isActive = true;
2.Compound Types:
•Array: Indexed or associative collections.
php
$colors = ["red", "green", "blue"]; $ages = ["Alice" => 25, "Bob" => 30];
•Object: Instances of user-defined classes.
php
class Car { public $brand; function __construct($brand) { $this->brand = $brand; } } $car = new Car("Toyota");
3.Special Types:
•NULL: Represents no value.
php
$var = NULL;
•Resource: Reference to external resources (e.g., database connections, file handles).
Operations in PHP
PHP supports various operations for arithmetic, assignment, comparison, logical processing, and more.
1. Arithmetic Operations
php
$a = 10; $b = 5; echo $a + $b; // Addition echo $a - $b; // Subtraction echo $a * $b; // Multiplication echo $a /
$b; // Division echo $a % $b; // Modulus
2. Assignment Operations
php
$x = 5; $x += 2; // Equivalent to $x = $x + 2;
3. Comparison Operations
php
$a == $b; // Equal $a != $b; // Not equal $a > $b; // Greater than $a < $b; // Less than
4. Logical Operations
php
if ($a > 0 && $b > 0) { echo "Both are positive."; }
Design a Database Using XML Concepts
XML can be used to represent a database-like structure for lightweight data storage or configuration.
XML Database Example:
xml
<library> <book id="1"> <title>PHP Basics</title> <author>John Doe</author> <year>2021</year> </book>
<book id="2"> <title>Advanced XML</title> <author>Jane Smith</author> <year>2023</year> </book>
</library>
PHP Code to Process XML:
php
$xml = simplexml_load_file("library.xml"); foreach ($xml->book as $book) { echo "Title: " . $book->title .
"<br>"; echo "Author: " . $book->author . "<br>"; echo "Year: " . $book->year . "<br><br>"; }
Expressions in PHP
Output in PHP Expressions are units of code that evaluate to a value.
PHP provides functions to display data. php
1.echo: Outputs strings or variables. $result = 5 + 3; // Arithmetic expression $greeting = "Hello, " . $name; //
php String concatenation
echo "Hello, World!"; echo $variable; Arrays
2.print: Similar to echo but returns a value. 1.Indexed Arrays:
php php
print "This is a test."; $fruits = ["Apple", "Banana", "Cherry"]; echo $fruits[0];
2.Associative Arrays:
Control Statements
php
Conditional Statements:
$ages = ["Alice" => 25, "Bob" => 30]; echo $ages["Alice"];
php
if ($age > 18) { echo "Adult"; } elseif ($age == 18) { echo "Just turned 18"; } else { echo "Minor"; }
Loops:
php
for ($i = 0; $i < 5; $i++) { echo $i; } while ($i < 10) { echo $i; $i++; }
Functions in PHP
php
function greet($name) { return "Hello, $name!"; } echo greet("John");