Sorting data in a HTML file using XML and XSLT

Posted on 2006-04-26
Last Modified: 2010-04-01

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)
Question by:winsuncareer
    1 Comment
    LVL 23

    Accepted Solution


    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    When we have a dead host and we lose all connections to the ESXi, and we need to find a way to move all VMs from that dead ESXi host.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now