1.2 Using Client-Side XSLT in a Browser

Now comes the action. An XSLT processor is probably readily available to you on your computer in a browser such as Microsoft Internet Explorer (IE) Version 6 or later, Netscape Navigator (Netscape) Version 7.1 or later, or Mozilla Version 1.4 or later. All three of these browsers have client-side XSLT processing ability already built-in.

A common way to apply an XSLT stylesheet like msg.xsl to the document msg.xml in a browser is by using a processing instruction. You can see a processing instruction in a slightly altered version of msg.xml called msg-pi.xml. Open the file msg-pi.xml from examples/ch01 with one of the browsers mentioned. The result tree (a result twig, really) is displayed. Figure 1-1 shows you what the result looks like in IE Version 6, with service pack 1 (SP1). I explain how msg-pi.xml works in the section "The XML Stylesheet Processing Instruction" which follows.

Figure 1-1. Transforming msg-pi.xml with Internet Explorer
figs/lxsl_0101.gif

When the XSLT processor in the browser found the pattern identified by the template in msg.xsl, it wrote the string Found it! onto the browser's canvas or rendering space.

If you look at the source for the page using View Source or View Page Source, you will see that the source tree for the transformation (the document msg-pi.xml) is displayed, not the result tree.


XSLT Support in Browsers

You'll get a chance to try out a variety of XSLT processors when running the examples in this book. Fortunately, the latest versions of IE, Netscape, and Mozilla (including Mozilla Firebird), which I'll use with many examples, have built-in XML and XSLT support. IE of course works on the Windows platform, but Netscape and Mozilla work on the big three: Windows, Macintosh, and Linux.

Earlier browsers did not support XML and XSLT for the obvious reason that neither XML nor XSLT existed when the browsers came out. Fortunately, it's fairly easy to upgrade to the latest version of a browser. And a nice thing about IE, Netscape, and Mozilla is that they are all free to download.

If your browser doesn't seem to work with an example in the book, it's probably because you have an older version of that browser that doesn't support XSLT. I won't often mention the version number of a browser when I use it in an example, so it's generally a good idea to install the latest browser of your choice on your computer.

To download or upgrade IE, go to http://www.microsoft.com/windows/ie/; for Netscape upgrades, point your browser at http://channels.netscape.com/ns/browsers/; and for Mozilla, go to http://www.mozilla.org.

To say the least, there are other good browsers out there besides IE, Netscape, and Mozilla. Other popular choices are Opera (http://www.opera.com) or Safari (http://www.apple.com/safari/; Mac only), but Opera and Safari do not at this moment support XSLT on the client side (the page-requesting side rather than the page-serving side). Consequently, I won't be using Opera or Safari with any examples in this book.


1.2.1 The XML Stylesheet Processing Instruction

To apply an XSLT stylesheet to an XML document with a browser, you must first add an XML stylesheet processing instruction to the document. This is the case with msg-pi.xml, which is why you can display it in an XSLT-aware browser. A processing instruction, or PI, allows you to include instructions for an application in an XML document.

The document msg-pi.xml, which you displayed earlier in a browser, contains an XML stylesheet PI:

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

The XML stylesheet PI should always come before the document element (msg in this case), and is part of what is called the prolog of an XML document. 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. Usually, there is only one XML stylesheet PI in a document, but under certain circumstances, you can have more than one.

For the official story on PIs in XML, refer to Section 2.6 of the XML specification. The xml-stylesheet PI is documented in the W3C Recommendation "Associating Style Sheets with XML Documents" (http://www.w3.org/TR/xml-stylesheet/).


In the XML stylesheet PI, the term xml-stylesheet is the target of the PI. The target identifies the name, purpose, or intent of the PI. This assumes that the application understands what the PI target is. Home-grown PIs are usually application-specific, but the XML stylesheet PI is widely supported and understood. If you invent a new, unique PI target, you also have to write the code to process your PI.

1.2.1.1 Attributes and pseudoattributes

In XML, attributes may only appear in element start tags or empty element tags, as shown in this element start tag (from message.xml):

<message priority="low">

This message element contains an attribute, with priority as the attribute name and low as the attribute value. The attribute name and value are separated by an equals sign (=). In well-formed XML, attribute values must always be surrounded by either single (') or double (") quotes. The quotes must not be mixed together. You can read more about attributes in Section 3.1 of the XML specification.

The constructs that follow the target in the XML stylesheet PI, href and type, are not attributes but are pseudoattributes. PIs can contain any legal XML characters between the target and the closing ?>, not just text that looks like attributes. For example, the following PI is perfectly legal:

<?do not go gentle into that good night?>

The first word following <? is do. This is the target of the PI, and there must be no space between <? and the target. The target is followed by the data not go gentle into that good night. This may not be complete nonsense to a Dylan Thomas fan, but a PI will be nonsense to an application unless the PI contains a target and other data that the application understands and knows what to do with it. If an XML processor does not understand the content of a PI, the consequences are not dire. The processor will simply ignore the PI and move on. Pseudoattributes structure the data so processors may have an easier time interpreting it.

The href pseudoattribute contains a value that is a URI reference. This URI specifies the relative location of the stylesheet msg.xsl. An XSLT processor knows where to find resources relative to its base URI. The base URI is usually the directory that holds the source document. The other pseudoattribute, type, identifies the content type of the stylesheet, text/xsl. The content type identifies the content of the stylesheet as XSL or XSLT text.

A content type of application/xsl or text/xslt may also work with some applications, but text/xsl works consistently. There is some confusion over what content type should be used for XSLT, but let's not get into that brouhaha. Just know that text/xsl is widely accepted and works consistently.




Learning XSLT
Learning XSLT
ISBN: 0596003277
EAN: 2147483647
Year: 2003
Pages: 164

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net