Writing an XSLT Stylesheet

xslt for dummies
Chapter 2 - Writing Your First XSLT Stylesheet
XSLT For Dummies
by Richard Wagner
Hungry Minds 2002

As you find out in Chapter 1, XSLT code is written inside an XSLT stylesheet. To create a new stylesheet, return to X-Factor and choose New XSLT Stylesheet from the File menu. Begin by entering:

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/ Transform" version="1.0"> 

Everything within an XSLT stylesheet is contained inside of an xsl:stylesheet element. Dont worry about the meaning of the xmlns attribute for now. Just type it in. You find out more about it in Chapter 3.

An XSLT stylesheet contains template rules that define what information from the original document goes into the new one and how that information is structured there.

 Tip   Take a quick look at the XSLT code in this example to get a high-level overview of what XSLT is like. But dont get too bogged down by the particulars just yet. Heck, you have the rest of the book for that.

Your first template rule converts the score elements children into attributes:

 <xsl:template match="score"> <score id="{@id}" film="{film}" composer="{composer}" releasedate="{year}"></score> <xsl:apply-templates/> </xsl:template> 

This template rule tells the processor to look for each score element in the document and, when the processor encounters one, to replace the score elements original content with this new structure. The {@id} plugs in the value of the score elements id attribute. The {film} , {composer} , and {year} expressions fill in the value of the child element that matches the text inside the brackets.

Additional template rules are defined to remove original child elements from appearing in the result document:

 <xsl:template match="grade"/> <xsl:template match="film"/> <xsl:template match="year"/> <xsl:template match="composer"/> 

Each of these template rules says: Hey, Mr. Processor. Ill sit this one out. Just treat me as if I were not here. Adding these lines is important, because the processor automatically assumes that element values want to go with the flow and be included in the outputunless you specifically tell it not to include them. So, if you dont add these rules, the processor includes their content in the output document both as elements (their original form) and as attributes (the new form defined in the preceding score template rule).

The final section of the stylesheet is added to maintain scores as the parent of the score elements:

 <xsl:template match="scores"> <scores> <xsl:apply-templates/> </scores> </xsl:template> </xsl:stylesheet> 

In this template rule, the apply-templates element tells the processor to include the contents of the scores element in the output. However, apply-templates doesnt include the tags of the elementonly whats inside the tags. Therefore, I reapply the tags to the output document by placing apply-templates in between the scores start and end tags.

An end xsl:stylesheet tag is added at the bottom of the stylesheet:


Listing 2-1 shows the complete XSLT stylesheet.

Listing 2-1: A Complete XSLT Stylesheet
start example
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- Template rule to convert child elements to attributes --> <xsl:template match="score"> <score id="{@id}" film="{film}" composer="{composer}" releasedate="{year}"/> <xsl:apply-templates/> </xsl:template> <!-- Remove child elements from appearing as usual in the result document --> <xsl:template match="grade"/> <xsl:template match="film"/> <xsl:template match="year"/> <xsl:template match="composer"/> <!-- Maintain the scores element --> <xsl:template match="scores"> <scores> <xsl:apply-templates/> </scores> </xsl:template> </xsl:stylesheet> 
end example

Type this code and save it as score.xsl in the same location as your score.xml file. By convention, an XSLT stylesheet has an extension of .xsl. Alternatively, you can save time and download the score.xsl file from the XSLT For Dummies Web site.

2000-2002    Feedback

XSLT For Dummies
XSLT for Dummies
ISBN: 0764536516
EAN: 2147483647
Year: 2002
Pages: 148

Similar book on Amazon

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