[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: match on attribute anywhere

Subject: Re: match on attribute anywhere
From: Trevor Nash <tcn@xxxxxxxxxxxxx>
Date: Wed, 13 Feb 2002 12:24:51 +0000
match x
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@xxxxxxxxxxxxx

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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.