RE: [xsl] Repetitive variable definitions

Subject: RE: [xsl] Repetitive variable definitions
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 14 Sep 2007 14:37:06 -0400
Dan,

In XSLT 1.0 I might use a template in a mode to do the color assignment I wanted for the tr and td elements. Then I would only have to count columns in those templates (maybe one template could do for both) and in the template matching the table.

In XSLT 2.0, I might write a function that returned the column count of a table for any element inside it.

Either way, I might find this XPath expression to be useful:

ancestor-or-self::table/tr[1]/td[last()]/@col

Also, depending on how snarly it got I might simply avoid assigning variables altogether, and just do the XPath operations directly. (There's often not much point to declaring a variable you use only once.)

Note that certain assumptions are being made here about the input. Any cells spanning columns might throw this off. Similarly, let's hope you're not concerned about special requirements for tables inside tables.

I hope this helps,
Wendell

XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="example.xsl"?>
<example>
    <table desc="Example: 1 col table">
        <tr>
            <td col="1">col 1</td>
        </tr>
    </table>
    <table desc="Example: 2 col table">
        <tr>
            <td col="1">col 1</td>
            <td col="2">col 2</td>
        </tr>
    </table>
    <table desc="Example: 3 col table">
        <tr>
            <td col="1">col 1</td>
            <td col="2">col 2</td>
            <td col="3">col 2</td>
        </tr>
    </table>
</example>

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
    <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"/>
        </table>
     </xsl:template>


<xsl:template match="tr"> <xsl:variable name="noofcols" select="self::tr[1]/td[last()]/@col"/> <tr> <xsl:choose> <xsl:when test="$noofcols = 1"> <xsl:attribute name="bgcolor">red</xsl:attribute> </xsl:when> <xsl:otherwise> </xsl:otherwise> </xsl:choose> <xsl:apply-templates select="td"/> </tr> </xsl:template>


<xsl:template match="td"> <xsl:variable name="noofcols" select="parent::tr[1]/td[last()]/@col"/> <td> <xsl:choose> <xsl:when test="$noofcols = 3"> <xsl:attribute name="bgcolor">blue</xsl:attribute> </xsl:when> </xsl:choose> td col variable = <xsl:value-of select="$noofcols"/> </td> </xsl:template>

</xsl:stylesheet>


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread