Using an XSLT Template

Earlier in this chapter, you prepared an XSL stylesheet using the <xsl:for-each> formatting instruction. In addition to for-each processing of XML elements, you can use templates to perform tranformations of XML documents. The XSL stylesheet can contain one or more XSLT templates. You can think of templates as special blocks of code that apply to one or more XML tags. Templates contain rules for displaying a particular branch of elments in the XML document. The use of templates is made possible via special formatting tags. In this section, you will learn how to use two of these tags, <xsl:template> and <xsl:apply-templates>, while creating a stylesheet named CoursesMatch.xsl that is based on a single template.

The following exercise demonstrates how to create a stylesheet that uses a template.

1. Open Notepad and enter the code shown below.

2. Save the file as CoursesMatch.xsl. <?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">

<xsl:template match="/">

<STRONG>VBA Course Schedule</STRONG><P/>

<TABLE border="1" cellPadding="4" cellSpacing="2">

<TH>Course Id</TH> <TH>Course Title</TH> <TH>Start Date</TH> <TH>No of Sessions</TH>

<xsl:apply-templates select="//Course"/>

</xsl:template>

<xsl:template rratch="Course"> <TR>

<TD><xsl:value-of select="@ID"/></TD> <TD><xsl:value-of select="Title"/></TD> <TD><xsl:value-of select="Startdate"/></TD> <TD><xsl:value-of select="Sessions"/></TD>

</xsl:template> </xsl:stylesheet>

The first line of the stylesheet code declares that this is an XML document that follows the XML 1.0 standard (version). As mentioned earlier, an XSL document is a type of XML document. While XML documents store data, XSL documents specify how the data should be displayed. The next line declares the namespace to be used to identify the tags in the xsl:stylesheet document (see Tip 17-4 for more information about namespaces). The third line of the code is where the template definition begins. The instruction:

<xsl:template match="/">

defines a template for the entire document. Notice that the <xsl:template> element has a match attribute. The value of the match attribute indicates the nodes (elements) for which this template is appropriate. For example, the special pattern "/" in the match attribute tells the XSL processor that this is the template for the document root. If you wanted to indicate that the template is appropriate only for <Title> elements, you would place "Title" as the value of the match attribute.

Following the definition of the template, the example code uses standard HTML tags to format the document title and table headings. The next instruction:

<xsl:apply-templates select="//Course"/>

directs the XSLT processor to find an appropriate <xsl:template> to apply. The select attribute of the <xsl:apply-templates> element selects the "Course" elements below the document root and asks the processor to find and apply an appropriate template. The text "//Course" is an XPath expression (see Tip 17-8) that tells the XSLT processor to get all the <Course> nodes. The first backslash represents the XML document's root node.

The apply-templates instruction is similar to the for-each instruction that was introduced in an earlier section. The major difference is that with apply-templates you can implement different processing by invoking many templates, depending on the rules that you've defined for each template. The for-each instruction, although easier to read and faster to process, allows only the code between the for-each open and close tags to be applied.

Now that you've defined the template to be used and asked the XSL processor to apply it, you need to write the closing tags </TABLE> and </xsl:template> and proceed to write a template that is appropriate for the "Course" elements.

The instruction:

<xsl:template rratch="Course">

tells the XSL processor that what follows are template rules to be used for all "Course" elements. The <xsl:template> element defines a template to be used to generate the desired output for the specified nodes. The match attribute identifies the source node or nodes to which the template rule applies. The next code fragment:

<TD><xsl:value-of select="@ID"/></TD> <TD><xsl:value-of select="Title"/></TD> <TD><xsl:value-of select="Startdate"/></TD> <TD><xsl:value-of select="Sessions"/></TD>

uses the <xsl:value-of> element that was discussed earlier in this chapter. Recall that this element's select attribute tells the XSL processor to retrieve the value of the specified element or attribute. Each node selected by the <xsl:apply-templates> element will be associated with a template and become the current node for the template.

The above lines are applied to each "Course" node found under the root element and will create a row of table data for each element matching the "Course" pattern.

To finish off your stylesheet, you need to write two closing tags:

</xsl:template> </xsl:stylesheet>

Now that the stylesheet is ready, you can link it to the Courses.xml document, as described in an earlier section titled "Linking an XML Document to a Stylesheet." When you open the Courses.xml document in your browser or inside Microsoft Excel 2002, you should see the data formatted in a table similar to Figure 17-21 (in the next section).

0 0

Post a comment