Using Internet Explorer to Transform XML Documents

Using Internet Explorer to Transform XML Documents

Theres one more topic to discuss during this overview of stylesheets, and thats how to use stylesheets in the Internet Explorer. As we saw in Chapter 1, you can use JavaScript to read in XML and XSL documents, and use the MSXML3 parser to perform the transformation. (For more information on this, see Chapter 10. You can also read about the Internet Explorer support at http://msdn.microsoft.com/xml/XSLGuide/).

However, if you want to open an XML document directly in Internet Explorer by navigating to it (for example, by typing its URI into the Address box), you're relying on the browser to use the <?xml-stylesheet?> and <xsl:stylesheet> elements itself, which means you need to make a few changes if you're using IE 5.5 or earlier.

Internet Explorer 6.0 and Getting and Installing the MSXML Parser

Note: IE 6.0 is just out as this book goes to press, and it does support full XSLT syntax (except that 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're using IE 5.5 or earlier, you can also download and install the latest version of the MSXML parser directly from Microsoft, replacing the earlier one used by the Internet Explorer. When you do, you don't need to make the modifications listed in this section. For more information, see http://msdn.microsoft.com/xml/general/xmlparser.asp. The download is currently at http://msdn.microsoft.com/downloads/default.asp?URL=/code/sample.asp?url=/msdn-files/027/000/541/msdncompositedoc.xml. (Note, however, that Microsoft seems to reorganize its site every fifteen minutes or so.) If you're using IE 5.5 or earlier, I urge you to download MSXML so that you won't have to modify all your XSLT stylesheets to use them in IE, or upgrade to version 6.0 or later.

It's necessary to modify both planets.xml and planets.xsl for IE version 5.5 or earlier. To use planets.xml with IE, you convert the type attribute in the <?xml-stylesheet?> processing instruction from text/xml to text/xsl:

Listing 2.14 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> 

You must also convert the stylesheet planets.xsl for use in IE version 5.5 or earlier. A major difference between the W3C XSL recommendation and the XSL implementation in IE is that version 5.5 or earlier does not implement any default XSL rulessee Chapter 3 (note that IE version 6.0, just out as this book goes to press, does not have this problem). That means for IE version 5.5 or earlier, I have to include an XSL rule for the root node of the document, which you specify with /. I also have to use a different XSL namespace in the stylesheet, http://www.w3.org/TR/WD-xsl, and omit the version attribute in the <xsl:stylesheet> element:

Listing 2.15 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:value-of select="MASS"/></TD>            <TD><xsl:value-of select="RADIUS"/></TD>            <TD><xsl:value-of select="DAY"/></TD>         </TR>     </xsl:template>  </xsl:stylesheet> 

And thats it! Now weve successfully implemented planets.xml and planets.xsl for direct viewing in the Internet Explorer. Those are the changes you must make to use this browser when you navigate to XSL-styled XML documents directly.

That completes this overview of working with stylesheets in XSL. The next chapter looks at the heart of stylesheetstemplatesin more detail.



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