1.4. XSLT Stylesheet Concepts

XSLT stylesheets are best understood according to their structure and the named elements within them. It has always been a hallmark of markup languages that there be a diligent attempt at human-readability for the element-type names and, where possible, other components. With XSLT, this has been fairly well achieved, making it easier to learn and understand XSLT stylesheets.

Let's compare the XML tree structure of a book with that of an XSLT stylesheet, shown side-by-side in Figure 1-3.

Figure 1-3. Comparing XML trees to XSLT trees.


If we rendered the XSLT side of this diagram as a stylesheet, it would look like Example 1-1.

This example shows the basic components of most XSLT stylesheets. The <xsl:stylesheet> element contains two other elements, an <xsl:output> element and an <xsl:template> element (sometimes called a template rule).

The HTML <p> tag simply sends the same tag to the output, as does the <hr> tag.

Another component in our example is the <xsl:apply-templates> element, which is inside the <p> tags.

Using the <book> XML input sample would generate the output shown in Example 1-2.

Notice the structure of the XSLT stylesheet in Example 1-1 that contributed to this output. The <xsl:template> element found a match on a <topic> in the input XML document. This was replaced by the contents of that <xsl:template> element—in this case, the <p></p> and <hr/> elements. Then the content of the <topic> was sent to the output, enclosed within the <p> and </p> open and close tags. This was done by the <xsl:apply-templates> element, which is contained within the <p> tag in the stylesheet.

Example 1-1. XSLT stylesheet as an XML document.
<?xml version="1.0"?>
<xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="1.0">
      <xsl:output method="html" />

      <xsl:template match="topic">
           <p>
                  <xsl:apply-templates />
           </p>
           <hr/>
      </xsl:template>
</xsl:stylesheet>

Example 1-2. Processing a topic.
					INPUT:


<book>
       <intro></intro>
              <chapter>
                     <section>
                         <topic source="song">Xanadu</topic>
                         <topic>Topic 2</topic>
                     </section>
                     <section></section>
              </chapter>
</book>
OUTPUT:


<p>Xanadu</p>
<hr/>
<p>Topic 2</p>
<hr/>

Notice also that the generated output is not well-formed XML. XSLT processors generate XML documents, but do not parse the output document.

1.4.1. Using XSLT to Convert XML to HTML

Let's use some simple XSLT to transform the elements in an XML document to HTML. As an example, we will use the common image of a year, subdivided by a loose notion of an agricultural calendar with planting, harvest, seasons, and months.[4] Example 1-3 illustrates how markup might be used to describe a year.

[4] Of course, variations in planting and harvest, as well as in seasons, are widespread (with the changing global climate, your experience of the weather might be alarmingly different!), so this is a generalized presentation, based primarily on the seasons and weather of the Northern Hemisphere (though our Iowa friends might take us to task on some points!).

Suppose we need to render our year for display in a conventional Web browser. Conversion to HTML is a frequent task for XSLT stylesheets. We'll make this a simple transformation, using the HTML unordered list format (<ul></ul>) for displaying the <month>s.

Using the stylesheet in Example 1-4, we create a simple list of the months with the HTML list item tags (<li></li>) for the output document.

Again, consider the XML document instance structure of this XSLT stylesheet. Taking for granted the required stylesheet components that will be discussed later, the two template rules that remain are simple. The first matches the <year> (using the match="year" attribute) and replaces it with an unordered list tag (<ul></ul>). The <ul> element is a child of the <xsl:template> element. Then, within the <ul> is the instruction—<xsl:apply-templates>—to process any children of <year>.

The <xsl:apply-templates> instruction element basically tells the processor to look for an <xsl:template> for each child of the <year>, recursively addressing each child of a child until all the descendants are processed. If the processor finds a rule for an element, it will follow the instructions in that template rule to process the node. If it does not, it will continue working down through the descendants until it reaches a text node. At this point, the text is sent to the output.

Example 1-3. Marking up a year with XML.
<?xml version="1.0"?>
<year>
      <planting>
             <season period="spring">
                    <month>March</month>
                    <month>April</month>
                    <month>May</month>
              </season>
             <season period="summer">
                    <month>June</month>
                    <month>July</month>
                    <month>August</month>
              </season>
      </planting>
      <harvest>
             <season period="fall">
                    <month>September</month>
                    <month>October</month>
                    <month>November</month>
              </season>
             <season period="winter">
                    <month>December</month>
                    <month>January</month>
                    <month>February</month>
              </season>
      </harvest>
</year>

The second template rule matches each month in the input document. Each <month> tag is replaced by the contents of the template rule (which can be called simply the template), in this case, a list item in HTML (<li></li>). Then, the text node child of each <month> is sent to the output with <xsl:apply-templates>. To be proper HTML, the output would still need a few things, but it will actually display in most browsers as shown in Figure 1-4.

Example 1-4. Basic stylesheet for unordered lists.
<?xml version="1.0"?>
<xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="1.0">
<xsl:output method="html" />
<xsl:template match="year">
          <ul>
                   <xsl:apply-templates />
          </ul>
</xsl:template>
<xsl:template match="month">
          <li>
                   <xsl:apply-templates />
          </li>
</xsl:template>
</xsl:stylesheet>

Figure 1-4. Web view of output from basic stylesheet for unordered lists.


Example 1-5 shows the resulting HTML when an examination of the source for this file is made. Notice that the file does not contain the normal <html> or <body> tags that are used in most HTML files.

Without having element rules to match on the <harvest>, <planting>, and <season> elements, their tags will not be output to the result, nor will the <html> and <body> tags that are required by HTML (but most browsers would properly display this as an unordered list, anyway). It is possible to do quite a few easy XML-to-HTML transformations in this way.

During our examples we have skipped over some very basic concepts that are crucial to understanding XSLT stylesheets. With this basic example of converting XML to HTML, we will now discuss the concepts and terminology that apply to all XSLT stylesheets.

Example 1-5. HTML output from basic stylesheet for unordered lists.
<ul>
      <li>March</li>
      <li>April</li>
      <li>May</li>
      <li>June</li>
      <li>July</li>
      <li>August</li>
      <li>September</li>
      <li>October</li>
      <li>November</li>
      <li>December</li>
      <li>January</li>
      <li>February</li>
</ul>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset