Forum Moderators: open
I am converting an XML file to HTML file using XSLT. I have a table with 3 columns viz action, count, time. Once i fill the table with values, i need to sort it based on what i click. i.e. if i click on action, it should sort by action, if i click on count, it should sort by count. So how do i create hyperlink for the attributes action, count, time in XSLT and how do i monitor which attribute has been clicked so that i can sort by that attribute in XSLT? Any replies greatly appreciated.
Thanks,
anki
Assuming
1) the action is represented by the action attribute of <log_entry>
2) the time is the run_time attribute of <log_entry>
3) the count is the position of the <log_entry>
and
4) the parameter $sortkey will be defined as "time" for @run_time or "action" for @action, and ignored in other cases
then to display <data> as a table sorted on one of these three pieces of data, you'd use XSL similar to this code. How the value of $sortkey gets passed to the template depends on where the stylesheet is being called from and how your processing engine accepts parameters.
<xsl:template match="data">
<!-- reserve $sortkey; if a global parameter this line is not necessary -->
<xsl:param name="sortkey" /><!-- set up the table to display the output -->
<table>
<tr>
<th>count</th>
<th>run_time</th>
<th>action</th>
</tr><!-- process on each /data/log_entry element -->
<xsl:choose>
<xsl:when test="$sortkey = 'action'">
<xsl:apply-templates>
<xsl:sort select="@action" />
</xsl:apply-templates>
</xsl:when>
<xsl:when test="$sortkey = 'time'">
<xsl:apply-templates>
<xsl:sort select="@run_time" data-type="number" />
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise> <!-- default sortkey is count -->
<xsl:apply-templates />
</xsl:otherwise>
</xsl:choose></table>
</xsl:template><xsl:template match="log_entry">
<!-- display each log_entry in a table row -->
<tr>
<td><xsl:value-of select="position()" /></td>
<td><xsl:value-of select="@run_time" /></td>
<td><xsl:value-of select="@action" /></td>
</tr>
</xsl:template>