Using Browsers to Transform XML Documents

Using Browsers to Transform XML Documents

Both the Microsoft Internet Explorer and Netscape Navigator include some support for XSLT. Of the two, the Internet Explorers support is far more developed, and Ill use version 5.5 of that browser here. You can read about the Internet Explorer XSLT support at http://msdn.microsoft.com/xml/XSLGuide/.

Internet Explorer 5.5 and earlier does not support exact XSLT syntax by default, so we'll have to make a few modifications to planets.xml and planets.xsl. (You'll learn more about this in the next chapter. There are downloads you can install for updated XSLT support.) In fact, just as this book goes to print, Internet Explorer 6.0 has become available. When I installed and tested it, it does appear to support standard XSLT syntax (except you still must use the type text/xsl for stylesheets like this: <?xml-stylesheet type="text/xsl" href="planets.xsl"?> instead of text/xml. If you still use IE 5.5 or earlier, you'll have to make the changes outlined here and in the next chapter. If you want to avoid all that, I suggest you upgrade to IE 6.0it looks like that browser supports full XSLT syntax.

To use planets.xml with IE (including version 6.0), I have to convert the type attribute in the <?xml-stylesheet?> processing instruction from text/xml to text/xsl (this assumes that planets.xsl is in the same directory as planets.xml, as specified by the href attribute):

Listing 1.3 Microsoft Internet Explorer Version of planets.xml
 <?xml version="1.0"?>  <?xml-stylesheet type="text/xsl" href="planets.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> 

Now you must also convert the stylesheet planets.xsl for use in IE if you're using version 5.5 or earlier (but not version 6.0 or laterthe only change you have to make is setting the type attribute in the <?xml-stylesheet?> processing instruction from text/xml to text/xsl). You'll see how to make this conversion in the next chapter; here's the new version of planets.xsl that you use:

Listing 1.4 Microsoft Internet Explorer Version of planets.xsl
 <?xml version="1.0"?>  <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">      <xsl:template match="/">          <HTML>              <HEAD>                  <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>                      <xsl:apply-templates/>                  </TABLE>              </BODY>          </HTML>      </xsl:template>      <xsl:template match="PLANETS">          <xsl:apply-templates/>      </xsl:template>      <xsl:template match="PLANET">         <TR>            <TD><xsl:value-of select="NAME"/></TD>            <TD><xsl:apply-templates select="MASS"/></TD>            <TD><xsl:apply-templates select="RADIUS"/></TD>            <TD><xsl:apply-templates select="DAY"/></TD>         </TR>      </xsl:template>      <xsl:template match="MASS">          <xsl:value-of select="."/>          <xsl:value-of select="@UNITS"/>      </xsl:template>      <xsl:template match="RADIUS">          <xsl:value-of select="."/>          <xsl:value-of select="@UNITS"/>      </xsl:template>      <xsl:template match="DAY">          <xsl:value-of select="."/>          <xsl:value-of select="@UNITS"/>      </xsl:template>  </xsl:stylesheet> 

Now you can open planets.xml in the Internet Explorer directly, as you see in Figure 1.3.

Figure 1.3. Performing an XSLT transformation in the Internet Explorer.
graphics/01fig03.gif

Although you can use XSLT with the Internet Explorer in this way, you need to modify your stylesheet to match what the Internet Explorer requires. Because the Internet Explorer does not currently support true XSLT when you open XML documents by navigating to them, I wont be using that browser to perform XSLT transformations in this book unless specifically noted. Ill use XSLT processors like Saxon and Xalan to perform transformations, and when the result is HTML, take a look at that result in the Internet Explorer.

Interestingly, there is a way to perform true XSLT transformations in the Internet Explorer without making any special modifications to XML or XSL documents, even if you dont download and install the latest MSXML parser (as discussed in Chapter 2)rather than navigate to an XML document, however, you must access the XSLT processor in the Internet Explorer, MSXML3, directly, using JavaScript.



Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

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