Re: [xsl] XSL - avoiding code duplication

Subject: Re: [xsl] XSL - avoiding code duplication
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Mon, 26 May 2003 13:14:17 -0400
Jason,

Try this:

<xsl:template match="commonref" mode="quux">
  <xsl:variable name="attribute">
    <xsl:call-template name="setattribute">
      <xsl:with-param name="node" select="."/>
    </xsl:call-template>
  </xsl:variable>
  ...
</xsl:template>

The rest, you can probably work out.

Note that strictly speaking you don't need to pass the "node" parameter in or even have the parameter (since the current node of the called template is the same as when it's called); although to have it certainly has its advantages, and can be a very good idea (it makes the template more versatile, etc.).

Enjoy,
Wendell

At 10:07 AM 5/26/2003, you wrote:
I'm trying to cut down on the duplication of XML and XSL in my
project....

...I'm not very fond of this kind of
cut-and-paste development so I tried to write a call-template function
to manage these variables, however, if I do that the scope of the
variables doesn't reach my mode function.  For example:
<xsl:template match="commonref" mode="quux">
   <xsl:call-template name="set-vars">
       <xsl:with-param name="node" select="."/>
   </xsl:call-template>
   <xsl:value-of select="$attribute"/>
</xsl:template>
<xsl:template name="set-vars">
  <xsl:param name="node/>
  <xsl:variable name="attribute">
      <xsl:choose>
         <xsl:when test="node/@attribute">
            <xsl:value-of select="node/@attribute"/>
         </xsl:when>
         <xsl:otherwise>
            <xsl:value-of select="id(node/@idref)/@attribute"/>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:variable>
</xsl:template>
Does not work and gives an error that the variable is out of scope.
Sensible.  But my question is how to I compartmentalize this code so I
don't have to cut-and-paste it all over the place (btw there are about
5 variables I need to do this with each time -- so the c-n-p is a lot
more than it seems here).
Suggestions?
-j



__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com

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

___&&__&_&___&_&__&&&__&_&__&__&&____&&_&___&__&_&&_____&__&__&&_____&_&&_ "Thus I make my own use of the telegraph, without consulting the directors, like the sparrows, which I perceive use it extensively for a perch." -- Thoreau


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



Current Thread