Page 342 - Open Soource Technologies 304.indd
P. 342
Web Technologies-I
Notes
<time>2002-04-30 09:04:23</time>
<author>k.tatroe</author>
</story>
</news>
Given example is the XSL document we will use to transform the XML document into HTML.
Each xsl:template element contains a rule for dealing with part of the input document.
News XSL transform
<?xml version=”1.0” encoding=”utf-8” ?>
<xsl:stylesheet version=”1.0” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>
<xsl:output method=”html” indent=”yes” encoding=”utf-8” />
<xsl:template match=”/news”>
<html>
<head>
<title>Current Stories</title>
</head> <body bgcolor=”white” >
<xsl:call-template name=”stories”/>
</body>
</html>
</xsl:template>
<xsl:template name=”stories”>
<xsl:for-each select=”story”>
<h1><xsl:value-of select=”title” /></h1> <p>
<xsl:value-of select=”author”/> (<xsl:value-of select=”time”/>)<br/> <xsl:value-of
select=”teaser”/> [ <a href=”{url}”>More</a> ] </p>
<hr />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The given below example is the very small amount of code necessary to transform the XML
document into an HTML document using the XSL style sheet. We create a processor, run the
files through it, and print the result.
XSL transformation from files
<?php
$processor = xslt_create( );
$result = xslt_process($processor, ‘news.xml’, ‘news.xsl’);
if(!$result) echo xslt_error($processor);
336 LOVELY PROFESSIONAL UNIVERSITY