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

If vs. apply-templates for optional attributes

Subject: If vs. apply-templates for optional attributes
From: "Hunsberger, Peter" <Peter.Hunsberger@xxxxxxxxxx>
Date: Fri, 7 Jun 2002 09:32:06 -0500
 If vs. apply-templates for optional attributes
Any opinions on which of the following two ways of adding an optional
attribute to an element is more efficient?

Method 1 - If:

  <testelement>>
      <xsl:if test="@attr != ''">
         <xsl:attribute name="attr"><xsl:value-of
select="@attr"/></xsl:attribute>
      </xsl:if>
  </testelement>

Method 2 - Apply templates:

  <testelement>>
      <xsl:apply-templates mode="attr" select="."/>
  </testelement>

  <xsl:template match="*[@attr]" mode="attr">
     <xsl:attribute name="attr"><xsl:value-of
select="@attr"/></xsl:attribute>
  </xsl:template>

I have about a dozen spots where I need to do this type of test, so using
method 2 ends up reducing the size of the XSLT overall.  Also, method 2 can
easily be extended, in particular to provide a default value by adding a
template like:

  <xsl:template match="*[not(@attr)]" mode="attr">
     <xsl:attribute name="attr">default</xsl:attribute>
  </xsl:template>

where as method 1 has to be converted into a choose block.  As a result I've
been favoring method 2.  However, I thought I should make a sanity check on
whether it might have  performance implications? It seems to me that method
2 has to be somewhat less efficient, but perhaps not enough to matter? 

I'll note that one might have to be a little careful on what you push to the
modal templates in method 2 (worse case is to add a default template to
gobble up anything not wanted), and that might add other performance issues,
but in my case that's not an issue...

Peter Hunsberger

Phone: 901-495-5252
E-mail: Peter.Hunsberger@xxxxxxxxxx


 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.