RE: [xsl] Repetitive variable definitions

Subject: RE: [xsl] Repetitive variable definitions
From: "Bjorndahl, Brad" <brad.bjorndahl@xxxxxxxxxxxxxxxx>
Date: Fri, 14 Sep 2007 14:35:34 -0400
Hi,

Try this. Pass 'noofcols' as a parameter. I think the best you can do in 1.0
is calculate it once for each table and pass it to each template that needs
it.
In 2.0, tunnel parameters allow you to declare the parameter only where you
need the value.

I also made a few changes to define attribute values better and produce valid
html.

Brad

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
  <xsl:output method="html" indent="yes" />
    <xsl:template match="/" >
      <html>
      <xsl:apply-templates />
      </html>
    </xsl:template  >

    <xsl:template match="table">
        <xsl:variable name="noofcols"  select="tr/td[last()]/@col"/>
          <table border="1">
              <tr>
                  <td align="center"  colspan="{$noofcols}">
                      <xsl:value-of select="@desc"/>
                  </td>
              </tr>
              <xsl:apply-templates select="tr">
                <xsl:with-param name="noofcols" select="$noofcols" />
              </xsl:apply-templates>
          </table>
     </xsl:template>

    <xsl:template match="tr">
        <xsl:param name="noofcols" />
        <tr>
          <xsl:attribute name="bgcolor">
            <xsl:choose>
                <xsl:when test="$noofcols = 1">red</xsl:when>
                <xsl:otherwise></xsl:otherwise>
            </xsl:choose>
          </xsl:attribute>
          <xsl:apply-templates select="td">
            <xsl:with-param name="noofcols" select="$noofcols" />
          </xsl:apply-templates>
        </tr>
    </xsl:template>


    <xsl:template match="td">
        <xsl:param name="noofcols" />
        <td>
          <xsl:attribute name="bgcolor">
          <xsl:choose>
              <xsl:when test="$noofcols = 3">blue</xsl:when>
          </xsl:choose>
        </xsl:attribute>
        td col variable = <xsl:value-of select="$noofcols"/>
      </td>
    </xsl:template>

</xsl:stylesheet>

Current Thread