Re: [xsl] Using keys in templates

Subject: Re: [xsl] Using keys in templates
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 13 Jul 2004 15:36:15 +0100
Hi Nicolas,

> 	I'm finding I'm using more and more constructs like :
>
> <xsl:key name="dists" match="/release/module/dist" use="@name"/>
>
> <xsl:template match="module/dist">
>  <xsl:variable name="dist" select="@name"/>
>  <xsl:if test="generate-id(.) = generate-id(key('dists',$dist))">
>    something
>  </xsl:if>
> </xsl:template>
>
> 	Would it be possible to kill the if using a syntax like :
>
> <xsl:template match="module/dist[some test]" priority="1">
> </xsl:template>
>
> <xsl:template match="module/dist" priority="0"/>

Yes -- you could use:

  match="module/dist[generate-id(.) =
                     generate-id(key('dists', @name))]"

but it would probably be better to only select the <dist> elements
that you want to process when you do the <xsl:apply-templates>, as in:

<xsl:template match="module">
  <xsl:apply-templates
    select="dist[generate-id(.) =
                 generate-id(key('dists', @name))]" />
</xsl:template>

<xsl:template match="dist">
  <!-- this template only processed for the first dist element with
       each particular name -->
  ...
</xsl:template>

Locating which template to use to process a node takes time, so you
should try to apply templates to as small a set as possible rather
than trying to find a template for every node, but only doing
something with some of them.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Current Thread