Re: [xsl] match on attribute anywhere

Subject: Re: [xsl] match on attribute anywhere
From: Trevor Nash <tcn@xxxxxxxxxxxxx>
Date: Thu, 14 Feb 2002 10:34:57 +0000
Chris wrote:

>The named template approaches were better, though I'm not sure they 
>directly addressed your problem.  Try this:
>
><xsl:template name="test-mark">
>   <xsl:choose>
>     <xsl:when test="@mark">
>       <span style="color:#FF0000">
>         <xsl:apply-templates/>
>       </span>
>     </xsl:when>
>     <xsl:otherwise>
>       <xsl:apply-templates/>
>     </xsl:otherwise>
>   </xsl:choose>
></xsl:template>
>
><xsl:template match="whatever">
>   <xsl:call-template name="test-mark"/>
></xsl:template>
>
>~Chris

I thought the point of the question was that there were several
'whatever-else'-s, each with *specific* processing except for the one
common bit: whatever was to be generated should be wrapped in a 'span'
to change the colour if @mark='1' was present.  Andrew?

If I am right, here is a tidier version of my first attempt:

For each element type or patterm 'x' write:

<!-- add a mode to the template you already have -->
<xsl:template match="x" mode="process">
    do whatever for element type 'x', including possibly
    xsl:apply-templates *without* a mode.
</xsl:template>

 To do the marking add:

<!-- pick up any elements with a mark attribute -->
<xsl:template match="*[@mark='1']" priority="2">
    <span style="color:#FF0000">
                  <!-- go back and do the standard thing, using 
                         a different mode so that you do
                         not recursively trigger this template -->
 	<xsl:apply-templates select="." mode="process" />
   </span>
</xsl:template>

<!-- and the others -->
<xsl:template match="*">
 	<xsl:apply-templates select="." mode="process" />
</xsl:template>

Though this version is easier to maintain (you don't have to have that
extra match+call for every template), its probably slower - it does a
template select twice for every node, where the original only did a
double select for marked nodes.  But then it had roughly twice as many
templates to look at, so it could go either way.

Regards,
Trevor Nash

--
Traditional training & distance learning,
Consultancy by email

Melvaig Software Engineering Limited
voice:     +44 (0) 1445 771 271 
email:     tcn@xxxxxxxxxxxxx

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread