[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Why Should I Do It This Way ?

Subject: Re: Why Should I Do It This Way ?
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Fri, 15 Apr 2011 11:50:24 +0100
Re:  Why Should I Do It This Way ?
You're right that the "push" style (using apply-templates) is the style that "goes with the flow" of XSLT as a language, and is therefore usually recommended to students.

Of course both work, and for a simple task, both work equally well. The benefits of the "push" style include the following:

* the pull style becomes more and more unwieldy and unreadable as the size and complexity of the transformation increases; the push style scales better.

* the push style makes it easier to change the stylesheet when the source data structure changes

* the push style gives you code that is more reusable: you can create a customized version of the code that does something slightly different by selectively overriding template rules.

Probably the main reason for recommending the push style to beginners is that they need to learn how to use template rules because they will need them eventually. They might not be needed for the simple transfomations that beginners are asked to write, but they will soon be needed when it comes to doing more serious work.

Michael Kay
Saxonica


On 15/04/2011 11:08, Kerry, Richard wrote:

Team,


A colleague, who is not on this mailing list, has recently written a couple of XSL stylesheets.
Here's one of them (device_description.xslt):

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
   <html>
   <body>
   <h1>
       <xsl:for-each select="device_description">
        <td><xsl:value-of select="@name"/></td>
        </xsl:for-each>
        </h1>
     <table border="1">
       <tr bgcolor="#9acd32">
         <th>Name</th>
         <th>Class/Style</th>
         <th>Access</th>
         <th>Slot</th>
         <th>Detail</th>
       </tr>
       <xsl:for-each select="device_description/param">
       <tr>
         <td><xsl:value-of select="@name"/></td>
         <td><xsl:value-of select="@class"/>  /<xsl:value-of select="@style"/></td>
         <td><xsl:value-of select="@access"/></td>
         <td><xsl:value-of select="@slot"/></td>
  <td>
  <xsl:for-each select="state">
          <xsl:value-of select="@value"/>  =<xsl:value-of select="@caption"/>  <br />
        </xsl:for-each>
  <xsl:for-each select="values">
          min =<xsl:value-of select="@min"/><br />
          max =<xsl:value-of select="@max"/><br />
          step =<xsl:value-of select="@step"/><br />
          bigstep =<xsl:value-of select="@bigstep"/><br />
          default =<xsl:value-of select="@defaultvalue"/><br />
        </xsl:for-each>
  <xsl:for-each select="display">

  <br />Display as:<br />
          (<i>value</i>  +<xsl:value-of select="@inoffset"/>) *<xsl:value-of select="@multiplier"/>  +<xsl:value-of select="@outoffset"/>  <xsl:value-of select="@units"/><br />
        </xsl:for-each>
       </td>
       </tr>
       </xsl:for-each>
     </table>
   </body>
   </html>
</xsl:template>
</xsl:stylesheet>


This will take an XML file like this (this is an extract, not the whole thing) :


<?xml version = '1.0'?>
<?xml-stylesheet type="text/xsl" href="device_description.xslt"?>
<!DOCTYPE device_description SYSTEM "device_description.dtd">
<device_description name="arc20">
     <param class="enum" access="readonly" style="button" name="SDI_Input" slot="8">
         <state caption="Missing" style="" value="0" msg="" />
         <state caption="Present" style="" value="1" msg="" />
     </param>
     <param class="enum" access="readwrite" style="button" name="Filter" slot="9">
         <state caption="Video" style="" value="0" msg="" />
         <state caption="Film" style="" value="1" msg="" />
         <state caption="Mixed" style="" value="2" msg="" />
         <state caption="Film Eff" style="" value="3" msg="" />
     </param>
     <param class="range" access="readwrite" style="" name="Pan" slot="11">
         <values defaultvalue="0" step="1" min="-180" max="180" bigstep="10" />
         <display inoffset="0" units="  px" outoffset="0" dp="" multiplier="1" />
     </param>
     <param class="range" access="readwrite" style="" name="Tilt" slot="12">
         <values defaultvalue="0" step="1" min="-100" max="100" bigstep="10" />
         <display inoffset="0" units="  px" outoffset="0" dp="" multiplier="1" />
     </param>
     <param class="range" access="readwrite" style="" name="H_Blank" slot="13">
         <values defaultvalue="0" step="1" min="0" max="190" bigstep="10" />
         <display inoffset="0" units="  px" outoffset="0" dp="" multiplier="1" />
     </param>
     <param class="enum" access="readwrite" style="" name="Comms" slot="30">
         <state caption="Fail" style="" value="0" msg="" />
         <state caption="OK" style="" value="1" msg="" />
     </param>
</device_description>


And convert it into an html equivalent.


I hadn't seen this until recently and when I did I thought "it shouldn't be written like that, it should be more like this" (device_description.xsl):

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
   <html>
   <body>
   <h1>
<xsl:apply-templates mode="names"/>
        </h1>
     <table border="1">
       <tr bgcolor="#9acd32">
         <th>Name</th>
         <th>Class/Style</th>
         <th>Access</th>
         <th>Slot</th>
         <th>Detail</th>
       </tr>
<xsl:apply-templates mode="params"/>
     </table>
   </body>
   </html>
</xsl:template>

<xsl:template match="device_description" mode="names">
        <td><xsl:value-of select="@name"/></td>
</xsl:template>

<xsl:template match="device_description/param" mode="params">
<tr>
<td><xsl:value-of select="@name"/></td>
<td><xsl:value-of select="@class"/>  /<xsl:value-of select="@style"/></td>
<td><xsl:value-of select="@access"/></td>
<td><xsl:value-of select="@slot"/></td>
<td>
<xsl:apply-templates />
</td>
</tr>
</xsl:template>

<xsl:template match="state">
<xsl:value-of select="@value"/>  =<xsl:value-of select="@caption"/>  <br />
</xsl:template>

<xsl:template match="values">
min =<xsl:value-of select="@min"/><br />
max =<xsl:value-of select="@max"/><br />
step =<xsl:value-of select="@step"/><br />
bigstep =<xsl:value-of select="@bigstep"/><br />
default =<xsl:value-of select="@defaultvalue"/><br />
</xsl:template>

<xsl:template match="display">
<br />Display as:<br />
(<i>value</i>  +<xsl:value-of select="@inoffset"/>) *<xsl:value-of select="@multiplier"/>  +<xsl:value-of select="@outoffset"/>  <xsl:value-of select="@units"/><br />
</xsl:template>

</xsl:stylesheet>


I haven't yet discussed this with my colleague, but when (if) I do so he'll say "Why should it ?", and I don't think I know the answer. Can the list members (assuming you agree with me) provide me with a summary of why the XSL stylesheet should be written in the latter style.

And would I be right that the file's extension should be .xsl and not .xslt ?

Regards,
Richard.





Richard Kerry
Colledia Control Engineer
Siemens IT Solutions and Services Ltd
Room 457 Drama Building, BBC Television Centre, Wood Lane, London, W12 7RJ
T: +44 (0)20 82259063 F: +44 (0)20 8576 8182 M: +44 (0)7921 244993
Email: richard.kerry@xxxxxxxxxxx<blocked::mailto:richard.kerry@xxxxxxxxxxx>
Website:  www.siemens.co.uk/it-solutions<blocked::outbind://47/www.siemens.co.uk/it-solutions>

This e-mail contains confidential information and is for the exclusive use of the addressee/s. If you are not the addressee, then any distribution, copying or use of this e-mail is prohibited. If received in error, please advise the sender and delete it immediately. We accept no liability for any loss or damage suffered by any person arising from use of this e-mail.

Siemens IT Solutions and Services Limited
Registered No: 1203466 England
Registered Office: Faraday House, Sir William Siemens Square, Frimley, Camberley, GU16 8QD

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.