XSLT is a short form of ‘EXtensible Stylesheet Language Transformation’, powerful technology and used to transform XML into various formats such as HTML. In this post, we will see how to convert an XML file to HTML with the help of XSLT in Java.

Key points:

  • HTML stands for ‘Hypertext Markup Language’.
  • XML stands for ‘Extensible Markup Language’.
  • XSLT provides the ability to transform XML from one format to another.
  • XSL is used to filter and sorts the XML data and can format XML.
  • XSL can be used to define how an XML file should look like by transforming the XML file into a browser recognizable format.

 

Prerequisite Files:

1. XML file (say report.xml)

2. XSL file (say stylesheet.xsl)

 

Sample XML file : report.xml

<?xml version="1.0" encoding="UTF-8"?>
<urlset>
   <url>
      <stepno>1</stepno>
      <stepname>Successfully Started browser at http://www.officedepot.com</stepname>
      <status>PASS</status>
   </url>
   <url>
      <stepno>2</stepno>
      <stepname>click : By.cssSelector: .ui-link-inherit</stepname>
      <status>PASS</status>
   </url>
</urlset>

Sample XSL file : stylesheet.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:template match="/">
      <html>
         <body>
            <h1>Test Case Results</h1>
            <table border="1">
               <tr>
                  <th>stepno</th>
                  <th>stepname</th>
                  <th>status</th>
               </tr>
               <xsl:for-each select="urlset/url">
                  <tr>
                     <td>
                        <xsl:value-of select="stepno" />
                     </td>
                     <td>
                        <xsl:value-of select="stepname" />
                     </td>
                     <td>
                        <xsl:value-of select="status" />
                     </td>
                  </tr>
               </xsl:for-each>
            </table>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

Below is the Java code to generate an HTML file using above XML and XSL files: (say report.html)

import java.io.FileOutputStream;

import java.io.OutputStream;

import javax.xml.transform.Source;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.stream.StreamResult;

import javax.xml.transform.stream.StreamSource;

public class Xmltohtml {

 public static void main(String[] args) throws Exception {

  try {

   TransformerFactory tFactory = TransformerFactory.newInstance();

   Source xslDoc = new StreamSource("/Users/jradmin/Documents/stylesheet.xsl");

   Source xmlDoc = new StreamSource("/Users/jradmin/Documents/report.xml");

   String outputFileName = "/Users/jradmin/Documents/report.html";

   OutputStream htmlFile = new FileOutputStream(outputFileName);

   Transformer trasform = tFactory.newTransformer(xslDoc);

   trasform.transform(xmlDoc, new StreamResult(htmlFile));

  } catch (Exception e)

  {

   e.printStackTrace();

  }

 }

}

Below is the generated HTML report (report.html):

HTML Report

 

Reference:

 

 

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts