Using XSLT Stylesheets in XML Documents

You use XSLT to manipulate documents, changing and working with their markup as you want. You can use XSLT to translate XML into formatted HTML that is readily viewed , for example. Or, you can change XML into plain text, or another restructured XML document, or even a JavaScript document. XSLT provides a way of gaining access to the content of XML documents and building new documents that use and manipulate that content. For that reason, it's worthy of our study.

One of the most common transformations is from XML documents to HTML documents, and that's the kind of transformation we'll see in the examples in this chapter.

To create an XSLT transformation, you need two documents: the document to transform and the stylesheet that specifies the transformation. Both documents are XML documents.

Here's an example: This document, ch13_01.xml, is a well- formed XML document that holds data about three planets, Mercury, Venus, and Earth. Throughout this chapter, I'll transform this document to HTML in various ways. For programs that can understand it, you can use the <?xml-stylesheet?> processing instruction to indicate what XSLT stylesheet to use; you set the type attribute to "text/xml" and the href attribute to the uniform resource identifier (URI) of the XSLT stylesheet, such as ch13_02.xsl in this example (XSLT stylesheets usually have to extension .xsl):

Listing ch13_01.xml
 <?xml version="1.0"?>  <?xml-stylesheet type="text/xml" href="ch13_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> 


Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

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