|
||||||||
| Chapter 3 - Transforming with Style (Stylesheets, That Is) | |
| XSLT For Dummies | |
| by Richard Wagner | |
| Hungry Minds 2002 | |
Structure of a Stylesheet
An XSLT stylesheet has a
A document is made up of one or more paragraphs. A paragraph is a division of a document that contains one or more sentences that express a unified thought. However, not all sentences in a well-crafted paragraph are created equal. Traditionally, the first
When you look at an XSLT stylesheet, youll find a comparable structure. At the top level is a
stylesheet
, which acts as the overall container for XSLT code, much like a document serves as a container for all the sentences inside it. Whereas a paragraph is the primary component of a document, a
template rule
is the basic building block of a stylesheet. And, like the first sentence in a paragraph, the
match pattern
defines where the template rule is going. Figure 3-1 highlights these
Taking this analogy a step further, there are some elements in a document that arent paragraphs per se. In a normal business letter, for example, the return address, date, greeting, and signature are all distinct, required elements but do not fit the definition of a paragraph. In the same way, an XSLT stylesheet has additional elements, such as xsl:output , that are valid to use but do not fit inside template rules.
|
|||||||||||
|
||||||||
| Chapter 3 - Transforming with Style (Stylesheets, That Is) | |
| XSLT For Dummies | |
| by Richard Wagner | |
| Hungry Minds 2002 | |
Constructing Your XSLT Stylesheet
As you read in Chapter 1, an XSLT stylesheet is a well-
xsl:stylesheet elementThe xsl:stylesheet element serves as the topmost element (or document element) of an XSLT stylesheet. The shell of any XSLT stylesheet consists of:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- XLST code goes here--> </xsl:stylesheet>
As you can see in the
This information
Alternatively, you can also use the xsl:transform element, which is synonymous to xsl:stylesheet :
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- XLST code goes here--> </xsl:transform> Tip Although both xsl:stylesheet and xsl:transform are valid, xsl:stylesheet is by far the most commonly used of the two elements. I use xsl:stylesheet throughout this book.
Top-level elementsAn xsl:stylesheet element contains all the XSLT code that appears in the stylesheet. By and large, the basic building block of the stylesheet is the template rule, defined by using the xsl:template , but you can actually add 11 additional XSLT elements directly inside the xsl:stylesheet element. These elements are called top-level elements and are shown in Table 3-1.
The following code snippet shows an XSLT stylesheet with some of these top-level elements defined:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/> <xsl:preserve-space elements="chapters"/> <xsl:template match="book"> <p><xsl:apply-templates/></p> </xsl:template> <xsl:include href="moretemplates.xsl"/> </xsl:stylesheet> Generally, you can put the top-level elements in any sequence you wish. The XSLT processor processes these elements the same way regardless of order. For example, if I move the elements around, the code generates the same results as the original:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:include href="moretemplates.xsl"/> <xsl:template match="book"> <p><xsl:apply-templates/></p> </xsl:template> <xsl:preserve-space elements="chapters"/> <xsl:output method="html"/> </xsl:stylesheet> However, there are a couple of exceptions to this rule, which tend to occur only in advanced situations. Specifically, when you use the xsl:import element, it must be the first top-level element defined under xsl:stylesheet . Also, in some error checking routines, element placement can become critical.
Comments
A
comment
is text included in your stylesheet for behind-the-scenes use that the XSLT processor ignores during the transformation. In stylesheets, people typically use comments to label a template rule or other part of the code describing its functionality. Just like in HTML, a comment is any text
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- Developed by: R. Wagner --> <!-- Last modified: 04/22 --> <!-- This stylesheet will output an HTML document using several template rules, one defined in this file and the others from moretemplates.xsl --> <!-- Output document to HTML format --> <xsl:output method="html"/> <!-- Preserve space for chapters elements --> <xsl:preserve-space elements="chapters"/> <!--- For each book element, surround its content with HTML paragraph tags --> <xsl:template match="book"> <p><xsl:apply-templates/></p> </xsl:template> <!-- Include more template rules, which are stored in a separate file --> <include href="moretemplates.xsl"/> </xsl:stylesheet> When I say the processor ignores any comment, I mean it. You can even insult the processor with <!-- Hey processor, youre a loser! --> and this still doesnt impact its performance. Now thats service.
Tip
Use comments
|
|||||||||||||||||||||||||||||||||||||