Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output HTML file from XML and XSLT stylesheet

Tags:

html

xml

xslt

I've created a XML data document and an XSLT stylesheet, and I want to output an HTML document based on the two. My stylesheet has the tag, and my XML document has the processor instuction (along with various "xsl:value-of" references). My question is, what are the actual "mechanics" of getting the XSLT processor (which, I understand, is built in to all web browsers) to read the XML and XSLT stylesheet files and output the HTML document, which will then display on the browser? The XML book that I've been reading doesn't specify this! Thank you

like image 441
Jack Avatar asked Oct 18 '10 12:10

Jack


3 Answers

You can either run XSL transforms in the "normal way" using Javascript API, or use an xml-stylesheet processing instruction, like this:

Load this in to your browser...

<?xml version="1.0"?>
<?xml-stylesheet href="demo.xslt" type="text/xsl"?>
<data>
    <first>first</first>
    <second>second</second>
</data>

and the stylesheet, save this as demo.xslt in the same dir as the XML file

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <html>
            <head><title>Xslt browser demo</title></head>
            <body>
                    Here's my data:
                <xsl:for-each select="/data/*"><b><xsl:value-of select="."/></b></xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

This works for me in Firefox on Linux.

like image 85
Robin Avatar answered Nov 14 '22 03:11

Robin


Following is a java code which is used to create the HTML file.When you will run execute this code the out.html file will be created.

package xslt;

import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import java.io.*;


class XSLT {
    public static void main ( String argv[] ) throws Exception {
    File stylesheet = new File("xslt-example.xsl");
    File xmlfile  = new File("SigmodRecord.xml");
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(xmlfile);
    StreamSource stylesource = new StreamSource(stylesheet);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer(stylesource);
    DOMSource source = new DOMSource(document);
    //The Html output is in out.html
    StreamResult result = new StreamResult("out.html");
    transformer.transform(source,result);
    }
}
like image 28
yuvraj kadam Avatar answered Nov 14 '22 03:11

yuvraj kadam


My question is, what are the actual "mechanics" of getting the XSLT processor (which, I understand, is built in to all web browsers) to read the XML and XSLT stylesheet files and output the HTML document, which will then display on the browser?

It is the task of the specific HTML browser being used to invoke its XSLT processor. Then the browser interpretes the results of the XSLT transformation as the HTML that should be displayed. Do note that in general browsesers are not required to support XSLT pre-processing, so there may exist browsers that do not have an associated XSLT processor and do not honor the xml-stylesheet PI for the type="text/xsl" pseudo-attribute.

For more information read the W3C spec on "Associating Style Sheets with XML Documents"

To test the XSLT transformation in this, somewhat primitive way, you can open the XML file with your browser (do your homework and learn how to open a local file from the browser) and examine the results with a "View Source" or similar command.

I certainly do not recommend this primitive technique. It is much better to use one of the many existing XSLT IDEs, such as the XSelerator, oXygen, Visual Studio, ..., etc.

like image 41
Dimitre Novatchev Avatar answered Nov 14 '22 02:11

Dimitre Novatchev