This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: match on attribute anywhere
Andrew Welch wrote:
>In my xml, if an element has an attribute 'mark=1' then it should be
>highlighted. I can achieve it by putting a test on each template:
>
...
>
>However, doing this for each template seems a bit overkill, I would like to
>specify a general template like this:
>
You might try something like this:
For each element type or patterm 'x' write:
<!-- add a name to the template you already have -->
<xsl:template match="x" name="standard-x">
do whatever for element type 'x', including possibly
xsl:apply-templates.
</xsl:template>
<!-- match the same thing in a different mode, calling the template
you just wrote -->
<xsl:template match="x" mode="mark">
<xsl:call-template name="standard-x" />
</xsl:template>
By itself the above just processes the input without doing any
marking. To do the marking add:
<!-- pick up any elements with a mark attribute -->
<xsl:template match="*[@mark='1']">
<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="mark" />
</span>
</xsl:template>
The basic idea is to see that you want to process a particular element
in one of two different modes - marked or not. The call-template
trick is necessary because XSLT doesn't allow you to put more than one
mode on a template (it would be nice just to write:
<!-- NOT LEGAL -->
<xsl:template match="x" mode="#default | mark" >
I have never really understood the thinking behind not allowing this -
its just shorthand for what I wrote above. You would probably want
'sticky' modes of course - some way of saying apply-templates
mode="whatever mode you are in".)
If its a huge stylesheet you might want to consider writing a
stylesheet to write the stylesheet.
Regards,
Trevor Nash
--
Traditional training & distance learning,
Consultancy by email
Melvaig Software Engineering Limited
voice: +44 (0) 1445 771 271
email: tcn@melvaig.co.uk
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list