Wednesday 26 September 2012

How to convert XML to HTML, using XSL Transformation?

1) Let us take an example of XML shown below (It is RSS format standard):

<?xml version="1.0" encoding="utf-8" standalone="no"?>

<rss xmlns:media="http://search.Google.com/" version="2.0">
    <channel>
        <title>News Headlines - Google! News</title>
        <link>http://news.Google.com/news/</link>
        <description>Get the latest news headlines from Google! News. </description>
        <language>en-US</language>
        <copyright>Copyright (c) 2012 Google! Inc. All rights reserved</copyright>
        <pubDate>Sat, 29 Sep 2013 18:43:11 -0300</pubDate>
        <ttl>5</ttl>
        <image>
        <title>News Headlines - Google! News</title>
        <link>http://news.Google.com/news/</link>
        <url>http://l.gimg.com/main_142c.gif</url>
        </image>
        <item>
            <title>Ship ran away on Shark attack</title>
            <description>Obama says "It is bad. We should boat from water world"</description>
            <link>http://news.Google.com/news.html</link>
            <pubDate>Sat, 29 Sep 2013 18:43:11 -0300</pubDate>
            <source url="http://www.pp.org/">Press</source>
            <guid isPermaLink="false">Ship ran away on Shark attack</guid>
        </item>
    </channel>
</rss>



2) Now we need to write a XSL file which shows result document.
Remember, here we use a result document tag which is present in version XSLT 2.0
You can convert XML to XHTML, HTML, JSON or Text File.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:output name="xml" method="xml" indent="yes"/>
<xsl:output name="html" method="html" indent="yes"/>
<xsl:output name="xhtml" method="xhtml" indent="yes"/>
<xsl:param name="dir">file:///c:/</xsl:param> //This is place where the output file is to be stored

<xsl:template match="/">
 <xsl:result-document format="html" href="{$dir}/FeedofGoogle.html">  //This is file name
        <html>
            <head>
                <title>Google Feeds</title>
            </head>
            <body>
                <xsl:variable name="HLink" select="rss/channel/link"></xsl:variable>
                <table border="1">
                    <xsl:for-each select="rss/channel/item">
                        <xsl:if test="position()=1">
                            <tr>
                                <td colspan="2">Topic 1</td>
                            </tr>
                            <tr>
                                <td>Title of News</td>
                                <td>
                                    <xsl:value-of select="title"/>
                                </td>
                               
                            </tr>
                            <tr>
                                <td>Description of News</td>
                                <td>
                                    <div>
                                        <xsl:value-of disable-output-escaping="yes" select="description"/>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>Domain - Link of News</td>
                                <td>
                                    <xsl:element name="a">
                                        <xsl:attribute name="href">
                                            <xsl:value-of select="link"/>
                                        </xsl:attribute>
                                        <xsl:value-of select="substring-before($HLink, '/n')" /> //This is function of XSLT1.0, which return whole string before substring 'n' appears
                                    </xsl:element>
                                </td>
                            </tr>
                            <tr>
                                <td>Image in News:</td>
                                <td>
                                    <xsl:element name="img">
                                        <xsl:attribute name="src">
                                            <xsl:value-of xmlns:media="http://search.google.com/" select="media:content/@url"/>
                                        </xsl:attribute>   
                                    </xsl:element>
                                </td>
                            </tr>
                        </xsl:if>
                    </xsl:for-each>
                </table>
            </body>
        </html>
 </xsl:result-document>

 </xsl:template>
</xsl:stylesheet>


You can also use one more Result Document tag into JSON, XHTML or File


3) After XSL is developed, it is require to transform the XML and XSl to HTML for that you can use some Transformer.

SAXON is one among transformer, famous around searches.

In Java, Saxon jar is available online
as we have XML and XSL, we need to go to command prompt and write following piece of code:

java -jar <SAXON.JAR PATH> <XML PATH> <XSL PATH>

Download the Jar from here: Saxon 8 Jar, Saxon 9.Jar

TRAX API is also used for transformation, as shown below.

 String outPutHTML = "d:\\FeedofGoogle.html";  //Path where HTML
  StreamSource sourceFeed = new StreamSource("http://news.google.com/rss/news");
  StreamSource xslDocument = new StreamSource("d:\\FeedTransformationCode.xsl");
  StreamResult resultHTMLDoc = new StreamResult(new FileOutputStream(outPutHTML));
  TransformerFactory transFactory = TransformerFactory.newInstance();
  Transformer transformer = transFactory.newTransformer(xslDocument);  
  transformer.transform(sourceFeed, resultHTMLDoc);


No comments:

Post a Comment