Monday, April 6, 2009

XSLT tutorial using Java and XML

Extensible Stylesheet Language Transformations (XSLT)
XSLT provides a framework for transforming the structure of anXML document. XSLT combines an input XML document with an XSLstylesheet to produce an output document like HTML, PDF ...........
An XSL stylesheet is a set of transformation instructions for convertinga source XML document to a target output document. It requires an XSLT-compliant processor. The most popular open source XSLTengine for Java is the Apache Software Foundation’s Xalan project.
Here I am going to transform our student record into HTML for rendering purpose.

1. First add jar xml-apis.jar (Google and download)
2. student.xml
3. student.xsl
4. XMLToHTML.java

student.xml
<?xml version="1.0" encoding="ISO-8859-1"?>StudentRecord>
<Student>
<name>Binod Kumar Suman</name>
<roll>110</roll>
<country>India</country>
<company>Satyam Computers</company>
</Student>

<Student>
<name>Pramod Kumar</name>
<roll>120</roll>
<country>India</country>
<company>Patni Computers</company>
</Student>

<Student>
<name>Manish</name>
<roll>130</roll>
<country>India</country>
<company>Infosys</company>
</Student>

</StudentRecord>

student.xsl

<?xml version="1.0" encoding="ISO-8859-1"? >
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform;
<xsl:template match="/">
<html> <body>
<h2>STUDENT RECORDS</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Name</th>
<th align="left">Roll</th>
</tr>

<xsl:for-each select="StudentRecord/Student">
<tr>
<td> <xsl:value-of select="name" /> </td>
<td> <xsl:value-of select="roll" /> </td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

XMLToHTML.java


import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;

public class XMLToHTML {

public static void main(String[] args) throws Exception {
File xmlFile = new File("src\\student.xml");
File xsltFile = new File("src\\student.xsl");
Source xmlSource = new StreamSource(xmlFile);
Source xsltSource = new StreamSource(xsltFile);
TransformerFactory transFact = TransformerFactory.newInstance();
Transformer trans = transFact.newTransformer(xsltSource);
//trans.transform(xmlSource, new StreamResult(System.out));
trans.transform(xmlSource, new StreamResult(new FileOutputStream("test.html")));
System.out.println("** HTML CREATED **");
}
}

After run the application, you will get new test.html file created in root folder of this application.

Interesting Note: Even witthout create html file, you can view xml in html formate in internet explorer. Without change any thing just do double click on student.xml, it will open in internet explorer like xml file.
Now add the XSN file infomation in XML then try to open XML file. You will get html formate.
How to add XSN information in XML file

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="student.xsl"?>
<StudentRecord>
<Student>
<name>Manish Kumar</name>
<roll>110</roll>
<country>India</country>
<company>Satyam Computers</company>
</Student>
<Student>
<name>Pramod Kumar</name>
<roll>120</roll>
<country>India</country>
<company>Patni Computers</company>
</Student>
<Student>
<name>Binod Kumar Suman</name>
<roll>130</roll>
<country>India</country>
<company>Satyam Computer Service Ltd</company>
</Student>
</StudentRecord>

Now you try to open this xml, it would be open in html formate as per the xsn.
Both student.xml and student.xsl should be in the same folder.

How to Filter Output in XSLT

You can give condition to the output from XML by adding these conditions :

1. =
2. !=
3. < less than
4. > greater than
5. element

student.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform; <xsl:template match="/"> <html>
<body>
<h2>STUDENT RECORDS</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Name</th>
<th align="left">Roll</th>
<xsl:for-each select="StudentRecord/Student[name='Binod Kumar Suman']">
<tr> <td>
<xsl:value-of select="name" />
</td> <td>
<xsl:value-of select="roll" />
</td> </tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Now you can run java program, you will get html file. In that file you will get only given student name output.
Second filter : Show only student record, whose roll number is greater than 115.

<xsl:for-each select="StudentRecord/Student">
<xsl:if test="roll > 115">
<tr> <td>
<xsl:value-of select="name" />
</td> <td>
<xsl:value-of select="roll" />
</td> </tr>
</xsl:if>
</xsl:for-each>

Now you can also sorting of your output
<xsl:for-each select="StudentRecord/Student">
<xsl:sort select="name"/>
<tr> <td>
<xsl:value-of select="name" />
</td> <td>
<xsl:value-of select="roll" /> </td>
</tr>
</xsl:for-each>

Actually there are many different ways to show xml file in formatted style using xslt
1. Using xsl file reference in xml file and open in internet browser (described above) that file is called XHTML file.
2. Using java Srcipt
How to use Java Script to show xml information in formated way as defined in xsl file
(A.) create xml file (Student.xml) as above
(B.) create xsl file (student.xsl) as above
(C.) create html file (test.htm) as below

test.htm

<html><body>
<script type="text/javascript">
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.load("student.xml")
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.load("student.xsl")
document.write(xml.transformNode(xsl))
</script>
</body>
</html>

Now open test.htm file in internet browser.

2 comments:

  1. Ajax Example is really good for beginners.Actually, i want to understand about
    JSON Object and JSON Array.

    ReplyDelete
  2. Hi Vikram, AJON is basically JavaSript Annotation for Asychronous request. I will give some good example on AJON in coming weekend. Thanks for comment and keep watching to my blog. :)

    ReplyDelete

You can put your comments here (Either feedback or your Question related to blog)