Forum Moderators: open

Message Too Old, No Replies

Hyperlink in XSL

         

yuppieyash

3:21 am on Jun 28, 2005 (gmt 0)

10+ Year Member



hi,

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

choster

4:11 am on Jun 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, YY.

What is the structure of your XML?

yuppieyash

1:39 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



Thanks for the reply. The structure goes like this:

<data>
<log_entry run_time="4086" action="retrieveUserPreferencesBean" username="PROJNAV" />
<log_entry run_time="571" action="retrieveCommentsBean" username="PROJNAV"/>
</data>

Thanks,
YY

choster

4:58 pm on Jun 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, let's slow down.

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>