Sorting data in a HTML file using XML and XSLT

Hi,

This example is working fine without sorting. But now I want to sort the data by clicking on the heading of each column.
For example if I click on "days" heading, all the data should be sorted accordingly. If I click on "letter" heading, all the data should be sorted accordingly. How do I do that in my XSLT.
I am using a simple java class to transform into a HTML file. I am pasting my XML, XSLT & JAVA class for applying XSLT.  What do I have to do to sort the data.


<?xml version="1.0" encoding="utf-8"?>
<Messages_Report category="XSLT">
     <messageDetails>
          <days>30</days>
          <msgid>MSGA</msgid>
          <letter>10</letter>
          <message>THANKYOU</message>
          <status>ENABLE</status>
          <lastuser>E186717</lastuser>
          <dateupd>2006-04-06</dateupd>
          <timeupd>16:33:38</timeupd>
     </messageDetails>
     <messageDetails>
          <days>36</days>
          <msgid>MSGB</msgid>
          <letter>1</letter>
          <message>I agree </message>
          <status>ENABLE</status>
          <lastuser>E186717</lastuser>
          <dateupd>2006-04-12</dateupd>
          <timeupd>11:33:49</timeupd>
     </messageDetails>
</Messages_Report>
=========================
Here is my XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="html" />
     <xsl:template match="/">
          <html>
               <head>
                    <title>Message Report Details</title>
               <style>
BODY, A, TD {
     font-family: "Tahoma", Sans-Serif;
     font-size: 9pt;
     color: #333333;
}

BODY {
     background-color: White;
     margin: 1em;
}

A:hover {
     color: #0099CC;
}

.bookTable {
     padding: 2pt;
     width: 100%;
     border: solid 1px #999999;
}

.bookCell{
     padding: 2pt;
     text-align: left;
     border: none;
}

H1 {
    color: White;
    background-color: #0F3063;
    font-family: "Arial", Sans-Serif;
    font-size: 16pt;
    font-weight: bold;
    height: 22px;
    padding-left: 1pt;
}

H2 {
    color: gray;
    background-color: #FFFFFF;
    font-family: "Arial", Sans-Serif;
    font-size: 14pt;
    font-weight: bold;
    height: 18px;
    padding-left: 1pt;
}

.tblHeader {
    background-color: #DADADA;
    font-weight: bold;
}

.tblFooter {
    background-color: #CACACA;
    font-weight: bold;
    color:Teal;
}

</style>
</head>
<body>
<h1>Message Report Detail</h1>
<table class="bookTable" cellspacing="0" cellpadding="0">
<thead class="tblHeader">
<tr>
     <td>days</td>
     <td>msgid</td>
     <td>letter</td>
     <td>message</td>
     <td>status</td>
</tr>
</thead>
<xsl:for-each select="/Messages_Report/messageDetails">
<tr class="bookCell">
     <td><xsl:value-of select="days"/></td>
     <td><xsl:value-of select="msgid"/></td>
     <td><xsl:value-of select="letter"/></td>
     <td><xsl:value-of select="message"/></td>
     <td><xsl:value-of select="status"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
===============

/*
 * Apply XSLT stylesheets
 * Input parameters are names of local XML and XSLT files
 * Output is sent to the console  
 * XSLT Parameters are not supported.
 */

import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import java.io.*;
public class ApplyXSLT {

   public static void main(String[] args) {
      int argsCount=0;
      try
      {
         argsCount = args.length;
         if(argsCount != 2)
         {
            System.err.println("Usage java ApplyXSLT <localXMLFile> " +
                    "<localXSLTFile>\nExamples");

            System.err.println("\tjava ApplyXSLT BestBooks.xml BestBooks.xsl ");
            System.err.println("\tjava ApplyXSLT lowercase.xml ToUpper.xsl ");
            System.exit(1);
         }
      }
      catch(Exception exp)
      {
         exp.printStackTrace();
         System.exit(1);
      }
     
      try
      {

         //   Source XML File
         StreamSource xmlFile = new StreamSource(new File(args[0]));
         
         //   Source XSLT Stylesheet
         StreamSource xsltFile = new StreamSource(new File(args[1]));
         TransformerFactory xsltFactory = TransformerFactory.newInstance();
         Transformer transformer = xsltFactory.newTransformer(xsltFile);

         //   Send transformed output to the console
         StreamResult resultStream = new StreamResult(System.out);

         //   Apply the transformation
         transformer.transform(xmlFile, resultStream);
      }
      catch(Exception exp)
      {
         exp.printStackTrace();
      }
   }
}
winsuncareerAsked:
Who is Participating?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.