Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit - 4 XML

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 82

XML - eXtensible Markup Language

K Mahesh

RCEW, Pasupula (V), Nandikotkur Road,


Near Venkayapalli, KURNOOL
Syllabi
• Creating and Using Forms Understanding
Common Form Issues, GET vs. POST, Validating
form input, Working with multiple forms, and
Preventing Multiple Submissions of a form.
• 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).
XML
• Extensible Markup Language (XML) is used to describe data. The
XML standard is a flexible way to create information formats and
electronically share structured data via the public Internet, as well
as via corporate networks.
• Short for Extensible Markup Language, a specification developed
by the W3C. XML is a pared- down version of SGML, designed
especially for Web documents. It allows designers to create their
own customized tags, enabling the definition, transmission,
validation, and interpretation of data between applications and
between organizations.
– XML was designed to describe data.
– XML tags are not predefined in XML. You must define your own tags.
– XML is self describing.
– XML uses a DTD (Document Type Definition) to formally describe the data.
XML Basics
• XML, or eXtensible markup language, is all about creating
a universal way for both formatting and presenting data.
• Once data is coded or marked up with XML tags, data can
then be used in many different ways.
Features of XML
– XML files are text files, which can be managed by any text editor.
– XML is very simple, because it has less than 10 syntax rules.
Because of these features, XML offers following advantages
– XML provides a basic syntax that can be used to share information between different kinds of
computers, different applications, and different organizations. XML data is stored in plain text
format.
– With XML, your data can be available to all kinds of "reading machines" (Handheld computers,
voice machines, news feeds, etc), and make it more available for blind people, or people with
other disabilities.
– Databases can trade tables, business applications can trade updates, and document systems can
share information.
– It supports Unicode, allowing almost any information in any written human language to be
communicated.
– Its self-documenting format describes structure and field names as well as specific values.
– Content-based XML markup enhances searchability, making it possible for agents and search
engines to categorize data instead of wasting processing power on context-based full- text
searches.
– XML is heavily used as a format for document storage and processing, both online and offline.
– It is based on international standards.
– It is platform-independent, thus relatively immune to changes in technology.
– Forward and backward compatibility are relatively easy to maintain despite changes in DTD or
Schema.
How can XML be used
– XML can keep data separated from HTML
– XML can be used to store data inside HTML documents
– XML can be used as a format to exchange information
– XML can be used to store data in files or in databases
XML Syntax
<?xml version="1.0"?>
<contact-info>
<name>Mahesh</name>
<company>RECW</company>
<phone>9440047101</phone>
</contact-info>
XML Anatomy

8
Well-Formed XML: Always Parsable
Any legal XML document is always parsable by an XML
parser, without knowledge of tag meaning
– The start – preamble – tells XML about the char. encoding
<?xml version=“1.0” encoding=“utf-8”?>
– There’s a single root element
– All open-tags have matching close-tags (unlike many HTML
documents!), or a special:
<tag/> shortcut for empty tags (equivalent to <tag></tag>)
– Attributes only appear once in an element
– XML is case-sensitive
– In XML all elements must be properly nested within each
other
<b><i>This text is bold and italic</i></b>
structure of an XML document
Comparisons
• HTML was designed to display data with focus on how data looks while XML was
designed to be a software and hardware independent tool used to transport and
store data, with focus on what data is.
• HTML is a markup language itself while XML provides a framework for defining
markup languages.
• HTML is a presentation language while XML is neither a programming language nor
a presentation language.
• HTML is case insensitive while XML is case sensitive.
• HTML is used for designing a web-page to be rendered on the client side while XML
is used basically to transport data between the application and the database.
• HTML has its own predefined tags while what makes XML flexible is that custom
tags can be defined and the tags are invented by the author of the XML document.
• HTML is not strict if the user does not use the closing tags but XML makes it
mandatory for the user the close each tag that has been used.
• HTML does not preserve white space while XML does.
• HTML is about displaying data, hence static but XML is about carrying information,
hence dynamic. Thus, it can be said that HTML and XML are not competitors but
rather complement to each other and clearly serving altogether different purposes.
Types of XML Documents / 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.
1. Well-formed XML document
2. Valid XML document
• Well-formed
– A "Well Formed" XML document is a document that conforms to
the XML syntax rules.
– They contain text and XML tags.
– Everything is entered correctly.
– They do not, however, refer to a DTD.

