Section 18.6.  Top-level instructions

Prev don't be afraid of buying books Next

18.6. Top-level instructions

XSLT also allows you to do more complex things. It supports all of XPath, sophisticated selections, stylesheet reuse and many other advanced features. We will introduce a few of these in this section.

Top-level instructions are those that go directly in the xsl:stylesheet element. They do not apply to any particular template rule but rather declare behaviors, variables and other things that affect the entire stylesheet. Except for xsl:import instructions, which we'll describe shortly, the order of top-level statements is not important.

18.6.1 Combining stylesheets

There are two ways to combine stylesheets: inclusion and import.

18.6.1.1 Including other stylesheets

The xsl:include instruction includes another stylesheet. Stylesheets may not include themselves directly or indirectly. The instructions in an included stylesheet are treated exactly as if they had been typed directly in the including stylesheet. They are not second-class in any sense. Example 18-18 demonstrates the inclusion of other stylesheets through both absolute and relative URIs.

Example 18-18. Including another stylesheet
 <xsl:include href="http://.../currency.xsl"/> <xsl:include href="bonds.xsl"/> 

18.6.1.2 Importing from other stylesheets

Importing is a little bit different from including. Just as in the real world, there are restrictions on imports! In the XSLT context that means imports are second-class. Imported rules only take effect when no rule in the main stylesheet matches. Also, import statements earlier in the document take precedence over later ones.

Import instructions must go at the top of a stylesheet, preceding any other top-level instructions and the xsl:template elements. A stylesheet must not directly or indirectly import itself. Example 18-19 demonstrates the importation of other stylesheets through both absolute and relative URIs.

Example 18-19. Importing another stylesheet
 <xsl:import href="http://.../stocks.xsl"/> <xsl:import href="credit-cards.xsl"/> 

18.6.2 Whitespace handling

Whitespace nodes are ordinary text nodes that happen to have only whitespace (tab, space, newline) characters in them.

In machine-to-machine applications, whitespace nodes are usually irrelevant. Even in publishing applications, some whitespace is not important for processing. A blank line between two section elements is just intended to make the source XML easier to read. It is not intended to affect what is seen by the ultimate readers of the rendered document.

XSLT has a feature that allows you to strip out these whitespace-only nodes based on the elements in which they occur. The xsl:strip-space instruction strips space from elements of specified types in the source document. Example 18-20 shows how you would strip space from address and date elements.

Example 18-20. Stripping space
 <xsl:strip-space elements="address date"/> 

In some vocabularies, all element types are so-called space-stripping. You can accomplish this by using an asterisk in the xsl:strip-space instruction (Example 18-21

Example 18-21. Strip space from all elements
 <xsl:strip-space elements="*"/> 

If a vocabulary has only a few non-whitespace stripping elements (whitespace-preserving elements), you can selectively override the blanket stripping statement with the xsl:preserve-space instruction. By default, whitespace is preserved.

18.6.3 Output descriptions

The xsl:output instruction sets various options that control what the stylesheet should generate as output. The main attribute in the instruction is the method attribute, which typically takes a value like "xml","html" or "text".

"xml" is appropriate for (surprise!) generating XML; it is the default. "html" uses html conventions for empty-elements, processing instructions and similar constructs. The "text" output is useful when you want to generate plain text without the XSLT processor representing delimiter characters such as less-than signs (<) as &lt;, and so forth.

Other attributes can control whether the output is indented for "pretty printing", what character encoding to use, whether to add an XML declaration and/or document type declaration and other (even more obscure) output options. Most XSLT stylesheets will not need to change these options from their defaults.

But if you find that the output of your stylesheet is not quite what you would expect, then xsl:output may have the answer for you.

18.6.4 Numeric formats

The xsl:decimal-format instruction allows you to describe how decimal numbers will be printed by your stylesheet. For instance, you can use this to change the character that separates the decimals from the integral part of the number. You could set that option to period for North Americans and comma for Europeans.

Other options allow you to change the "grouping-separator" between the billions, millions and thousands and to choose characters or strings to represent "infinity", "minus", "percent", "per-mille" (per thousand), "the zero digit" and "Not a number". The latter is used for error handling.

18.6.5 Attribute sets

xsl:attribute-set allows you to define a reusable set of attributes. If you have several different types of images that must share certain attributes then it is more efficient to define those in an attribute set than to repeat them all in each template rule. Example 18-22 demonstrates the basic idea.

Example 18-22. Reusable set of attribute values
 <xsl:attribute-set name="big-image">     <xsl:attribute name="width">500px</xsl:attribute>     <xsl:attribute name="height">500px</xsl:attribute> </xsl:attribute-set> <xsl:attribute-set name="small-image">     <xsl:attribute name="width">100px</xsl:attribute>     <xsl:attribute name="height">100px</xsl:attribute> </xsl:attribute-set> ... in a template ... <img xsl:use-attribute-sets="big-image"> 

18.6.6 Namespace alias

Just as xsl:output helps you to solve the problem of how to treat a less-than sign as an ordinary data character, xsl:namespace-alias helps you to treat a namespace prefixed literal result element as just a literal result element, even if the namespace happens to be XSLT! This sounds strange but it could happen if you were writing an XSLT stylesheet that generates an XSLT stylesheet. Needless to say, this is not a common situation so we will not dwell on it further.

18.6.7 Keys

Keys are a performance-enhancement concept borrowed from the database world. Their main use is to provide a (possibly) faster way to reference elements by the values of their attributes or subelements. You get to decide which attribute or element values form the key by declaring it, as shown in Example 18-23.

Example 18-23. Declaring a key
 <xsl:key name="employees-by-ssn"          match="employee"          use="social-security"/> 

Keys don't let you do anything that can't be done with normal XPath addressing, which is why we're not explaining them in detail. Their major benefit is to allow the XSLT processor to build a lookup table to attempt to speed up keyed references. Whether the attempt succeeds depends on the number of keys, how often they are referenced, and the complexity of the XPath expressions they replace.

Keys can also simplify a stylesheet by letting you use simple key names in place of possibly complex XPath expressions. Not all XSLT processors support keys.

Amazon


XML in Office 2003. Information Sharing with Desktop XML
XML in Office 2003: Information Sharing with Desktop XML
ISBN: 013142193X
EAN: 2147483647
Year: 2003
Pages: 176

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