http://hades.name/blog/tag/xml/Hades Blag blog posts with tag intersection xml2010-01-17T22:30:17ZEdwarddjango-atompubhttp://hades.name/blog/2010/01/03/simple-tools-simple-tasks/Simple Tools for Simple Tasks2010-01-17T22:30:17Z2010-01-03T21:13:48Z<p>I&#8217;d like to tell you today what simple tools there are to make your life easier. Of course, a hammered screw holds better than a screwed nail, but it is much more fun to use appropriate tools for their jobs. In the long run at&nbsp;least.</p> <p>Imagine you want to create a simple photogallery for your site to tell your readers where have you been this summer, what have you seen and what have you done. Ideally it would be a simple page with a title (the place you visited) and large photos with your short comments. You start with a simple html, like&nbsp;this:</p> <div class="code"><span class="hl kwa">&lt;html&gt;</span><br /> &nbsp;<span class="hl kwa">&lt;head&gt;&lt;title&gt;</span>Saint-Petersburg<span class="hl kwa">&lt;/title&gt;&lt;/head&gt;</span><br /> &nbsp;<span class="hl kwa">&lt;body&gt;</span><br /> &nbsp;&nbsp;<span class="hl kwa">&lt;h1&gt;</span>Saint-Petersburg<span class="hl kwa">&lt;/h1&gt;</span><br /> &nbsp;&nbsp;<span class="hl kwa">&lt;p&gt;</span>Wow, there&#8217;s an awful lot of Zenith shops there.<span class="hl kwa">&lt;/p&gt;</span><br /> &nbsp;&nbsp;<span class="hl kwa">&lt;img</span> <span class="hl kwb">src</span>=<span class="hl str">&#8220;zenith01.jpg&#8221;</span> <span class="hl kwa">/&gt;</span><br /> &nbsp;&nbsp;<span class="hl kwa">&lt;p&gt;</span>The saleswoman seems like a gifted businessman.<span class="hl kwa">&lt;/p&gt;</span><br /> &nbsp;&nbsp;<span class="hl kwa">&lt;img</span> <span class="hl kwb">src</span>=<span class="hl str">&#8220;businessman01.jpg&#8221;</span> <span class="hl kwa">/&gt;</span><br /> &nbsp;<span class="hl kwa">&lt;/body&gt;</span><br /> <span class="hl kwa">&lt;/html&gt;</span><br /> </div> <p>But you are not a mere mortal, but a fine software developer! Or another kind of brilliant person. So you immediately found a major design flaw: wherever you will need to change the design or layout of this page, or add perhaps a <span class="caps">GPS</span> link for each photo, you would need to edit the page (or all the individual photo entries)&nbsp;manually. </p> <p>You could also use some kind of a dynamic photo gallery. But that is an obvious overkill: web applications are much more <span class="caps">CPU</span> and <span class="caps">RAM</span> hungry. Java programmers may stop reading at this point: efficiency problems never stop them. Also the web galleries usually have cumbersome point-and-click interfaces which involve too much, well, pointing and clicking for simple tasks. Enterprise programmers may stop reading at this point and join their Java&nbsp;colleagues. </p> <p>So you would write a simple, relatively fast web application that would generate something like the above code from textual descriptions of photos and a template page. That&#8217;s your only option, right? Wrong! That still might not even be an option: some sysadmins still disable <span class="caps">CGI</span> and stuff for their web servers (that includes university homepages and some of the cheapest hostings). And even if your web-app is extremely fast, static pages still seem way&nbsp;better.</p> <p>Here&#8217;s how you can have your cake and eat it. For nearly ten years now, there exists a technology called <span class="caps">XSL</span> Transformations, or <span class="caps">XSLT</span>. In short, it is a language that describes how to convert one <span class="caps">XML</span> file into another. Imagine an <span class="caps">XML</span> file, say <code>peter.xml</code>:</p> <div class="code"><span class="hl kwa">&lt;?xml</span> <span class="hl kwb">version</span>=<span class="hl str">&#8220;1.0&#8221;</span> <span class="hl kwb">encoding</span>=<span class="hl str">&#8220;<span class="caps">UTF</span>-8&#8221;</span><span class="hl kwa">?&gt;</span><br /> <span class="hl kwa">&lt;?xml-stylesheet</span> <span class="hl kwb">type</span>=<span class="hl str">&#8220;text/xsl&#8221;</span> <span class="hl kwb">href</span>=<span class="hl str">&#8220;album.xslt&#8221;</span><span class="hl kwa">?&gt;</span><br /> <span class="hl kwa">&lt;album&gt;</span><br /> &nbsp;<span class="hl kwa">&lt;title&gt;</span>Saint-Petersburg<span class="hl kwa">&lt;/title&gt;</span><br /> &nbsp;<span class="hl kwa">&lt;photo&gt;</span><br /> &nbsp;&nbsp;<span class="hl kwa">&lt;image&gt;</span>zenith01.jpg<span class="hl kwa">&lt;/image&gt;</span><br /> &nbsp;&nbsp;<span class="hl kwa">&lt;comment&gt;</span>Wow, there&#8217;s an awful lot of Zenith shops there.<span class="hl kwa">&lt;/comment&gt;</span><br /> &nbsp;<span class="hl kwa">&lt;/photo&gt;</span><br /> &nbsp;<span class="hl kwa">&lt;photo&gt;</span><br /> &nbsp;&nbsp;<span class="hl kwa">&lt;image&gt;</span>businessman01.jpg<span class="hl kwa">&lt;/image&gt;</span><br /> &nbsp;&nbsp;<span class="hl kwa">&lt;comment&gt;</span>The saleswoman seems like a gifted businessman.<span class="hl kwa">&lt;/comment&gt;</span><br /> &nbsp;<span class="hl kwa">&lt;/photo&gt;</span><br /> <span class="hl kwa">&lt;/album&gt;</span><br /> </div> <p>It looks exactly like gazillions of other <span class="caps">XML</span> files in the world. Except for the second line, which in English says &#8220;Oh, hi, you want to show this file to a user? Just apply a stylesheet at <code>album.xslt</code> and he won&#8217;t go mad. Thanks!&#8221; Here&#8217;s the <code>album.xslt</code>:</p> <div class="code"><span class="hl kwa">&lt;?xml</span> <span class="hl kwb">version</span>=<span class="hl str">&#8220;1.0&#8221;</span> <span class="hl kwb">encoding</span>=<span class="hl str">&#8220;<span class="caps">UTF</span>-8&#8221;</span><span class="hl kwa">?&gt;</span><br /> <span class="hl kwa">&lt;xsl:stylesheet</span> <span class="hl kwb">version</span>=<span class="hl str">&#8220;1.0&#8221;</span> xmlns:<span class="hl kwb">xsl</span>=<span class="hl str">&#8220;http://www.w3.org/1999/<span class="caps">XSL</span>/Transform&#8221;</span><span class="hl kwa">&gt;</span><br /> &nbsp;<span class="hl kwa">&lt;xsl:template</span> <span class="hl kwb">match</span>=<span class="hl str">&#8220;/&#8221;</span><span class="hl kwa">&gt;</span><br /> &nbsp;&nbsp;<span class="hl kwa">&lt;html&gt;</span><br /> &nbsp;&nbsp;&nbsp;<span class="hl kwa">&lt;head&gt;&lt;title&gt;&lt;xsl:value-of</span> <span class="hl kwb">select</span>=<span class="hl str">&#8220;album/title&#8221;</span> <span class="hl kwa">/&gt;&lt;/title&gt;&lt;/head&gt;</span><br /> &nbsp;&nbsp;&nbsp;<span class="hl kwa">&lt;body&gt;</span><br /> &nbsp;&nbsp;&nbsp;&nbsp;<span class="hl kwa">&lt;h1&gt;&lt;xsl:value-of</span> <span class="hl kwb">select</span>=<span class="hl str">&#8220;album/title&#8221;</span> <span class="hl kwa">/&gt;&lt;/h1&gt;</span><br /> &nbsp;&nbsp;&nbsp;&nbsp;<span class="hl kwa">&lt;xsl:for-each</span> <span class="hl kwb">select</span>=<span class="hl str">&#8220;album/photo&#8221;</span><span class="hl kwa">&gt;</span><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hl kwa">&lt;p&gt;&lt;xsl:value-of</span> <span class="hl kwb">select</span>=<span class="hl str">&#8220;comment&#8221;</span> <span class="hl kwa">/&gt;&lt;/p&gt;</span><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hl kwa">&lt;img&gt;&lt;xsl:attribute</span> <span class="hl kwb">name</span>=<span class="hl str">&#8220;src&#8221;</span><span class="hl kwa">&gt;</span><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hl kwa">&lt;xsl:value-of</span> <span class="hl kwb">select</span>=<span class="hl str">&#8220;image&#8221;</span> <span class="hl kwa">/&gt;</span><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hl kwa">&lt;/xsl:attribute&gt;&lt;/img&gt;</span><br /> &nbsp;&nbsp;&nbsp;&nbsp;<span class="hl kwa">&lt;/xsl:for-each&gt;</span><br /> &nbsp;&nbsp;&nbsp;<span class="hl kwa">&lt;/body&gt;</span><br /> &nbsp;&nbsp;<span class="hl kwa">&lt;/html&gt;</span><br /> &nbsp;<span class="hl kwa">&lt;/xsl:template&gt;</span><br /> <span class="hl kwa">&lt;/xsl:stylesheet&gt;</span></div> <p>If you look closely at it, you will see that it basically tells you (or your browser) how to make <code>peter.html</code> from <code>peter.xml</code>. And that&#8217;s it. So here are the bonuses you&nbsp;get:</p> <ul><li>both files are static, you don&#8217;t need to have a muscular web server or a friendly (or non-muscular)&nbsp;sysadmin; </li><li>if you have hundreds of photos on a single page and at some moment decide that comments should go below the photo, you&#8217;d need to swap two lines, not hundreds of&nbsp;lines; </li><li>if you visited thousands of places and at some moment decide that every page should have a copyright footer or be redesigned completely, you&#8217;d need to change one single file, not thousands of&nbsp;them; </li><li>you can even have different styles for the same set of photos. For example, you may want to show smaller images when viewed from a handheld device. Or bigger images when viewed on your 60&#8221;&nbsp;plasma; </li><li>you don&#8217;t increase the butthurterol level in your&nbsp;blood.</li></ul> <p>Next time I shall probably show you some of my deployment tricks. See you&nbsp;later!</p>