<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
• Valid
– Valid documents not only conform to XML syntax but they also
are error checked against a Document Type Definition (DTD) or
schema

<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "InternalNote.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Building blocks of XML documents
• XML documents (and HTML documents) are made up by
the following building blocks:
– Elements,
– Tags,
– Attributes,
– Entities,
– PCDATA, and
– CDATA
– Processing instruction (to control application)
Building blocks of XML documents
• Elements
– Elements are the main building blocks of both XML and HTML
documents.
– Examples of HTML elements are "body" and "table".
– Examples of XML elements could be "note" and "message". Elements
can contain text, other elements, or be empty.
– Examples of empty HTML elements are "hr", "br" and "img".
– In a DTD, elements are declared with an ELEMENT declaration.
• Tags
– Tags are used to markup elements.
– A starting tag like <element_name> mark up the beginning of an
element, and an ending tag like </element_name> mark up the end of
an element.
– Examples: A body element: <body>body text in between</body>.
– A message element: <message>some message in between</message>
Building blocks of XML documents
• Attributes
– Attributes provide extra information about elements.
– Attributes are placed inside the start tag of an element. Attributes come in
name/value pairs. The following "img" element has an additional information about
a source file:
<img src="computer.gif" />
– The name of the element is "img". The name of the attribute is "src". The value of
the attribute is "computer.gif". Since the element itself is empty it is closed by a " /".
• PCDATA
– PCDATA stands for Parsed Character data.
– PCDATA is the text that will be parsed by a parser.
– Tags inside the PCDATA will be treated as markup and entities will be expanded.
• CDATA
– CDATA: (Unparsed Character data): CDATA contains the text which is not parsed
further in an XML document.
– Tags inside the CDATA text are not treated as markup and entities will not be
expanded.
<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE garden [
<!ELEMENT garden (plants)*>
<!ELEMENT plants (#PCDATA)>
<!ATTLIST plants category CDATA #REQUIRED>
]>
<garden>
<plants category = "flowers" />
<plants category = "shrubs">
</plants>
</garden>
Building blocks of XML documents
• Entities
– Entities as variables used to define common text. Entity references are
references to entities.
– Most of you will known the HTML entity reference: "&nbsp;" that is used to
insert an extra space in an HTML document.
– Entities are expanded when a document is parsed by an XML parser.
– References always begin with the symbol "&" which is a reserved character
and end with the symbol ";".
– XML has two types of references −
1. Entity References − An entity reference contains a name between the
start and the end delimiters. For example &amp; where amp is name.
The name refers to a predefined string of text and/or markup.
2. Character References − These contain references, such as &#65;,
contains a hash mark (“#”) followed by a number. The number always
refers to the Unicode code of a character. In this case, 65 refers to
alphabet "A".
Predefined Character Entities
Entity References Character
&lt; <
&gt; >
&amp; &
&quot; "
&apos; '
Numeric Character Entities
Entity name Character Decimal Hexadecimal
reference reference

quot " &#34; &#x22;


amp & &#38; &#x26;
apos ' &#39; &#x27;
lt < &#60; &#x3C;
gt > &#62; &#x3E;

Named Character Entity


Processing Instructions (PIs)
Processing instructions (PIs) can be used to pass information to
applications. PIs can appear anywhere in the document outside the
markup. They can appear in the prolog, including the document type
definition (DTD), in textual content, or after the document.

Syntax :
<?target instructions?>
Where:
target - identifies the application to which the instruction is directed.
instruction - it is a character that describes the information for the
application to process.

Example:
<?xml-stylesheet href="tutorialspointstyle.css” type="text/css"?>
XML - Comments
XML comment has the following syntax −
<!--Your comment-->
A comment starts with <!-- and ends with -->.
You can add textual notes as comments between the characters. 

•Comments cannot appear before XML declaration.


•Comments may appear anywhere in a document.
•Comments must not appear within attribute values.
•Comments cannot be nested inside the other comments.
Example - 1
<?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>
Document Type Definitions (DTD)
• DTD stands for Document Type Definition.
• It defines the legal building blocks of an XML document.
• It is used to define document structure with a list of legal
elements and attributes.
• Its main purpose is to define the structure of an XML
document. It contains a list of legal elements and define
the structure with the help of them.
• An XML document is called "well-formed" if it contains
the correct syntax.
• A well-formed and valid XML document is one which
have been validated against DTD.
External DTD
employee.xml

<?xml version="1.0"?>  
<!DOCTYPE employee SYSTEM "employee.dtd">  
<employee>  
  <firstname>Mahesh</firstname>  
  <lastname>Babu</lastname>  
  <email>kmaheshcse@gmail.com</email>  
</employee>   

employee.dtd
<!ELEMENT employee (firstname,lastname,email)>  
<!ELEMENT firstname (#PCDATA)>  
<!ELEMENT lastname (#PCDATA)>  
<!ELEMENT email (#PCDATA)>  
<!DOCTYPE employee : It defines that the root element of
the document is employee.
<!ELEMENT employee: It defines that the employee
element contains 3 elements "firstname, lastname and
email".
<!ELEMENT firstname: It defines that the firstname
element is #PCDATA typed. (parse-able data type).
<!ELEMENT lastname: It defines that the lastname element
is #PCDATA typed. (parse-able data type).
<!ELEMENT email: It defines that the email element is
#PCDATA typed. (parse-able data type).
Types of DTD

• A DTD can be declared inline in your XML


document, or as an external reference.
– Internal DTD
– External DTD
Internal 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>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
External DTD
<?xml version="1.0"?>
sample.xml
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

note.dtd
<?xml version="1.0"?>
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
When to Use a DTD?
• With a DTD, independent groups of people can agree to use a
standard DTD for interchanging data.
• With a DTD, you can verify that the data you receive from the
outside world is valid.
•You can also use a DTD to verify your own data.

When NOT to Use a DTD?


XML does not require a DTD.
When you are experimenting with XML, or when you are working
with small XML files, creating DTDs may be a waste of time.
XML Schema (XSD)
• A schema formally describes what a given XML document
contains, in the same way a database schema describes the
data that can be contained in a database (table structure, data
types).
• An XML schema describes the coarse shape of the XML
document, what fields an element can contain, which sub
elements it can contain, and so forth.
• It also can describe the values that can be placed into any
element or attribute.
• An XML Schema is a language for expressing constraints about
XML documents.
• There are several different schema languages in widespread
use, but the main ones are Document Type Definitions (DTDs),
Relax-NG, Schematron and W3C XSD (XML Schema Definitions).
XML Schema Used For
• to provide a list of elements and attributes in a vocabulary;
• to associate data types, such as integer, string, etc., or more
specifically such as hatsize, sock_colour, etc., with values
found in documents;
• to constrain where elements and attributes can appear, and
what can appear inside those elements, such as saying that
a chapter title occurs inside a chapter, and that a chapter
must consist of a chapter title followed by one or more
paragraphs of text;
• to provide documentation that is both human-readable and
machine-processable;
• to give a formal description of one or more documents.
• XML Schema is an XML-based alternative to DTD
Example: emp.xsd

<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>
The Schema above is interpreted like this:

• <xs:element name="note"> defines the element called


"note"
• <xs:complexType> the "note" element is a complex type
• <xs:sequence> the complex type is a sequence of
elements
• <xs:element name="to" type="xs:string"> the element
"to" is of type string (text)
• <xs:element name="from" type="xs:string"> the
element "from" is of type string
• <xs:element name="heading" type="xs:string"> the
element "heading" is of type string
• <xs:element name="body" type="xs:string"> the
element "body" is of type string
XML Schemas are More Powerful
than DTD
• XML Schemas are written in XML
• XML Schemas are extensible to additions
• XML Schemas support data types
• XML Schemas support namespaces
Why Use an XML Schema?

• With XML Schema, your XML files can carry a


description of its own format.
• With XML Schema, independent groups of
people can agree on a standard for
interchanging data.
• With XML Schema, you can verify data.
XML Schemas Support Data Types

• It is easier to describe document content


• It is easier to define restrictions on data
• It is easier to validate the correctness of data
• It is easier to convert data between different
data types
XML Schema built-in data types
• xs:string
• xs:decimal
• xs:integer
• xs:boolean
• xs:date
• xs:time
XML Schemas use XML Syntax

• You don't have to learn a new language


• You can use your XML editor to edit your
Schema files
• You can use your XML parser to parse your
Schema files
• You can manipulate your Schemas with the
XML DOM
• You can transform your Schemas with XSLT
DTD vs. XSD
DTD XSD
• DTD stands for Document • XSD stands for XML Schema
Type Definition. Definition.
• DTDs are derived from • XSDs are written in XML.
SGML syntax.
• DTD doesn't support • XSD supports datatypes for
datatypes. elements and attributes.
• DTD doesn't support • XSD supports namespace.
namespace.
• DTD doesn't define order • XSD defines order for child
for child elements. elements.
• DTD is not extensible. • XSD is extensible.
• DTD provides less control on • XSD provides more control
XML structure. on XML structure.
XML Parser
• 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.
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.
• All modern browsers have a built-in XML parser
that can convert text into an XML DOM object.
<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var parser, xmlDoc;
var text = "<bookstore><book>" +
"<title>Everyday Italian</title>" +
"<author>Giada De Laurentiis</author>" +
"<year>2005</year>" +
"</book></bookstore>";

parser = new DOMParser();


xmlDoc = parser.parseFromString(text,"text/xml");

document.getElementById("demo").innerHTML =
xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
</script>

</body>
</html>
Types of XML Parsers
• These are the two main types of XML Parsers:
– DOM
– SAX
XML DOM
XML DOM
• The W3C Document Object Model (DOM) is a platform
and language-neutral interface that allows programs and
scripts to dynamically access and update the content,
structure, and style of a document.
• 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.
• The XML DOM defines a standard way for accessing and
manipulating XML documents. It presents an XML
document as a tree-structure.
• The XML DOM is a standard for how to get, change, add,
and delete XML elements.
XML DOM
Advantages
• It supports both read and write operations and the
API is very simple to use.
• It is preferred when random access to widely
separated parts of a document is required.
Disadvantages
• It is memory inefficient. (consumes more memory
because the whole XML document needs to loaded
into memory).
• It is comparatively slower than other parsers.
<!DOCTYPE html> Ex:1
<html>
<body>

<p id="demo"></p>

<script>
var parser, xmlDoc;
var text = "<bookstore><book>" +
"<title>Everyday Italian</title>" +
"<author>Giada De Laurentiis</author>" +
"<year>2005</year>" +
"</book></bookstore>";

parser = new DOMParser();


xmlDoc = parser.parseFromString(text,"text/xml");

document.getElementById("demo").innerHTML =
xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
</script>

</body>
</html>
<?xml version="1.0" encoding="ISO-8859-1"?> Ex:2
<note> note.xml
<to>kmahesh@gmail.com</to>
<from>recw@gmail.com</from>
<body>Hello XML DOM</body>
</note>
<!DOCTYPE html> Ex:2
<html>
<body>
xmldom.html
<h1>Important Note</h1>
<div>
<b>To:</b> <span id="to"></span><br>
<b>From:</b> <span id="from"></span><br>
<b>Message:</b> <span id="message"></span>
</div>
<script>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","note.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
</script>
</body>
</html>
SAX (Simple API for XML)
• A SAX Parser implements SAX API. This API is an
event based API and less intuitive.
• It does not create any internal structure.
• Clients does not know what methods to call, they
just overrides the methods of the API and place
his own code inside method.
• It is an event based parser, it works like an event
handler in Java.
SAX (Simple API for XML)
Advantages
• 1) It is simple and memory efficient.
• 2) It is very fast and works for huge documents.
Disadvantages
• 1) It is event-based so its API is less intuitive.
• 2) Clients never know the full information because
the data is broken into pieces.
XSL and XSLT Transformation
• XSL (eXtensible Stylesheet Language) is a styling
language for XML.
• XSLT stands for XSL Transformations.
• XSLT is a language for transforming XML
documents.
• XPath is a language for navigating in XML
documents.
• XQuery is a language for querying XML documents.
• XSL = Style Sheets for XML
• XSL describes how the XML elements should be
displayed.
XSLT (XSL Transformation)
• XSLT is the most important part of XSL.
• XSLT is used to transform an XML document into another
XML document, or another type of document that is
recognized by a browser, like HTML and XHTML.
• Normally XSLT does this by transforming each XML element
into an (X)HTML element.
• With XSLT you can add/remove elements and attributes to
or from the output file. You can also rearrange and sort
elements, perform tests and make decisions about which
elements to hide and display, and a lot more.
• A common way to describe the transformation process is
to say that XSLT transforms an XML source-tree into an
XML result-tree.
XSLT Uses XPath
• XSLT uses XPath to find information in an XML document.
• XPath is used to navigate through elements and
attributes in XML documents.
• In the transformation process, XSLT uses XPath to define
parts of the source document that should 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.
• All major browsers support XSLT and XPath.
• XSLT became a 
W3C Recommendation 16. November 1999.
XSLT - Transformation
• How to transform XML into XHTML using XSLT?
Correct Style Sheet Declaration
• The root element that declares the document to be an
XSL style sheet is <xsl:stylesheet> or <xsl:transform>.
• <xsl:stylesheet> and <xsl:transform> are completely
synonymous and either can be used!
• create an XSL Style Sheet as "cdcatalog.xsl"

or
XSLT - Transformation
• Link the XSL Style Sheet to the XML Document
• If you have an XSLT compliant browser it will
nicely transform your XML into XHTML.
CDCatalog.xml CDCatalogXSLT.xsl

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

<xsl:stylesheet version="1.0"
<?xml-stylesheet type="text/xsl"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
href="CDCatalogXSLT.xsl"?>
<xsl:template match="/">
<catalog>   <html>
  <cd>   <body>
    <title>Empire Burlesque</title>   <h2>My CD Collection</h2>
    <artist>Bob Dylan</artist>   <table border="1">
    <country>USA</country>     <tr bgcolor="#9acd32">
      <th>Title</th>
    <company>Columbia</company>       <th>Artist</th>
    <price>10.90</price>     </tr>
    <year>1985</year>     <xsl:for-each select="catalog/cd">
  </cd>     <tr>
.       <td><xsl:value-of select="title"/></td>
.       <td><xsl:value-of select="artist"/></td>
</catalog>     </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>
XSLT <xsl:template> Element
• An XSL style sheet consists of one or more set of rules
that are called templates.
• A template contains rules to apply when a specified
node is matched.
• The <xsl:template> element is used to build templates.
<xsl:template match="/">
• The match attribute is used to associate a template
with an XML element. The match attribute can also be
used to define a template for the entire XML
document. The value of the match attribute is an
XPath expression (i.e. match="/" defines the whole
document).
<xsl:stylesheet>
• <xsl:stylesheet>, defines that this document is an
XSLT style sheet document (along with the version
number and XSLT namespace attributes).
• An XML namespace is a collection of names
that can be used as element or attribute names in
an XML document. The namespace qualifies element
names uniquely on the Web in order to avoid conflicts
between elements with the same name.

<xsl:stylesheet version="1.0“
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
XSLT <xsl:value-of> Element
• The <xsl:value-of> element is used to extract the
value of a selected node.
• The <xsl:value-of> element can be used to extract
the value of an XML element and add it to the
output stream of the transformation:

<td><xsl:value-of select="catalog/cd/title"/></td>
XSLT <xsl:for-each> Element
• The <xsl:for-each> element allows you to do
looping in XSLT.
• The XSL <xsl:for-each> element can be used to
select every XML element of a specified node-set:

<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>
XSLT <xsl:sort> Element
• The <xsl:sort> element is used to sort the output.

<xsl:for-each select="catalog/cd">
      <xsl:sort select="artist"/>
<xsl:if> Element
• To put a conditional if test against the content of the
XML file, add an <xsl:if> element to the XSL
document.
<xsl:if test="expression">
  ...some output if the expression is true...
</xsl:if>

<xsl:for-each select="catalog/cd">
      <xsl:if test="price &gt; 10">
        <tr>
          <td><xsl:value-of select="title"/></td>
          <td><xsl:value-of select="artist"/></td>
          <td><xsl:value-of select="price"/></td>
        </tr>
      </xsl:if>
<xsl:choose> Element
• The <xsl:choose> element is used in conjunction
with <xsl:when> and <xsl:otherwise> to express
multiple conditional tests.
<xsl:choose>
  <xsl:when test="expression">
    ... some output ...
  </xsl:when>
  <xsl:otherwise>
    ... some output ....
  </xsl:otherwise>
</xsl:choose>
<xsl:apply-templates> Element
• The <xsl:apply-templates> element applies a
template rule to the current element or to the
current element's child nodes.
• If we add a "select" attribute to the <xsl:apply-
templates> element, it will process only the child
elements that matches the value of the attribute.
• We can use the "select" attribute to specify in
which order the child nodes are to be processed.
<xsl:apply-templates/>
PHP Code: Transform XML to XHTML on the Server
<?php
// Load XML file
$xml = new DOMDocument;
$xml->load('cdcatalog.xml');

// Load XSL file


$xsl = new DOMDocument;
$xsl->load('cdcatalog.xsl');

// Configure the transformer


$proc = new XSLTProcessor;

// Attach the xsl rules


Install the XSL extension to get that
class. This might be as easy as $proc->importStyleSheet($xsl);
uncommenting (remove the starting
';') on the line that
reads extension=php_xsl.dll in php.ini
on Windows echo $proc->transformToXML($xml);
Before RSS & Atom
After RSS & Atom

-Web Based- -Non-Web Based-


 Google Reader The Benefit?
 RssReader Is to organize news,
 Netvibes  RSS Bandit
 Pageflakes highlights, and
 RSSOwl information in one
 My Yahoo!  Thunderbird
 Bloglines handy location, saving
 NewsGator the user valuable time
 Fwicki from having to checkout
all their favorite
websites for new
information on a weekly,
daily, hourly… bases.
What is RSS and Atom all about?
• They are a form of communication using XML
documents to broadcast information updates
to a large group of subscribers.
• To distribute up-to-date web content from one
web site to thousands of other web sites
around the world.
• RSS allows fast browsing for news and
updates.
RSS (Really Simple Syndication)
• With RSS it is possible to distribute up-to-date web
content from one web site to thousands of other
web sites around the world.
• RSS allows fast browsing for news and updates.
• RSS stands for Really Simple Syndication
• RSS allows you to syndicate your site content
• RSS defines an easy way to share and view
headlines and content
• RSS files can be automatically updated
• RSS allows personalized views for different sites
• RSS is written in XML
Why use RSS?
• RSS was designed to show selected data.
• Without RSS, users will have to check your site daily for
new updates. This may be too time-consuming for many
users. With an RSS feed (RSS is often called a News feed or
RSS feed) they can check your site faster using an RSS
aggregator (a site or program that gathers and sorts out
RSS feeds).
• Since RSS data is small and fast-loading, it can easily be
used with services like cell phones or PDA's.
• Web-rings with similar information can easily share data
on their web sites to make them better and more useful.
What is RSS 2.0 ?
• RSS stands for Really Simple Syndication. It's an easy
way for you to keep up with news and information
that's important to you, and helps you avoid the
conventional methods of browsing or searching for
information on websites. Now the content you want
can be delivered directly to you without cluttering
your inbox with e-mail messages. This content is
called a "feed.“

• RSS is written in the Internet coding language known


as XML (eXtensible Markup Language).
 RSS must be the root
RSS 2.0
element followed by one
channel element.

 Required channel elements:

title The name of the channel. It's how people refer


to your service. If you have an HTML website
that contains the same information as your RSS
file, the title of your channel should be the
same as the title of your website.

link The URL to the HTML website corresponding to


the channel.

description Phrase or sentence describing the channel.


RSS 2.0
Optional <channel > Elements Elements for Item
Is the email address
language title
for the author.
Copyright link
description
item
author It has one optional
managingEditor category attribute consisting of
webMaster comments “domain”.
pubDate enclosure
lastBuildDate guid
category pubData Has three required
source attributes.
generator
docs
cloud Has three required elements, along -Required-
ttl with optional ones. • url
image • length
• type
rating -Required- -Optional-
textInput • url • width
skipHours • title • height
skipDays • link
Note: Not all required and optional elements or attributes are shown!
<?xml version="1.0"?>
<rss version="2.0">
<channel>
RSS 2.0 Sample
<title>Liftoff News</title>
<link>http://liftoff.msfc.nasa.gov/</link>
<description>Liftoff to Space Exploration.</description>
<language>en-us</language>
<pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate>
<lastBuildDate>Tue, 10 Jun 2003 09:41:01 GMT</lastBuildDate>
<docs>http://blogs.law.harvard.edu/tech/rss</docs>
<generator>Weblog Editor 2.0</generator>
<managingEditor>editor@example.com</managingEditor>
<webMaster>webmaster@example.com</webMaster>
<item>
<title>Star City</title>
<link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>
<description>How do Americans get ready to work with Russians aboard the International Spac
Station? They take a crash course in culture, language and protocol at Russia's &lt;a
href="http://howe.iki.rssi.ru/GCTC/gctc_e.htm"&gt;Star City&lt;/a&gt;.
</description>
<pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid>
</item>
<item>
<title>The Engine That Does More</title>
<link>http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp</link>
<description>Before man travels to Mars, NASA hopes to design new engines that will let us fly
through the Solar System more quickly. The proposed VASIMR engine would do that.
</description>
<pubDate>Tue, 27 May 2003 08:37:32 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/05/27.html#item571</guid>
</item>
</channel>
</rss>
What is ATOM 1.0 ?
• ATOM – The Atom Syndication Format is the next generation of
XML-based file formats, designed to allow information--the
contents of web pages, for example--to be syndicated between
applications.
• Like RSS before it, Atom places the content and metadata of an
internet resource into a machine-parsable format, perfect for
displaying, filtering, remixing, and archiving.
• The name Atom applies to a pair of related Web standards.
The Atom Syndication Format is an XML language used for web
feeds, while the Atom Publishing Protocol is a simple HTTP-
based protocol for creating and updating web resources.
• Web feeds allow software programs to check for updates
published on a website.
Atom
General considerations:
• All elements described in Atom must be in the
 In Atom, <feed> is used for http://www.w3.org/2005/Atom namespace.
the root element. • xml:lang may be used to identify the language of any
human readable text.
• xml:base may be used to control how relative URIs
are resolved.
 Required <feed> elements:

 id A unique identifier, which can be as simple as


the URI of a blog entry or other Web resource
represented by an entry.

 title Contains a human readable title for the feed.


Often the same as the title of the associated
website.

 updated A timestamp which indicates when the last


update occurred.
(Must conform to RFC 3339)
Atom
Optional <feed> Elements
category Has three required elements, along
contributor with optional ones.
entry
published -Required- -Optional-
contributor • id • author
generator • title • content
icon • update • link
logo • summary
• category
source
• contributor
rights • published
subtitle • source
author Has the required element <name>, • rights
link along with two optional ones.

-Optional-
• uri
• email
Note: Not all required and optional elements or attributes are shown!
<?xml version="1.0"?>
Atom Sample
<feed xmlns="http://www.w3.org/2005/Atom">
<link rel="self" href="http://example.org/blog/index.atom"/>
<id>http://example.org/blog/index.atom</id>
<icon>../favicon.ico</icon>
<title>An Atom Sampler</title>
<subtitle>No Splitting</subtitle>
<author>
<name>Ernie Rutherford </name>
<email>ernie@example.org</email>
<uri>.</uri>
</author>
<updated>2006-10-25T03:38:08-04:00</updated>
<link href="."/>
<entry>
<id>tag:example.org,2004:2417</id>
<link href="2006/10/23/moonshine"/>
<title>Moonshine</title>
<content type="text">
Anyone who expects a source of power from the transformation of the atom is talking moonshine.
</content>
<published>2006-10-23T15:33:00-04:00</published>
<updated>2006-10-23T15:47:31-04:00</updated>
</entry>
<entry>
<id>>tag:example.org,2004:2416</id>
<link href="2006/10/21/think"/>
<title type="html">&lt;strong&gt;Think!&lt;/strong&gt;</title>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<p>We haven't got the money, so we've got to think!</p>
</div>
</content>
<updated>2006-10-21T06:02:39-04:00</updated>
</entry>
</feed>
END

You might also like