Sorting data in a HTML file using XML and XSLT


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">
          <message>I agree </message>
Here is my XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="">
     <xsl:output method="html" />
     <xsl:template match="/">
                    <title>Message Report Details</title>
     font-family: "Tahoma", Sans-Serif;
     font-size: 9pt;
     color: #333333;

     background-color: White;
     margin: 1em;

A:hover {
     color: #0099CC;

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

     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;

<h1>Message Report Detail</h1>
<table class="bookTable" cellspacing="0" cellpadding="0">
<thead class="tblHeader">
<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>

 * 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.*;

public class ApplyXSLT {

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

            System.err.println("\tjava ApplyXSLT BestBooks.xml BestBooks.xsl ");
            System.err.println("\tjava ApplyXSLT lowercase.xml ToUpper.xsl ");
      catch(Exception exp)

         //   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)
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.