[xsl] Why Should I Do It This Way ?

Subject: [xsl] Why Should I Do It This Way ?
From: "Kerry, Richard" <richard.kerry@xxxxxxxxxxx>
Date: Fri, 15 Apr 2011 12:08:29 +0200
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