Techniques for Performing XSLT Transformations

XSLT transformations can happen in three different places:

  • In the server. A server program, such as a .NET or JavaServer Page (JSP) program that operates on a Web server, can use XSLT to transform an XML document and send it to the client program, such as a browser.

  • In the client. A client program, such as an HTML browser, can perform XSLT transformations. For example, Internet Explorer has full XSLT 1.0 support.

  • With a separate program. You can use standalone programs to perform your own XSLT transformations.

As a client program example, recall that Internet Explorer lets you perform XSLT 1.0 transformations. To make that transformation happen, you have to connect your stylesheet to the XML document to transform. You can do that using an <?xml-stylesheet?> processing instruction like this: <?xml-stylesheet type="text/xsl" href="ch05_02.xsl"?> (note that some other XSLT processors will require this attribute to be type="text/xml" , not type="text/xsl" ). You can see Internet Explorerenabled version of our sample XML document in ch05_03.xml (see Listing 5.2), where we're putting the <?xml-stylesheet?> processing instruction to work.

Listing 5.2 An XML Document Holding Planetary Data ( ch05_03.xml )
 <?xml version="1.0" encoding="UTF-8"?>  <?xml-stylesheet type="text/xsl" href="ch05_02.xsl"?>  <planets>     <planet>         <name>Mercury</name>         <mass units="(Earth = 1)">.0553</mass>         <day units="days">58.65</day>         <radius units="miles">1516</radius>         <density units="(Earth = 1)">.983</density>         <distance units="million miles">43.4</distance><!--At perihelion-->     </planet>     <planet>         <name>Venus</name>         <mass units="(Earth = 1)">.815</mass>         <day units="days">116.75</day>         <radius units="miles">3716</radius>         <density units="(Earth = 1)">.943</density>         <distance units="million miles">66.8</distance><!--At perihelion-->     </planet>     <planet>         <name>Earth</name>         <mass units="(Earth = 1)">1</mass>         <day units="days">1</day>         <radius units="miles">2107</radius>         <density units="(Earth = 1)">1</density>         <distance units="million miles">128.4</distance><!--At perihelion-->     </planet> </planets> 

You can see the results in Figure 5.1. Using Internet Explorer like this is the most accessible way to perform XSLT transformations for most people.

Figure 5.1. Using XSLT in the client.

graphics/05fig01.jpg

You can also use standalone packages to perform XSLT transformations. For example, the Xalan XSLT processor mentioned in Chapter 1 lets you perform XSLT transformations. You can download Xalan at http://xml.apache.org/xalan-j/index.html. Note that you'll need to have Java installed; the current version as of this writing is 1.4, which you can download for free from http://java.sun.com/j2se/1.4/download.html.

Here's how you'd use Xalan to transform ch05_01.xml , using ch05_02.xsl , into a new result document, which we'll call results.html :

 
 %xalan ch05_01.xml ch05_02.xsl results.html 

And here's what results.html looks like when you do this (where we've indented the HTML to make it more readable):

 
 <HTML>     <HEAD>         <META http-equiv="Content-Type" content="text/html; charset=UTF-8">         <TITLE>             The Planets Table         </TITLE>     </HEAD>     <BODY>         <H1>             The Planets Table         </H1>         <TABLE BORDER="2">             <TR>                 <TD>Name</TD>                 <TD>Mass</TD>                 <TD>Radius</TD>                 <TD>Day</TD>             </TR>             <TR>                 <TD>Mercury</TD>                 <TD>.0553 (Earth = 1)</TD>                 <TD>1516 miles</TD>                 <TD>58.65 days</TD>             </TR>             <TR>                 <TD>Venus</TD>                 <TD>.815 (Earth = 1)</TD>                 <TD>3716 miles</TD>                 <TD>116.75 days</TD>             </TR>             <TR>                 <TD>Earth</TD>                 <TD>1 (Earth = 1)</TD>                 <TD>2107 miles</TD>                 <TD>1 days</TD>             </TR>         </TABLE>     </BODY> </HTML> 

Another popular XSLT processor that runs with Java is named Saxon, and you can download Saxon for free from http://saxon. sourceforge .net/. As we'll see in this chapter, Saxon can do some things that Xalan can't, such as indent output documents automatically. We're also going to see Saxon at work in the second half of this book, because it's the first XSLT processor to include any support for XPath 2.0 (Saxon is written by Michael Kay, who is on the XPath 2.0 W3C design committee.)

That's how you make XSLT transformations happen. Now let's get more details on how to create stylesheets.



XPath. Navigating XML with XPath 1.0 and 2.0 Kick Start
XPath Kick Start: Navigating XML with XPath 1.0 and 2.0
ISBN: 0672324113
EAN: 2147483647
Year: 2002
Pages: 131

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