<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>dotvoid.com &#187; xsl</title>
	<atom:link href="http://www.dotvoid.com/tag/xsl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotvoid.com</link>
	<description>Experiments and thoughts in PHP and javascript</description>
	<lastBuildDate>Tue, 11 Oct 2011 12:49:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Putting XSL and PHP to work</title>
		<link>http://www.dotvoid.com/2002/08/putting-xsl-and-php-to-work/</link>
		<comments>http://www.dotvoid.com/2002/08/putting-xsl-and-php-to-work/#comments</comments>
		<pubDate>Wed, 14 Aug 2002 22:46:42 +0000</pubDate>
		<dc:creator>Danne</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xsl]]></category>

		<guid isPermaLink="false">http://www.commodi.com/?p=24</guid>
		<description><![CDATA[
We all know PHP is great for web development. We also know that XSLT is a great way of formatting, or tranforming, XML data. Combine these technologies and we have a very powerful toolkit in developing large scale web sites where content is clearly separated from logic and presentation. This article will show you how [...]]]></description>
			<content:encoded><![CDATA[<div class="preamble">
<p>We all know PHP is great for web development. We also know that XSLT is a great way of formatting, or tranforming, XML data. Combine these technologies and we have a very powerful toolkit in developing large scale web sites where content is clearly separated from logic and presentation. This article will show you how to put the revamped XSLT API in PHP 4.1 to good use.</p></div>
<h3>Introduction</h3>
<p>The xslt api in php changed quite a bit in version 4.1. One of the major reasons is to make the xslt extension more loosely tied to the actual XSLT processor used. Still only Sablotron from <a href="http://www.gingerall.com/">Ginger Alliance</a> is supported but plans are to support other libraries such as Xalan or libxslt at a later stage.</p>
<p>Among the more notable changes are the removal of the functions xslt_run() and xslt_output_begintransform(). When running configure you should now use the options &#8211;enable-xslt &#8211;with-xslt-sablot instead of the old option &#8211;with-sablot. If you use Sablotron with javascript-support you also need to specify &#8211;with-sablot-js. This last option is common to overlook.</p>
<p>A warning might be in place. This article is written on the assumption that you already know or at least have a basic understanding of both xslt and php.</p>
<h3>Template and data</h3>
<p>In most of the examples I will be using the xml and xslt below. Both are very simple and I will use a traditional example with books.</p>
<pre><code>&lt;?xml version='1.0'?&gt;
&lt;library&gt;
  &lt;book&gt;
      &lt;title&gt;Lord of the Rings&lt;/title&gt;
      &lt;author&gt;Tolkien&lt;/author&gt;
      &lt;description&gt;A nice little tale about hobbits&lt;/description&gt;
    &lt;/book&gt;
    &lt;book&gt;
      &lt;title&gt;Soldier of the Mist&lt;/title&gt;
      &lt;author&gt;Wolfe&lt;/author&gt;
      &lt;description&gt;Sad story about a soldiers memory loss&lt;/description&gt;
  &lt;/book&gt;
&lt;/library&gt;</code></pre>
<pre><code>&lt;?xml version='1.0'?&gt;
&lt;xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

&lt;xsl:template match="/"&gt;
  &lt;html&gt;
  &lt;head&gt;&lt;title&gt;Booklist&lt;/title&gt;&lt;/head&gt;
  &lt;body&gt;
  &lt;xsl:apply-templates /&gt;
  &lt;/body&gt;
  &lt;/html&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match="library"&gt;
  &lt;xsl:apply-templates /&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match="book"&gt;
  &lt;p&gt;
  &lt;xsl:apply-templates /&gt;
  &lt;/p&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match="title"&gt;
  &lt;b&gt;&lt;xsl:value-of select="." /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match="author"&gt;
  &lt;i&gt;&lt;xsl:value-of select="." /&gt;&lt;/i&gt;&lt;br /&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match="description"&gt;
  &lt;b&gt;&lt;xsl:value-of select="." /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;</code></pre>
<h3>Using static files</h3>
<p>Lets assume that the examples above can be found in the two files mylibrary.xml and libraryhtml.xsl. To publish the xml in html format is then very straightforward. Remember to give the full path names to the xml and xsl files. (Because of the php-bug Bug #16228 XSLT file path issues\&#8221; some might need to prefix the file path with file://)</p>
<pre><code>&lt;?php
   $xp = xslt_create();
   $result = xslt_process($xp, 'mylibrary.xml', 'libraryhtml.xsl');
   echo $result;
   xslt_free($xp);
?&gt;</code></pre>
<p>That is it. Very simple. First we use the xslt_create() function to obtain a resource handle to a xslt processor. This resource handle must be given as the first parameter to all xslt api functions. The function xslt_process() takes the resource handle and the two file names as parameters and stores the result of the transformation in the variable $result. In this case we just echo the result to the client. The last thing we do is to clean up after ourselves by freeing the xslt processor using xslt_free().</p>
<p>If we wanted to, xslt_process could write the result into a file instead. It&#8217;s as easy as adding a fourth parameter with a filename.</p>
<pre><code>&lt;?php
   $xp = xslt_create();
   xslt_process($xp, 'mylibrary.xml', 'libraryhtml.xsl', 'library.html');
   xslt_free($xp);
?&gt;</code></pre>
<h3>A more dynamic approach</h3>
<p>Often the xml is generated dynamically in some way. Whether it comes from a database or some other data source we need to to feed xslt_process() with xml data stored in a variable.</p>
<pre><code>&lt;?php
$xml = get_xml_from_datasource();

$arguments = array(
  '/_xml' =&gt; $xml
);

$xp = xslt_create();
$result = xslt_process($xp, 'arg:/_xml', 'libraryhtml.xsl',
                       NULL, $arguments);
echo $result;
xslt_free($xp);
?&gt;</code></pre>
<p>Again not very complicated. We create an associative array for storing arguments to xslt_process(). You will see why an array is needed below. We tell xslt_process() that we want it too read the xml from the arguments by giving it &#8216;arg:/_xml&#8217; as a second parameter instead of a filename of a xml file. The arguments array is given as a fifth parameter. The fourth, file result parameter, must then be set to NULL indicating that we want the result stored in a variable.</p>
<p>The arguments must be supplied in an array. Apart from the xml we could also supply the xslt as an argument instead of a file. This could be useful if we have a template library stored in database as might be the case in Content Management System.</p>
<pre><code>&lt;?php
$xml = get_xml_from_datasource();
$xsl = get_xsl_from_templatelibrary();

$arguments = array(
  '/_xml' =&gt; $xml,
  '/_xsl' =&gt; $xsl
);

$xp = xslt_create();
$result = xslt_process($xp, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments);
echo $result;
xslt_free($xp);
?&gt;</code></pre>
<p>This is all nice and dandy but hardly enough. In more complex environments you would want proper error handling. When developing and debugging it could be useful to log results of transformations to a log file. Sometimes sending parameters to the xsl template might be needed. Luckily we can do all that.</p>
<h3>Error handling and logging</h3>
<p>When calling xslt_process() it always returns a result. Whether we want the result of the transformation in a variable or not the return value evaluates to false if something went wrong. If it is false we can use the functions xslt_errno() and xslt_error() to find out what went wrong. If something goes wrong warnings will often be echoed to the client. We should always avoid that by prepending @ to the function call.</p>
<p>In a live system it would not be very nice to just blurt out an error code with a cryptic error message. More proper would be to automatically send an alert via email to an administrator and redirect the user to an error page begging for his forgiveness. We do want our visitors to come back now, don&#8217;t we?</p>
<pre><code>&lt;?php
$xp = xslt_create();

$result = @xslt_process($xp, 'library.xml', 'libraryhtml.xsl');
if (!$result)
{
    $msg = "An error occurred on line " .__LINE__;
    $msg .= " in " .$_SERVER['PHP_SELF'] ."\n";
    $msg .= "Error no: " .xslt_errno($xp) ."\n";
    $msg .= "Error   : " .xslt_error($xp) ."\n";
    send_message_to_admin($msg);
    header('Location: http://www.dotvoid.com/error.php');
    exit(0);
}

echo $result;
xslt_free($xp);
?&gt;</code></pre>
<p>Even better would be to wrap this functionality in one function and make all errors go there. Easier to handle and cleaner code. We do that using the function xslt_set_error_handler(). The parameter $fields contain all the information we need.</p>
<pre><code>&lt;?php
function handle_xslt_error($xp, $errorno, $level, $fields)
{
    // Handle errors
    header('Location: http://www.dotvoid.com/error.php');
    exit(0);
}

$xp = xslt_create();
xslt_set_error_handler($xp, "handle_xslt_error");

$result = @xslt_process($xp, 'library.xml', 'libraryhtml.xsl');

echo $result;
xslt_free($xp);
?&gt;</code></pre>
<p>If we want to read the information stored in the $fields parameter we can use the following code.</p>
<pre><code>$msg = '';
if(is_array($fields))
{
   while(list($key, $value) = each($fields))
    {
      $msg .= "$key =&gt; $value&lt;br&gt;\n";
    }
   echo $msg;
}</code></pre>
<p>The function xslt_set_error_handler() makes it easy to put a custom error handler in an external library file. It keeps the code clean, robust and easier to maintain. But when we are developing or debugging an application we want to know what is going on all the time. Not only when errors occur. We need logging.</p>
<pre><code>&lt;?php

$xp = xslt_create();
xslt_set_log($xp, true);
xslt_set_log($xp, 'debug.log');

$result = @xslt_process($xp, 'library.xml', 'libraryhtml.xsl');
echo $result;

xslt_free($xp);
?&gt;</code></pre>
<p>The function xslt_set_log either takes a boolean or a string for the filename of a logfile. When we turn the logging on we first call xslt_set_log() with true and then we give it the path to the logfile. So if we would like to we could turn logging off by giving the function false as the second parameter. The logging facility tells us what files (or dynamically generated xml/xsl) are used in the transformation and in how many milliseconds it took.</p>
<h3>Sending parameters</h3>
<p>Sending parameters to the xsl template is easy. It is often needed in more serious applications. For example when passing the name of the logged in user, what locale is going to be used or just about any dynamica data nuggets not included in the xml. In the example below we send the currentLocale to the xsl template. First we change the libraryhtml.xsl a bit. We add the xsl:param element using en-us as default.</p>
<pre><code>&lt;?xml version='1.0'?&gt;
&lt;xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

&lt;xsl:param name="locale"&gt;en-us&lt;/xsl:param&gt;

&lt;xsl:template match="/"&gt;
  &lt;html&gt;
  &lt;head&gt;&lt;title&gt;Booklist&lt;/title&gt;&lt;/head&gt;
  &lt;body&gt;
  &lt;xsl:apply-templates /&gt;
  &lt;/body&gt;
  &lt;/html&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match="library"&gt;
  &lt;xsl:choose&gt;
    &lt;xsl:when test="$locale = 'sv'"&gt;
       You speak Swedish&lt;br /&gt;
    &lt;/xsl:when&gt;
    &lt;xsl:otherwise&gt;
       You speak American English&lt;br /&gt;
    &lt;/xsl:otherwise&gt;
  &lt;/xsl:choose&gt;

  &lt;xsl:apply-templates /&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match="book"&gt;
  &lt;p&gt;
  &lt;xsl:apply-templates /&gt;
  &lt;/p&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match="title"&gt;
  &lt;b&gt;&lt;xsl:value-of select="." /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match="author"&gt;
  &lt;i&gt;&lt;xsl:value-of select="." /&gt;&lt;/i&gt;&lt;br /&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match="description"&gt;
  &lt;b&gt;&lt;xsl:value-of select="." /&gt;&lt;/b&gt;&lt;br /&gt;
&lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;</code></pre>
<p>We add the last parameter to xslt_process() which is an array of key-value pairs stored in an associative array. A small problem is that xslt_process() will not accept a NULL instead of the optional fifth argument. Just give it an empty array and all will be fine.</p>
<pre><code>&lt;?php
$params = array('locale' =&gt; 'sv');
$xp = xslt_create();

$result = xslt_process($xp, 'mylibrary.xml', 'libraryhtml.xsl',
                       NULL, array(), $params);
echo $result;

xslt_free($xp);
?&gt;</code></pre>
<h3>Conclusion</h3>
<p>The concept presented above in all its simplicity is suitable for even the largest projects. The logic to create and/or retrieve the data will be completely separated from the presentation layer. It would be easy to add code in the logic to detect the browser type and apply different xsl templates for wml or html.</p>
<p>PHP mixed with HTML is a bad choice when creating anything more complex than a very small web site. Some prefer to use FastTemplates or another template implementation in PHP, which might be good for some needs. However, both XHTML and WML are based on XML which makes XSLT an even more natural choice for the web. XSLT is rich in features and extremely powerful in transforming XML to other formats. Not to mention that XSLT is a standard.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://www.dotvoid.com/2002/08/putting-xsl-and-php-to-work/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://www.dotvoid.com/2002/08/putting-xsl-and-php-to-work/&amp;title=Putting+XSL+and+PHP+to+work" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.dotvoid.com/2002/08/putting-xsl-and-php-to-work/&amp;t=Putting+XSL+and+PHP+to+work" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://www.dotvoid.com/2002/08/putting-xsl-and-php-to-work/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://www.dotvoid.com/2002/08/putting-xsl-and-php-to-work/&amp;title=Putting+XSL+and+PHP+to+work&amp;summary=%0D%0A%0D%0AWe%20all%20know%20PHP%20is%20great%20for%20web%20development.%20We%20also%20know%20that%20XSLT%20is%20a%20great%20way%20of%20formatting%2C%20or%20tranforming%2C%20XML%20data.%20Combine%20these%20technologies%20and%20we%20have%20a%20very%20powerful%20toolkit%20in%20developing%20large%20scale%20web%20sites%20where%20content%20is%20clearly%20separated%20from%20logic%20and%20presentation.%20This%20art&amp;source=dotvoid.com" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-plaxo">
			<a href="http://www.plaxo.com/?share_link=http://www.dotvoid.com/2002/08/putting-xsl-and-php-to-work/" rel="nofollow" class="external" title="Share this on Plaxo">Share this on Plaxo</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://www.dotvoid.com/2002/08/putting-xsl-and-php-to-work/&amp;title=Putting+XSL+and+PHP+to+work" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Putting+XSL+and+PHP+to+work+-+http://b2l.me/wtybr&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.dotvoid.com/2002/08/putting-xsl-and-php-to-work/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick and dirty XSLT tutorial</title>
		<link>http://www.dotvoid.com/2002/08/quick-and-dirty-xslt-tutorial/</link>
		<comments>http://www.dotvoid.com/2002/08/quick-and-dirty-xslt-tutorial/#comments</comments>
		<pubDate>Fri, 02 Aug 2002 13:02:25 +0000</pubDate>
		<dc:creator>Danne</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[xpath]]></category>
		<category><![CDATA[xsl]]></category>
		<category><![CDATA[xslt]]></category>

		<guid isPermaLink="false">http://www.commodi.com/?p=18</guid>
		<description><![CDATA[
XSLT is a powerful way of getting data from an XML document into a HTML document, altough it is not the only use for it. This article will show you how to get started with XSLT using a simple example while explaining the basics of the language.
Explanation
The stylesheet above introduces many important concepts of XSLT [...]]]></description>
			<content:encoded><![CDATA[<div class="preamble">
<p>XSLT is a powerful way of getting data from an XML document into a HTML document, altough it is not the only use for it. This article will show you how to get started with XSLT using a simple example while explaining the basics of the language.</p></div>
<h3>Explanation</h3>
<p>The stylesheet above introduces many important concepts of XSLT and how it works. The first line is the XML declaration. The <code>&lt;xsl:stylesheet ...&gt;</code> and <code>&lt;/xsl:stylesheet&gt;</code> marks the start and end of the stylesheet. Other than that there is for now no need to explain that further. Another thing that is important is that XSLT is XML. And thus needs to be wellformed. This means that all tags, whether HTML or XSLT, needs to be closed. This is done either by a start and end tag like <code>&lt;li&gt;item&lt;/li&gt;</code> or by using a short form like <code>&lt;xsl:value-of select="title"/&gt;</code> (Note the forward slash in the end).</p>
<p>The stylesheet contain three templates, or rules. These match certain patterns like &#8220;item&#8221; or &#8220;news&#8221;. The first rule is special. It matches the root in the XML document. You can look at it as you are traversing a tree of nodes.</p>
<pre><code> root node (/)
     '- news
         +- source
         +- item
         |   +- title
         |   '- link
         '- item
             +- title
             '- link</code></pre>
<p>The XSLT tag <code>&lt;xsl:apply-templates/&gt;</code> tell the processor to look at the XML and the template rules to find matches. So in the root node above we have <code>&lt;xsl:apply-templates select="news"/&gt;</code>. That tell the processor to look for news elements in the XML document.</p>
<p>Getting the information Four different ways of displaying information from the XML is used above. The normal way of getting the value of an XML tag is to use <code>&lt;xsl:value-of select="xml-node"/&gt;</code>. In the news template we use <code>&lt;xsl:value-of select="source"/&gt;</code> to get the value of &#8220;source&#8221; printed.</p>
<p>In the same template we also use a function to count the number of &#8220;item&#8221; found in the XML using the function count(). There are many such functions available in XSLT, for formatting, sorting, etc.</p>
<p>In the item template we want to create an html link using the value of the XML tag &#8220;link&#8221;. Since we cannot use <code>&lt;xsl:value-of ../&gt;</code> in html attributes, in this case HREF, we need to enclose the name of the node we want in curly braces {} and enter that as the value of HREF.</p>
<p>Last we want the value of the date. Date is an attribute of the XML tag &#8220;item&#8221;. To select the value of an attribute in the XML you put a @ in front of the name.</p>
<p>If you look at the tree above and think about the templates. I have used only the simple names of nodes to get their values. Since the XML really is a tree, like a directory structure, you can write full paths directly. Names are separated by forward slash. If you in the root node wanted the value of the XML tag &#8220;source&#8221; you could write the template like below.</p>
<pre><code>&lt;xsl:template match="/"&gt;
  &lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;My news&lt;/title&gt;
  &lt;/head&gt;

  &lt;body bgcolor="#ffffff"&gt;
    &lt;xsl:value-of select="news/source"/&gt;.
    &lt;xsl:apply-templates select="news"/&gt;
  &lt;/body&gt;
  &lt;/html&gt;
&lt;/xsl:template&gt;</code></pre>
<h3>What is XSLT</h3>
<p>Although people often only say XSL they usually refer to XSLT in conjunction with XPath. This tutorial won&#8217;t go into the details of these matters. Instead I will show you by example how to get started with XSLT to produce your HTML documents.</p>
<p>XSLT, however, stands for Extensible Stylesheet Language Transformation which was standardized by W3C in 1999. XSLT is a programming language and a stylesheet written in XSLT allow you to transform XML data into HTML or another text based format. Another way of looking at XSLT is that it is a way of extracting the information from XML data.</p>
<p>It is different than conventional languages in that XSLT is a rule driven language. You create template rules that match a certain pattern in XML data that is processed. Within the templates you define what output should be the result of the XML element.</p>
<h3>A small example</h3>
<p>Let us start with the XML document containing information and how we want that information displayed in a html document. Here is an XML document, more specifically a small list of news items.</p>
<pre><code>
&lt;?xml version="1.0"?&gt;
&lt;news&gt;
  &lt;source&gt;dotvoid.com&lt;/source&gt;
  &lt;item type="technology" date="20010323"&gt;
    &lt;title&gt;New tutorial&lt;/title&gt;
    &lt;link&gt;http://www.dotvoid.com&lt;/link&gt;
  &lt;/item&gt;
  &lt;item type="economy" date="20010322"&gt;
    &lt;title&gt;Nasdaq Hausse&lt;/title&gt;
    &lt;link&gt;http://no.way.void&lt;/link&gt;
  &lt;/item&gt;
&lt;/news&gt;
</code></pre>
<p>So we have a few news items which we now want to see nicely formatted on a web page. Lets begin with creating the end result of what we want. A simple but nicely formatted list of links to the news source.</p>
<pre><code>&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;My news&lt;/title&gt;
  &lt;/head&gt;

  &lt;body bgcolor="#ffffff"&gt;
    2 news items from dotvoid.com:

    &lt;ul&gt;
    &lt;li&gt;&lt;a HREF="http://www.dotvoid.com"&gt;New tutorial&lt;/a&gt;
        (20010323)&lt;/li&gt;
    &lt;li&gt;&lt;a HREF="http://no.way.void"&gt;Nasdaq Hausse&lt;/a&gt;
        (20010333)&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/body&gt;
&lt;/html&gt;
</code></pre>
<h3>Creating the XSLT</h3>
<p>The XSLT to create the web page above will then lool like this.</p>
<pre><code>&lt;?xml version="1.0"?&gt;
&lt;xsl:stylesheet
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     version="1.0"&gt;

&lt;xsl:template match="/"&gt;
  &lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;My news&lt;/title&gt;
  &lt;/head&gt;

  &lt;body bgcolor="#ffffff"&gt;
    &lt;xsl:apply-templates select="news"/&gt;
  &lt;/body&gt;
  &lt;/html&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match="news"&gt;
  &lt;xsl:value-of select="count(item)"/&gt;
  news items from
  &lt;xsl:value-of select="source"/&gt;:

  &lt;ul&gt;
     &lt;xsl:apply-templates select="item"/&gt;
  &lt;/ul&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match="item"&gt;
  &lt;li&gt;
    &lt;a href="{link}"&gt;&lt;xsl:value-of select="title"/&gt;&lt;/a&gt;
    (&lt;xsl:value-of select="@date"/&gt;)
  &lt;/li&gt;
&lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;
</code></pre>
<h3>How to run the examples</h3>
<p>There are many, both free and commercial, XSLT processors available. I mostly use Sablotron from Ginger Alliance. Mainly because that is the one used in the PHP scripting language. It comes with a simple command line interface and is available for Linux, NT/2000, Solaris along with a few more unices. You can read more about it and download it at www.gingerall.com.</p>
<p>To test the examples above you save the XSL and XML into two files. For example news.xml and mypage.xsl. Then run it using</p>
<pre><code>sabcmd mypage.xsl news.xml
</code></pre>
<h3>Rounding off</h3>
<p>I hope this is a good starter on how you can use XSLT and XML. XSLT can at an easy way of separating content from design. It can also be utilized in building complex systems that produce content for a multitude of devices such as mobile phones and PDA&#8217;s. Using XML and XSLT you produce content once and apply different stylesheets for different devices.</p>
<p>If you want to ask questions or discuss this article do so in XML/XSLT forum here on dotvoid.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-comfeed">
			<a href="http://www.dotvoid.com/2002/08/quick-and-dirty-xslt-tutorial/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://www.dotvoid.com/2002/08/quick-and-dirty-xslt-tutorial/&amp;title=Quick+and+dirty+XSLT+tutorial" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.dotvoid.com/2002/08/quick-and-dirty-xslt-tutorial/&amp;t=Quick+and+dirty+XSLT+tutorial" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://www.dotvoid.com/2002/08/quick-and-dirty-xslt-tutorial/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://www.dotvoid.com/2002/08/quick-and-dirty-xslt-tutorial/&amp;title=Quick+and+dirty+XSLT+tutorial&amp;summary=%0D%0A%0D%0AXSLT%20is%20a%20powerful%20way%20of%20getting%20data%20from%20an%20XML%20document%20into%20a%20HTML%20document%2C%20altough%20it%20is%20not%20the%20only%20use%20for%20it.%20This%20article%20will%20show%20you%20how%20to%20get%20started%20with%20XSLT%20using%20a%20simple%20example%20while%20explaining%20the%20basics%20of%20the%20language.%0D%0AExplanation%0D%0AThe%20stylesheet%20above%20introduces%20many%20&amp;source=dotvoid.com" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
		<li class="shr-plaxo">
			<a href="http://www.plaxo.com/?share_link=http://www.dotvoid.com/2002/08/quick-and-dirty-xslt-tutorial/" rel="nofollow" class="external" title="Share this on Plaxo">Share this on Plaxo</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://www.dotvoid.com/2002/08/quick-and-dirty-xslt-tutorial/&amp;title=Quick+and+dirty+XSLT+tutorial" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Quick+and+dirty+XSLT+tutorial+-+File: /data/app/webapp/functions.php<br />Line: 66<br />Message: Duplicate entry 'wt34H' for key 'code'&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://www.dotvoid.com/2002/08/quick-and-dirty-xslt-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

