Sorting data in a HTML file using XML and XSLT

Posted on 2006-04-26
Medium Priority
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="http://www.w3.org/1999/XSL/Transform">
     <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.*;
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;
         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

Siva Prasanna Kumar earned 2000 total points
ID: 16566988

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Currently, there is an issue with being able to copy values from an external application to a dropdown list in Project Web Access (PWA).  The standard copy and paste methods don't seem to work properly. Here is a way to accomplish this task to s…
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Loops Section Overview
Suggested Courses

862 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