Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT: storing enumerated values in variables 1

Status
Not open for further replies.

goaway1234

Programmer
Jun 2, 2004
78
0
0
US
I am relatively new to XSLT, so i hope this isn't too noobish.

The task at hand is creating a web page where the result of some transform is displayed in a table, which has column headers that users can click to sort it. What I have is currently working, and for simplicity I removed all but one column and some javascript:

Code:
    <xsl:template match="//current-issues">
        <table style="width:100%">
            <thead>
                <tr>
                    <td class="clickable" style="text-decoration: underline" id="reportedHeader_{$status}">Reported Date</td>
                </tr>
            </thead>
            <tbody>
                <xsl:choose>
                    <xsl:when test="$sort-direction='ascending'">
                        <xsl:apply-templates select="current-issues/i">
                            <xsl:sort data-type="number" select="@reported-sort" order="ascending"/>
                        </xsl:apply-templates>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="current-issues/i">
                            <xsl:sort data-type="number" select="@reported-sort" order="descending"/>
                        </xsl:apply-templates>
                    </xsl:otherwise>
                </xsl:choose>
            </tbody>
        </table>
    </xsl:template>

I had to throw in an extraneous <xsl:choose> and commit a copy-paste style crime to sort the data ascending or descending depending on the value of the $sort-direction variable.

Is there any way to store the value of the sort order in a variable and eliminate the xsl:choose element? The most intuitive and clean solution gives a parse error:

Code:
    <xsl:template match="//current-issues">
        <table style="width:100%">
            <thead>
                <tr>
                    <td class="clickable" style="text-decoration: underline" id="reportedHeader_{$status}">Reported Date</td>
                </tr>
            </thead>
            <tbody>
                <xsl:apply-templates select="current-issues/i">
                    <xsl:sort data-type="number" select="@reported-sort" order="$sort-direction"/>
                </xsl:apply-templates>
            </tbody>
        </table>
    </xsl:template>
 
You are almost there. The order attribute is interpreted as an attribute value template. (Also data-type, lang and case-order as shown here.)

This means that you may place an XPath expression inside curly braces, and that expression in turn is evaluated, converted to string (as if the string function were used), and used as the value of the attribute. (Note that attribute value templates are not universally applicable for all attributes in XSLT.)

So, here is the line you need:
Code:
<xsl:sort data-type="number" select="@reported-sort" order="[COLOR=blue white]{[/color]$sort-direction[COLOR=blue white]}[/color]"/>

[small]Note to tsuji: Am I learning? [bigsmile][/small]

Tom Morrison
 
[small]Note to tm: 100%... oh, I mean myself!...[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top