<?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>Pontus Östlund &#187; Tutorials</title>
	<atom:link href="http://www.poppa.se/blog/category/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.poppa.se/blog</link>
	<description>My blog about web development and such</description>
	<lastBuildDate>Mon, 16 Jan 2012 00:38:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The power of XSL (pt.1)</title>
		<link>http://www.poppa.se/blog/the-power-of-xsl-pt-1/</link>
		<comments>http://www.poppa.se/blog/the-power-of-xsl-pt-1/#comments</comments>
		<pubDate>Tue, 26 Dec 2006 15:27:19 +0000</pubDate>
		<dc:creator>Pontus</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[XSL/XSLT]]></category>

		<guid isPermaLink="false">http://www.poppa.se/blog/?p=37</guid>
		<description><![CDATA[I&#8217;m a lucky bastard! At work we use the beautiful Roxen CMS which utilize XSL for the entire templating system. I was a little bit familiar with XSL before we got Roxen CMS but I hadn&#8217;t really understood the power of it. It didn&#8217;t take long before I really started to love XSL.
My aim with [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a lucky bastard! At <a href='http://www.tekniskaverken.se' title='Tekniska Verken'>work</a> we use the beautiful <a href='http://www.roxen.com'>Roxen CMS</a> which utilize XSL for the entire templating system. I was a little bit familiar with XSL before we got Roxen CMS but I hadn&#8217;t really understood the power of it. It didn&#8217;t take long before I really started to love XSL.</p>
<p>My aim with this tutorial series is to shed some light on why I think XSL is so great and I hope that who ever reads this will get a more pleasant future (web development-wise that is).</p>
<p>I would like to make a note: I will not go through the fundamentals of XSL and XML here. I presume that some knowledge and preconception of programming and structure of XML already is at hand.</p>
<h2>The X in XSL</h2>
<p>As you already know, or have come to understand, XSL is used for styling or formatting, or parsing if you like, XML. XML is beautiful in that way that it is platform and device independent, standardized and read by virtually any programming language. XML data can be parsed, styled, used, displayed and so on in virtually any way and by any technique you can imagine.</p>
<p>So if any programming language can parse XML why do we need XSL? Well, the answer is quite simple: What if you by any reason have to implement your solution in a different programming language? You would have to rewrite everything and that&#8217;s not to fun. If you use XSL you can, as long as the language your moving to have an XSLT engine. So you could say that your solution becomes far more portable if it&#8217;s utilizing XSL.</p>
<h2>Let&#8217;s begin with some code</h2>
<p>Enough with the b s. In the first few examples we will use no programming language, the XSL transformation will be handled by an ordinary web browser. Both Gecko-based browsers (Mozilla*) and Internet Explorer handles client side XSL transformation. So we will only need an XML document and an XSL style sheet.</p>
<h3>The XML</h3>
<p>Here comes the XML we will start off with. I&#8217;ll use the classic discography example:</p>
<pre><code lang='xml'><?xml version="1.0" encoding="ISO-8859-1"?>
<discography>
  <artist>
    <name>Dream Theater</name>
    <country>USA</country>
    <album>

      <year>1992</year>
<tracks>8</tracks>
    </album>
    <album>

      <year>1995</year>
<tracks>11</tracks>
    </album>
  </artist>
  <artist>
    <name>Dave Matthews Band</name>
    <country>USA</country>
    <album>

      <year>1994</year>
<tracks>11</tracks>
    </album>
    <album>

      <year>1996</year>
<tracks>12</tracks>
    </album>
  </artist>
</discography></code></pre>
<p>Okey, there&#8217;s nothing strange about this. We have a structure with a depth of three nodes: <code>discography --> artist --> album</code>.</p>
<h3>The XSL</h3>
<p>First lets build an XSL skeleton. This is how we&#8217;ll start every XSL document:</p>
<pre><code lang='xsl'><?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!-- We'll insert our XSL code here -->
</xsl:stylesheet></code></pre>
<p>Every XSL shall contain the <code>xsl:stylesheet</code> definition. The <code>xmlns:xsl</code> defines what name space we&#8217;ll be using, in this case the official one from W3C. We also say that we&#8217;ll be using version 1 of XSL transform.</p>
<p>This won&#8217;t do much for us. Let&#8217;s pop some more stuff in there. You can transform an XML document into a variety of other document formats; HTML, XML, Text and so on. Which format to output is done with the top level instruction <code>xsl:output</code>. Since this will be our main template we also need to write an instruction that will match the root node of our XML document (that is the discography node). To match a node we use <code>xsl:template</code> with the attribute match.</p>
<pre><code lang='xsl'><?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!-- Output as HTML -->
  <xsl:output method="html"/>

  <!-- Match the root of our XML document -->
  <xsl:template match="/">
    <!- - This is where we'll start our HTML code -->
<h1>Code goes here</h1>

  </xsl:template>
</xsl:stylesheet></code></pre>
<p>Before we do an initial test we need to assign the XSL to the XML. This is done in a similar way as assigning a CSS to an HTML document (<code>
<link rel="stylesheet"...></code>). To assign an XSL to an XML we need to &#8220;import&#8221; the XSL in the XML:</p>
<pre><code lang='xml'><?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="the-xsl.xsl"?>
<discography>
  <!-- ... -->
</discography></code></pre>
<p>Okey, so lets save the XML as <code>discography.xml</code> and the XSL as <code>discography.xsl</code> in a folder and load the XML in our browser. The result should look something like below:</p>
<p><img src='/blog/data/images/tutorials/xslt/xsl-tut-pt1-1.png' alt='Example 1' title='' /></p>
<h2>Game, set xsl:template match</h2>
<p>Okey, the first output here is not that exciting, so lets move on to some more interesting code. Let us put some HTML in our XSL template so we can see the basic structure:</p>
<pre><code lang='xsl'><?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!-- Output as HTML -->
  <xsl:output method="html"/>

  <!-- Match the root of our XML document -->
  <xsl:template match="/">
    <html>
      <head>

      </head>
      <body>
<h1>My CD Collection</h1>

        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet></code></pre>
<p>Except for the basic HTML here the one interesting thing to note here is the instruction <code>xsl:apply-templates</code>. In a way we&#8217;re gonna loop (not in the way as ordinary loops in regular programming languages, although you can do that too in XSL, but that I&#8217;ll leave for now) through the XML document and you can say that xsl:apply-templates just passes on the XML data to the next matching rule. Remember: we have <code>xsl:template match="/"</code> which matches the root of our XML document. Now we&#8217;re gonna write some more <code>xsl:template match="..."</code> instructions that will match the root&#8217;s child nodes.</p>
<p>We have the following structure: <code>discography --> artist --> album</code>. So we&#8217;ll write one <code>xsl:template match="..."</code> for each one of them. I&#8217;ll leave out the XSL skeleton now just for space reasons:</p>
<pre><code lang='xsl'><xsl:template match="discography">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="artist">
<h2>
    <xsl:value-of select="name"/>
    <xsl:text> </xsl:text>
    <small><xsl:value-of select="country"/></small>
  </h2>
<ul><xsl:apply-templates select="album"/></ul>

</xsl:template>

<xsl:template match="album">
<li>
    <strong><xsl:value-of select="title"/></strong>
    <xsl:text> </xsl:text>
    <small>(<xsl:value-of select="year"/>)</small>
    <br/>
    <xsl:value-of select="tracks"/> tracks
  </li>

</xsl:template></code></pre>
<p>So what&#8217;s happening here is that we first match the discography node and just continue applying templates. It may seem unnecessary to have this template since all we do is continue applying templates but if we had left this template out the discography node from the XML document would be output in our resulting HTML document. (There&#8217;s other ways to handle this but that&#8217;s out of the scope for now).</p>
<p>The <code>xsl:apply-templates</code> in the discography template will move us on to the artist template. The artist node have a child node named <code>name</code>. The value of this node we output with <code><xsl:value-of select="name"/></code>.  </p>
<p>To output whitespace in XSL we use <code><xsl:text> </xsl:text></code> and that&#8217;s what we&#8217;re doing to get a space between the value of the name node and the value of the country node. Now we&#8217;re done with the artist node so let&#8217;s move on to the album node by <code>xsl:apply-templates</code>. Note that I&#8217;ve added <code>select="album"</code> in the <code>xsl:apply-templates</code> here. The reason for this that when ever XSL find nodes it can not find a template for it just outputs that node into the result. If you remove the <code>select="album"</code> you&#8217;ll see that the artist name and country will be output twice. To prevent this we specify that we want to <code>apply-templates</code> for the artist node and thus we catch that node there and we&#8217;ll be fine. There are several ways to fix this but that&#8217;s a more advanced action so I leave that out for now.</p>
<p>There&#8217;s nothing new in the album template so we need no further explanation there.</p>
<h2>The entire XSL</h2>
<pre><code lang='xsl'><?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <!-- Output as HTML -->
  <xsl:output method="html"/>

  <!-- Match the root of our XML document -->
  <xsl:template match="/">
    <html>
      <head>

      </head>
      <body>
<h1>My CD Collection</h1>

        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="discography">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="artist">
<h2>
      <xsl:value-of select="name"/>
       <xsl:text> </xsl:text>
      <small><xsl:value-of select="country"/></small>
     </h2>
<ul><xsl:apply-templates select="album"/></ul>

  </xsl:template>

  <xsl:template match="album">
<li>
      <strong><xsl:value-of select="title"/></strong>
       <xsl:text> </xsl:text>
      <small>(<xsl:value-of select="year"/>)</small>
      <br/>
      <xsl:value-of select="tracks"/> tracks
    </li>

  </xsl:template>
</xsl:stylesheet></code></pre>
<p>The result of this should look something like below:</p>
<p><img src='/blog/data/images/tutorials/xslt/xsl-tut-pt1-2.png' alt='Example 2' title='' /></p>
<p>And that&#8217;s that for this time&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.poppa.se/blog/the-power-of-xsl-pt-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

