Saturday, May 23, 2015

XML: Transformation with XSL

XSL Transformations is a language for transforming XML documents into other XML documents, text documents or HTML documents.
If you went through my previous post on Parse and Validate xml, you already aware with the xml that I used to create POJOs. Here the xml that I used and if you aware with its second line you can see I linked xsl with xml in there.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="Students.xsl"?>
<studentsns:students xmlns:studentsns="http://www.charitha.org/students"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="StudentsSchema.xsd">
 <studentsns:student id="1">
  <studentsns:name>Charitha</studentsns:name>
  <studentsns:year>2009</studentsns:year>
  <studentsns:nic>895623145v</studentsns:nic>
 </studentsns:student>
 <studentsns:student id="2">
  <studentsns:name>Achithra</studentsns:name>
  <studentsns:year>2009</studentsns:year>
  <studentsns:nic>881234566v</studentsns:nic>
 </studentsns:student>
 <studentsns:student id="3">
  <studentsns:name>Malan</studentsns:name>
  <studentsns:year>2011</studentsns:year>
  <studentsns:nic>921123903v</studentsns:nic>
 </studentsns:student>
</studentsns:students>

This is the style sheet that I used to style my xml. In that xml I used  xml-stylesheet to stylize the document.

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:studentsns="http://www.charitha.org/students"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="StudentsSchema.xsd">

 <xsl:template match="/">
  <html>
   <body>
    <h2>Student Details</h2>
    <table border="1">
     <tr bgcolor="#9acd32">
      <th>Student ID</th>
      <th>Student Name</th>
      <th>Reg. Year</th>
      <th>NIC Number</th>
     </tr>
     <xsl:for-each select="studentsns:students/studentsns:student">
      <tr>
       <td>
        <xsl:value-of select="@id" />
       </td>
       <td>
        <xsl:value-of select="studentsns:name" />
       </td>
       <td>
        <xsl:value-of select="studentsns:year" />
       </td>
       <td>
        <xsl:value-of select="studentsns:nic" />
       </td>
      </tr>
     </xsl:for-each>
    </table>
   </body>
  </html>
 </xsl:template>

</xsl:stylesheet>

In this style sheet I used xsl:template tag to define styles for the document which is going to transform as HTML. So I wrote HTML contents as usual and specifically used xsl namespace to define special transformation instructions. Since we need to display student data from students list, we need to iterate through each student in students list. So in here we are using xsl:for-each tag and select "studentsns:students/studentsns:student". Then inside repetitive table rows we are selecting attributes by using "@" symbol before the attribute name and selecting tags by their name with namespace. xsl:value-of tag giving the value for selected tag or attribute.

If you open that xml with your web browser after placing above xsl with in the same directory of your xml, you will see the stylized xml in the browser.


Normally most of web browsers rendered xml from file source and display styled xml as HTML. Now we are going to transform xml in to HTML using javax.xml.transform.TransformerFactory. It is quite simple and we can generate HTML file from our xml as follows.

// stream source sml
StreamSource xmlSourse = new StreamSource(new File("students.xml"));
// stream source xsl
StreamSource xslSourse = new StreamSource(new File("students.xsl"));
// stream result output html
StreamResult htmlOutput = new StreamResult(new File("students.html"));

try {
 TransformerFactory.newInstance().newTransformer(xslSourse).transform(xmlSourse,
 htmlOutput);
} catch (TransformerException | TransformerFactoryConfigurationError e) {
 System.err.println("Transformation failed: " + e.getMessage());
}

Then students.html file generates in the same directory where your xml is placed and it is consists with populated data.


<html xmlns:studentsns="http://www.charitha.org/students"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<body>
 <h2>Student Details</h2>
 <table border="1">
  <tr bgcolor="#9acd32">
   <th>Student ID</th>
   <th>Student Name</th>
   <th>Reg. Year</th>
   <th>NIC Number</th>
  </tr>
  <tr>
   <td>1</td>
   <td>Charitha</td>
   <td>2009</td>
   <td>891623897v</td>
  </tr>
  <tr>
   <td>2</td>
   <td>Achithra</td>
   <td>2009</td>
   <td>881234566v</td>
  </tr>
  <tr>
   <td>3</td>
   <td>Malan</td>
   <td>2011</td>
   <td>921123903v</td>
  </tr>
 </table>
</body>
</html>

So if we opened the html, it will display same as the styled xml which we opened earlier.

No comments:

Post a Comment