Embedded Stylesheets


There is one exception to the rule that the stylesheet module must be an XML document. The principal stylesheet module can be embedded within another XML document, typically the document whose style it is defining.

The ability to embed stylesheets within the source document is best regarded as a carryover from CSS. It can be useful if you have a freestanding document that you want to distribute as a self-contained unit, but in most situations it is better to use an external stylesheet that can be used for many different source documents. I sometimes use an embedded stylesheet when I have a "one-of-a-kind" document such as a diary of events to be displayed on a Web site, as it simplifies things to keep the stylesheet and the data together. Some people like to embed stylesheets to reduce download time, but this can be counterproductive, because it means the browser cannot spot that the stylesheet is already present in its cache.

Important  

Not all products support embedded stylesheets. Check the documentation for your particular product before using them.

The outermost element of the stylesheet is still an <xsl:stylesheet> or <xsl:transform> element, but it will no longer be the outermost element of the XML document (that is, the document element). The <xsl:stylesheet> element will generally have an id attribute to identify it, and will be referenced within its containing document using the <?xml-stylesheet?> processing instruction, as shown in the following example.

Embedded Stylesheets
start example

This example shows a stylesheet embedded within an XML source document containing a list of books.

Source

The data file, embedded.xml, containing both source document and stylesheet, is as follows .

  <?xml version="1.0"?>   <!DOCTYPE books [   <!ATTLIST xsl:stylesheet id ID #REQUIRED>   ]>   <?xml-stylesheet type="text/xml" href="#stylel"?>   <books>   <book category="reference">   <author>Nigel Rees</author>   <title>Sayings of the Century</title>   <price>8.95</price>   </book>   <book category="fiction">   <author>Evelyn Waugh</author>   <title>Sword of Honour</title>   <price>12.99</price>   </book>   <book category="fiction">   <author>Herman Melville</author>   <title>Moby Dick<title>   <price>8.99</price>   </book>   <book category="fiction">   <author>J. R. R. Tolkien</author>   <title>The Lord of the Rings</title>   <price>22.99</price>   </book>   <xsl:stylesheet id="style1" version="1.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   <xsl:template match="xsl:stylesheet"/>   <xsl:template match="books">   <html><body>   <h1>A list of books</h1>   <table>   <xsl:apply-templates/>   </table>   </body></html>   </xsl:template>   <xsl:template match="book">   <tr><xsl:apply-templates/></tr>   </xsl:template>   <xsl:template match="author  title  price">   <td><xsl:value-of select="."/></td>   </xsl:template>   </xsl:stylesheet>   </books>  

You can run this stylesheet using Saxon with a command of the form:

  java -jar c:\saxon\saxon7.jar -a embedded.xml  

The -a option tells Saxon to look for an <?xml-stylesheet?> processing instruction in the supplied source document, and to process the source document using that stylesheet. Saxon doesn't allow you (when using the command line interface) to specify the criteria for selecting a specific stylesheet, so if there are several, it uses a composite stylesheet that imports all of them.

Saxon will recognize the relative URI «#style1 » only if it refers to the value of an attribute of type ID. The «id » attribute of the <xsl:stylesheet> element therefore needs to be declared as having this type: this is the purpose of the short <!DOCTYPE> entry. This isn't sufficient to invoke validation of the document (if you do try to invoke validation, by specifying the -v option on the command line, you will get a string of error messages referring to undeclared elements), but it is sufficient to register the attribute type of the «id » attribute.

Output

The output of this embedded stylesheet, when viewed in a Web browser, is shown in Figure 3-2.

click to expand
Figure 3-2
end example
 

Note the empty template rule that matches the <xsl:stylesheet> element. This is needed because without it the stylesheet will try to process itself along with the rest of the document. The empty template rule ensures that when the <xsl:stylesheet> element is matched, no output is generated and its child elements are not processed . You may need to take care to avoid matching other elements in the stylesheet as well. For example, if the stylesheet looks for book titles using an expression such as «//title », this could accidentally match a <title> literal result element within the embedded stylesheet.

An embedded stylesheet module will generally be used as the principal stylesheet module. The standard doesn't explicitly say whether or not an embedded stylesheet can be included or imported into another. In practice, the details of what is supported are likely to vary from one product to another; few of the current products have much to say about embedded stylesheets in their documentation.

If namespace declarations occur outside the embedded stylesheet, they will still be in scope within the embedded stylesheet, which may result in extra namespace nodes being copied into the result tree. You can suppress such namespaces, using the exclude-result-pref ixes attribute on the <xsl:stylesheet> element. If you are using an XML 1.1 parser, another solution would be to undeclare these namespaces (that is, to remove them from the set of namespaces that are in scope) by using a namespace undeclaration of the form «xmlns:pref ix="" ». (With XML 1.0, the only namespace that can be undeclared is the default namespace.)




XSLT 2.0 Programmer's Reference
NetBeansв„ў IDE Field Guide: Developing Desktop, Web, Enterprise, and Mobile Applications (2nd Edition)
ISBN: 764569090
EAN: 2147483647
Year: 2003
Pages: 324

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