How to output HTML file from XML and XSLT stylesheet

I created an XML data document and an XSLT stylesheet, and I want to output an HTML document based on two. There is a tag in my stylesheet, and my XML document has processor performance (along with various "xsl: value-of" links). My question is, what are the actual โ€œmechanicsโ€ of getting an XSLT processor (which, as I understand it, is built into all web browsers) for reading XML and XSLT style files and outputting an HTML document that will then be displayed on the browser? In the XML book I read, this is not indicated! Thanks you

+6
html xml xslt
source share
4 answers

The following is the Java code that is used to create the HTML file. When you run this code, an 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); } } 
+3
source share

You can start XSL transformations in the usual way using the Javascript API or use the xml table processing instruction, for example:

Download this to your browser ...

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

and stylesheet, save this as demo.xslt in the same directory 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 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 on Firefox on Linux.

+2
source share

Demetrius's answer is what you need. But here you have an example:

XML:

 <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?> <document> ... </document> 

Opening the previous XML document in any browser (not really, but you get it ...), and it converts the XML document using stylesheet.xsl and displays the result.

This is really a mess when it comes to conversions in imo browsers, poor support and only XSLT 1.0. The MIME text/xsl not even โ€œcorrectโ€, but it is most often supported by browsers. The correct MIME type should be application/xslt+xml , but it is not supported in any (?) Browser, as far as I know. See Alejandros Comment below.

+2
source share

My question is: what are the actual โ€œmechanicsโ€ of getting the XSLT processor (which, as I understand it, is built into all web browsers) to read the XML and XSLT stylesheet files and output an HTML document that will then be displayed in the browser?

This is the task of the particular HTML browser used to invoke its XSLT processor. The browser then interprets the results of the XSLT transformation as the HTML that should be displayed. Note that in general, browsers are not required to support XSLT preprocessing, so there may be browsers that do not have an associated XSLT processor, and do not follow the xml-stylesheet PI for the type="text/xsl" pseudo-attribute.

For more information, read the W3C specification on the Linking Style Sheets to XML Documents page .

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

Of course, I do not recommend this primitive method . it is much better to use one of the many existing XSLT IDEs such as XSelerator, oXygen, Visual Studio, ... etc.

+1
source share

All Articles