RE: [xsl] Using vars

Subject: RE: [xsl] Using vars
From: "Passin, Tom" <tpassin@xxxxxxxxxxxx>
Date: Mon, 26 Jul 2004 14:55:44 -0400
> From: xptm [mailto:xptm@xxxxxxx]
> In terms of performance, what will be the best way to do this?
> <Formatos>
>   <Forms>
>     <Form Nome="ProcessoTipoGrupoListaDoc">
>       <Grids>
>         <Grid Nome="ProcessoTipoGrupoLista_Grid"
> SourceObject="ProcessoTipoGrupoLista_Grid">
>           <Zooms>
>             <Zoom Coluna="ProcessoTipoGrupo">
>               <FormZoom>ProcessoTipoGrupoDoc</FormZoom>
>               <ModoZoom>AZ</ModoZoom>
>               <ParametroZoom>ProcessoTipoGrupo</ParametroZoom>
>             </Zoom>
>           </Zooms>
>         </Grid>
>       </Grids>
>       (*/ n Grids/ *)
>     </Form>
>   </Forms>
> </Formatos>
> should i use one variable like

I would do neither (see below).

> <xsl:template name="StandardEvents">
>   <Events>
>     <xsl:variable name="pages"
> )='FormZoom'
> or name()='ParametroZoom']" />
>     <Event method="tablemouse" type="MouseHandler">
>             <xsl:attribute name="target">GridPanel</xsl:attribute>
>             <xsl:attribute name="next">
>               <xsl:for-each select="$pages">
>                 <xsl:if test="name()='FormZoom'">
>                   <xsl:value-of select="." />
>                   <xsl:text>:</xsl:text>
>                 </xsl:if>
>               </xsl:for-each>
>             </xsl:attribute>
>             <xsl:attribute name="params">
>               <xsl:for-each select="$pages">
>                 <xsl:if test="name()='ParametroZoom'">
>                   <xsl:value-of select="." />
>                   <xsl:text>:</xsl:text>
>                 </xsl:if>
>               </xsl:for-each>
>             </xsl:attribute>
>     </Event>
>   </Events>
> </xsl:template>

> or two vars, like
> [2nd example snipped]

First, you can simplify the basic template.  For example
/*[name()="xxx"] is the same as simply /xxx. For the fixed value
attributes, you do not need to use xsl:attribute, just use literal
result fragments.  You do not need the xsl:text elements, either.

Second, the design somewhat depends on whether you may sometimes have
more than "FormZoom" elements etc., in a "Zoom" element.  From your
design, it looks like you expect to have more than one, and to use the
":" to separate them.  If this is not the case, the template can be
simplified more.

Here is how I would probably do it -

<xsl:template name="StandardEvents">
  <xsl:variable name='zoom-base'

  <xsl:variable name='pages' select="$zoom-base/FormZoom"/>
  <xsl:variable name='parms' select="$zoom-base/ParametroZoom"/>
    <Event method="tablemouse" type="MouseHandler" target='GridPanel'
      <xsl:attribute name="next">
        <xsl:for-each select="$pages">
          <xsl:value-of select="." />:
      <xsl:attribute name="params">
        <xsl:for-each select="$parms">
          <xsl:value-of select="." />:

If there will never be more than one FormZoom, etc., element in a Zoom
element, then I would use this -

    <Event method="tablemouse" type="MouseHandler" target='GridPanel'
       Next='{$pages}:' params='{$parms}:'/>

As for which is faster, you always have to test.  If the processor has
to evaluate /*[name()="xxx"], that will be slower than telling it /xxx
directly.   OTOH, the processor might detect that the two are the same
and optimize away the difference.  Your second method does require a
long xpath expression to be evaluated twice, which is why I used the
working variable zoom-base above.  But you never know - perhaps some
processor could determine that the base part of the two xpath
expressions is the same and re-use it.

However, separating the two expressions into two cleanly written
variables makes the code easier to read and to maintain.  That's worth

If you only use xsl:element and xsl:attribute when you absolutely need
to, your code will also be easier to read and maintain.  The only reason
to use them is to insert calculated element or attribute names, sonly
use them for that purpose.

Current Thread
  • [xsl] Using vars
    • xptm - Mon, 26 Jul 2004 19:09:24 +0100
      • <Possible follow-ups>
      • Passin, Tom - Mon, 26 Jul 2004 14:55:44 -0400 <=
      • xptm - Tue, 27 Jul 2004 14:00:55 +0100