Stylesheet Structure


How is a stylesheet laid out? Are there restrictions on element placement? Do they even contain elements? We'll answer these questions here.

A stylesheet contains a set of template rules. A template rule has two parts : a pattern, which is matched against nodes in the source tree, and a template, which details a portion of the result tree and how it is constructed (transformed). This enables a stylesheet to be applied to a wide document set that has similar source tree structures.

Associating Stylesheets with XML Documents

One note of importance here is that the XSLT specification does not designate how an XSLT stylesheet is associated with an XML document. The W3C does recommend, however, that XSL processors follow the method described in the W3C document "Associating Stylesheets with XML Documents Version 1.0" located at http://www.w3.org/1999/06/REC-xml-stylesheet-19990629. This document was made a W3C recommendation on June 29, 1999.

In essence, this document states that stylesheets should be associated with an XML document by using a processing instruction whose target is xml-stylesheet . This is similar to the behavior of the HTML 4.0 tag <LINK REL="stylesheet"> . Here's an example:

 <?xml-stylesheet href="mystyle.css" type="text/css"?> 

Table 2.5 defines the following attributes for the xml-stylesheet statement.

Table 2.5. xml-stylesheet Statement Attributes

Attribute

Required

Default

Description

href

Yes

n/a

Specifies the URL of the stylesheet document. Example: href="mystylesheet.xsl" .

type

Yes

n/a

Specifies that this is a stylesheet document. Example: type="text/css" .

title

No

n/a

Specifies a title for this stylesheet.

media

No

n/a

Specifies that this stylesheet be designed for use with the specified media. For example, graphical displays, television screens, handheld devices, speech-based browsers, braille-based tactile devices, and so on. The currently defined media are as follows :

screen

Intended for nonpaged computer screens.

tty

Intended for media using a fixed-pitch character grid, such as teletypes, terminals, or portable devices with limited display capabilities.

tv

Intended for television-type devices (low resolution, color , limited scrollability).

projection

Intended for projectors.

handheld

Intended for handheld devices (small screen, monochrome, bitmapped graphics, limited bandwidth).

print

Intended for paged, opaque material and for documents viewed onscreen in print preview mode.

braille

Intended for braille tactile feedback devices.

aural

Intended for speech synthesizers.

all

Suitable for all devices.

charset

No

n/a

The charset parameter identifies a character encoding, which is a method of converting a sequence of bytes into a sequence of characters . For one byte per character, this is straightforward. No conversion is necessary. For two (Unicode) or three (Universal Naming Convention) bytes per character, this becomes important.

alternate

No

"no"

Designates substitute versions for the document in which the link occurs, in case a specified stylesheet is unavailable.

To me, the media descriptor is the most interesting. It's rarely used today, but I think you can see there is a lot of potential for the various categories listed. For example, an XSL stylesheet could be utilized to transform an XML document into a Wireless Application Protocol (WAP) document.

Those of you who are HTML gurus might have noticed that the linguistics of the attributes are exactly as with <LINK REL="stylesheet"> in HTML 4.0, with the exception of the alternate pseudo attribute. If alternate="yes" is specified, then the processing instruction has the linguistics of <LINK REL="alternate stylesheet"> instead of <LINK REL="stylesheet'> .

Let's look at some of the tags and their place in a stylesheet and then move on to a detailed discussion of these things called templates.

<xsl:stylesheet>

This is the base tag for an XSL stylesheet. It encompasses the entire document. Here is its definition with attributes:

 <xsl:stylesheet>      version = number>      <!-- Content: (xsl:import, other top-level-elements) -->  </xsl:stylesheet> 

An <xsl:stylesheet> element must have a version attribute, indicating the version of XSLT that the stylesheet requires. As of this writing, the version of XSLT should be 1.0. I think this value will change very soon to 1.1. This requirement will allow compatibility with future versions of XSLT. Stylesheets can check for the version number and process accordingly . As you know, in any programming language, as versions change so does the language.

<xsl:transform> is allowed as a synonym for <xsl:stylesheet> , and it has the identical attributes and definitions as <xsl:stylesheet> .

Layout and Order of Elements

The <xsl:stylesheet> element can contain the following types of elements:

  • xsl:import

  • xsl:include

  • xsl: strip-space

  • xsl: preserve-space

  • xsl:output

  • xsl:key

  • xsl:decimal-format

  • xsl: namespace-alias

  • xsl:attribute-set

  • xsl:variable

  • xsl:param

  • xsl:template

We'll be covering most of these in the rest of this chapter.

An element occurring as an immediate child of the <xsl:stylesheet> element is called a top-level element. The order in which these children occur isn't important except for xsl:import elements. If an xsl:import element occurs in a stylesheet, it must be the immediate child of the <xsl:stylesheet> element. The other elements can be ordered in any way you want, and as a result, it isn't necessary for stylesheet creation tools to provide control over the order in which the elements occur. (This should serve to explain the comment tag that's in the example in the preceding section.)

Example of Stylesheet Structure

Listing 2.4 is an example of the structure of a stylesheet. I've left this as a bare-bones example to illustrate element layout. Ellipses ( ) indicate where I've left out attribute values or content. Although this example shows one of each type of allowed element, stylesheets do not have to have all these elements, and there can be more than one of each of them.

Listing 2.4 Sample Stylesheet Structure
 <xsl:stylesheet version="1.0"                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    <xsl:import href="..."/>    <xsl:include href="..."/>    <xsl:strip-space elements="..."/>    <xsl:preserve-space elements="..."/>    <xsl:output method="..."/>    <xsl:key name="..." match="..." use="..."/>    <xsl:decimal-format name="..."/>    <xsl:namespace-alias stylesheet-prefix="..." result-prefix="..."/>    <xsl:attribute-set name="...">    </xsl:attribute-set>    <xsl:variable name="...">...</xsl:variable>    <xsl:param name="...">...</xsl:param>    <xsl:template match="...">    </xsl:template>    <xsl:template name="...">    </xsl:template>  </xsl:stylesheet> 

Again, we'll be discussing most of these elements shortly.



XML and SQL Server 2000
XML and SQL Server 2000
ISBN: 0735711127
EAN: 2147483647
Year: 2005
Pages: 104
Authors: John Griffin

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