06 May 2010

How Do I Get XSLT to Work in a Browser?

An XSLT processor is probably readily available to you on your computer desktop in the form of a web browser: Microsoft Internet Explorer (IE) Version 6, Netscape Navigator (Netscape) Version 7.1, Mozilla Version 1.4, or Mozilla Firebird 0.7. Each of these browsers has client-side XSLT processing ability already built into them.

The way to apply an XSLT stylesheet like msg.xsl to the document msg.xml in a browser is by using a processing instruction. A processing instruction (PI) allows you to include instructions for an application in an XML document.

You can see a processing instruction in a slightly altered version of msg.xml, which I call msg-pi.xml:

<?xml-stylesheet href="msg.xsl" type="text/xsl"?>
<msg/>


The XML stylesheet PI should always come before the first element in the document (the document element msg in msg-pi.xml). The purpose of this PI is similar to one of the purposes of the link tag in HTML, that is, to associate a stylesheet with the document. Save msg-pi.xml in a text file with the other files. If you open msg-pi.xml in one of the browsers I mentioned, the built-in XSLT processor in the browser will write the string Found it! on the browser's canvas or rendering space.

No comments: