StackStalk
  • Home
  • Java
    • Java Collection
    • Spring Boot Collection
  • Python
    • Python Collection
  • C++
    • C++ Collection
    • Progamming Problems
    • Algorithms
    • Data Structures
    • Design Patterns
  • General
    • Tips and Tricks

Saturday, January 25, 2014

Transform XML using XSLT in Java

 January 25, 2014     Java     No comments   

This article explains how to transform XML documents using XSLT.

XSLT Introduction

XSLT (eXtensible Stylesheet Language Transformations) is a language for transforming XML documents into HTML, XML, or other types of documents. Formatting rules to transform the input XML data is specified in a XML stylesheet (XSL). Inside the XSL typically XML Path language (XPath) is used to identify different parts of the XML document.

Xalan-Java (http://xml.apache.org/xalan-j/) is an XSLT processor for transforming XML documents. Xalan-Java fully implements W3C specification of XSLT version 1.0 and XPath version 1.0.

Simple Java program to transform XML into HTML

In this example we are interested to transform an XML document into a HTML table for presentation.

This is input XML data file.
<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
   <book id="bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
   </book>
</catalog>
This is input XSL file.
<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <body>
  <table border="1">
   <tr>
    <th>Title</th>
    <th>Author</th>
    <th>Description</th>
    <th>Genre</th>
       <th>Price</th> 
   </tr>
   <xsl:for-each select="catalog/book">
     <tr>
       <td><xsl:value-of select="title"/></td>
       <td><xsl:value-of select="author"/></td>
       <td><xsl:value-of select="description"/></td>
       <td><xsl:value-of select="genre"/></td>
       <td><xsl:value-of select="price"/></td>
     </tr>
   </xsl:for-each>
  </table>
 </body>
</html>
Here is the Java program to perform the transformation. Note that we use a system property to specify that we are interested in using Xalan XSLT processor.
package com.sourcetricks.transform;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class TransformXml {

 public static void main(String[] args) {
  
  // Input xml data file
  String xmlInput = "resources/input.xml";
  
  // Input xsl (stylesheet) file
  String xslInput = "resources/input.xsl";
  
  // Output html file
  String htmlOutput = "/tmp/output.html";
  
  // Set the property to use xalan processor
  System.setProperty("javax.xml.transform.TransformerFactory",
    "org.apache.xalan.processor.TransformerFactoryImpl");
  
  // try with resources
  try ( FileOutputStream os = new FileOutputStream(htmlOutput) )
  {
   FileInputStream xml = new FileInputStream(xmlInput);
   FileInputStream xsl = new FileInputStream(xslInput);
   
   // Instantiate a transformer factory
   TransformerFactory tFactory = TransformerFactory.newInstance();
   
   // Use the TransformerFactory to process the stylesheet source and produce a Transformer 
   StreamSource styleSource = new StreamSource(xsl);
   Transformer transformer = tFactory.newTransformer(styleSource);
   
   // Use the transformer and perform the transformation
   StreamSource xmlSource = new StreamSource(xml);
   StreamResult result = new StreamResult(os);
   transformer.transform(xmlSource, result);
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
 }
}
This is the output of the program.
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Follow @StackStalk
Get new posts by email:
Powered by follow.it

Popular Posts

  • Python FastAPI file upload and download
    In this article, we will look at an example of how to implement a file upload and download API in a Python FastAPI microservice. Example bel...
  • Avro Producer and Consumer with Python using Confluent Kafka
    In this article, we will understand Avro a popular data serialization format in streaming data applications and develop a simple Avro Produc...
  • Monitor Spring Boot App with Micrometer and Prometheus
    Modern distributed applications typically have multiple microservices working together. Ability to monitor and manage aspects like health, m...
  • Server-Sent Events with Spring WebFlux
    In this article we will review the concepts of server-sent events and work on an example using WebFlux. Before getting into this article it ...
  • Accessing the Kubernetes API
    In this article, we will explore the steps required to access the Kubernetes API and overcome common challenges. All operations and communic...
  • Python FastAPI microservice with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Scheduling jobs in Python
    When developing applications and microservices we run into scenarios where there is a need to run scheduled tasks. Examples include performi...
  • Using Tekton to deploy KNative services
    Tekton is a popular open-source framework for building continuous delivery pipelines. Tekton provides a declarative way to define pipelines ...

Copyright © StackStalk