14.2 An Embedded Stylesheet

Using a few tweaks and techniques, you can also embed a stylesheet in a document so that it's all stored in one package. An embedded stylesheet is a good idea when it's suitable to store both source and stylesheet in one convenient location. It's a convenience for situations when you write a stylesheet that will always be used with the same XML document, and it is distinct from stylesheets that might process a family of XML documents.

The stylesheet scandinavia.xml, shown in Example 14-1, uses this method to produce default HTML output.

Example 14-1. An XML document with an embedded stylesheet
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="#scand" type="text/xsl"?> <!DOCTYPE europe [  <!ATTLIST xsl:stylesheet id ID #IMPLIED> ]>     <europe>  <scandinavia>   <state>Finland</state>   <state>Sweden</state>   <state>Iceland</state>   <state>Norway</state>   <state>Denmark</state>  </scandinavia>     <!-- embedded stylesheet -->     <xsl:stylesheet  version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">     <xsl:template match="europe">  <xsl:apply-templates select="scandinavia"/> </xsl:template>     <xsl:template match="scandinavia">  <html>  <head><title>Scandanavian European States</title></head>  <style type="text/css">   h3,h4 {color: gray}   body {font-family: sans-serif}   span {color: red}  </style>  <body>   <h3>Alphabetical List of Scandanavian European States</h3>   <h4>Total Number of States:<xsl:text> </xsl:text>    <span><xsl:value-of select="count(state)"/></span></h4>   <ul>    <xsl:apply-templates select="state">     <xsl:sort/>    </xsl:apply-templates>   </ul>  </body>  </html> </xsl:template>     <xsl:template match="state">  <li><xsl:apply-templates/></li> </xsl:template>     </xsl:stylesheet>     </europe>

What's a stylesheet doing inside a source document? This works because of the id attribute on the stylesheet element. The scandinavia.xml document has an XML stylesheet PI at the top. It's href pseudoattribute contains a relative fragment identifier, #scand, and the fragment identifier refers to the value of the id attribute on stylesheet. That's the trick that makes the transformation happen.

Note also that the id attribute is declared in the internal subset DTD. This is required by most processors in order for an embedded stylesheet to work. (Xalan and the XSLT processor in Mozilla work without it, for example, but others, such as Saxon, do not.)

One other thing to note is that the complete stylesheet lives inside the document element of the source document (europe). The stylesheet can be at the top, bottom or middle of the source tree. The exact location within the document element doesn't matter, as long as the stylesheet resides within the document element.

Try it using the -a option with Xalan (this option works with Saxon, too). The -a option instructs the XSLT processor to find the stylesheet using the XML stylesheet PI:

xalan -i 1 -a scandinavia.xml

Here's the result you'll see:

<html>  <head>   <META http-equiv="Content-Type" content="text/html; charset=UTF-8">   <title>Scandanavian European States</title>  </head>  <style type="text/css">   h3,h4 {color: gray}   body {font-family: sans-serif}   span {color: red}  </style>  <body>   <h3>Alphabetical List of Scandanavian European States</h3>   <h4>Total Number of States: <span>5</span>   </h4>   <ul>    <li>Denmark</li>    <li>Finland</li>    <li>Iceland</li>    <li>Norway</li>    <li>Sweden</li>   </ul>  </body> </html>

Because scandinavia.xml has an XML stylesheet PI, you can load the file directly with a browser that supports client-side XSLT such as Mozilla, Mozilla Firebird, and Netscape. The transformation will occur automatically, as it does in Netscape, shown in Figure 14-2.

Figure 14-2. scandinavia.xml in Netscape
figs/lxsl_1402.gif

Loading an XML document with an embedded stylesheet does not work in IE 6.




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