B.2 A Simple Template Approach

All three of the approaches in this appendix include a source document, stylesheet, and result document.

To execute the example stylesheets in this appendix, you will need an XSLT processor. See the sidebar in Chapter 3 called "Command-Line Tools" for more information on obtaining and using a command-line XSLT processor.

Example B-1 shows the source document for our first example transformation.

Example B-1. An XML source document containing people's names
<people>   <person>     <givenName>Joe</givenName>     <familyName>Johnson</familyName>   </person>   <person>     <givenName>Jane</givenName>     <familyName>Johnson</familyName>   </person>   <person>     <givenName>Jim</givenName>     <familyName>Johannson</familyName>   </person>   <person>     <givenName>Jody</givenName>     <familyName>Johannson</familyName>   </person> </people>

The stylesheet in Example B-2 looks much like the result document that it creates. Specifically, the content of the <xsl:template match="/"> element is essentially an HTML template of the result, along with some placeholders for dynamic content. The dynamic parts of the stylesheet below are highlighted.

Example B-2. A very simple stylesheet for combining people's names with HTML
<xsl:stylesheet version="1.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">       <xsl:output method="html" indent="yes"/>       <xsl:template match="/">     <html>       <head>         <title>Name list</title>       </head>       <table>         <tr>           <th>Given Name</th>           <th>Family Name</th>         </tr>         <xsl:for-each select="/people/person">           <tr>             <td>               <xsl:value-of select="givenName"/>             </td>             <td>               <xsl:value-of select="familyName"/>             </td>           </tr>         </xsl:for-each>       </table>     </html>   </xsl:template>     </xsl:stylesheet>

Let's look at each element in this stylesheet, drilling down into the hierarchy as we go.

First, the root element, xsl:stylesheet, contains one required attribute, the version attribute, which has a value of 1.0. The root element also declares the XSLT namespace, mapped to the xsl prefix. It doesn't matter what prefix you use, of course, but the xsl prefix is the most widely accepted convention:

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

The xsl:output instruction gives the XSLT processor hints on how to serialize the result tree. In this case, method="html" instructs the processor to output the result tree using HTML serialization rules (not necessarily well-formed XML), and indent="yes" instructs it to insert some indentation into the result (to facilitate readability):

  <xsl:output method="html" indent="yes"/>

In this stylesheet, the root element contains an xsl:template element. In XSLT, any xsl:template element that has a match attribute is called a template rule. The value of the match attribute determines what parts of the source document will trigger this template rule. This stylesheet's one and only template rule matches the root node of the source document, as indicated by the value of the simple slash (/):

  <xsl:template match="/">     ...   </xsl:template>

Many stylesheets contain a template rule that matches the root node in this way. It is often called the "root template rule" and is sometimes considered analogous to the main function in a C or Java program, because it is effectively where processing begins. The analogy breaks down however because it is possible to write a stylesheet that doesn't explicitly include a root template rule. But for now, it's okay to think of it as the starting point for all processing.

The "root node" is not the same thing as the "root element." In XPath/XSLT, every document contains a root node, which is the top-level, "invisible" container of everything in the document, including the root, or document, element.

Inside the root template rule, we see some regular HTML markup:

    <html>       <head>         <title>Name list</title>       </head>       <table>         <tr>           <th>Given Name</th>           <th>Family Name</th>         </tr>         ...       </table>     </html>

These elements (as a whole, including their end tags) are called literal result elements, because they effectively create literal html, head, title, etc. elements in the result of the transformation (the result tree). In fact, any element not in the XSLT namespace that occurs inside (as a child or descendant of) the xsl:template element is interpreted as a literal result element.

Next, we see an element in the XSLT namespace, xsl:for-each. This element is an example of an XSLT instruction. An instruction is any element inside (as a child or descendant of) the xsl:template element that is in the XSLT namespace.

  <xsl:for-each select="/people/person">     ...   </xsl:for-each>

The xsl:for-each instruction iterates over certain nodes from the source document (Example B-1), repeating the content inside the element once for each selected node. In this case, the XPath expression /people/person returns a node-set consisting of the person element children of the people element in the source document. For each of those elements, a new table row (tr element) is created in the result, using a tr literal result element:

  <tr>     ...   </tr>

Then, inside each table row, there are two placeholders for dynamic content. The xsl:value-of instruction inserts the string-value of the selected node into the result document. In this case, the first table column (a td element) will contain the value of the givenName element child of the current node in XSLT processing (the person element being processed), and the second table column contains the value of the familyName element:

  <td>     <xsl:value-of select="givenName"/>   </td>   <td>     <xsl:value-of select="familyName"/>   </td>

The HTML result of this transformation is shown in Example B-3.

Example B-3. The result of running the stylesheet in Example B-2 against the XML document in Example B-1
<html>   <head>     <META http-equiv="Content-Type" content="text/html; charset=UTF-16">     <title>Name list</title>   </head>   <table>     <tr>       <th>Given Name</th>       <th>Family Name</th>     </tr>     <tr>       <td>Joe</td>       <td>Johnson</td>     </tr>     <tr>       <td>Jane</td>       <td>Johnson</td>     </tr>     <tr>       <td>Jim</td>       <td>Johannson</td>     </tr>     <tr>       <td>Jody</td>       <td>Johannson</td>     </tr>   </table> </html>

The number and order of table rows in the result (besides the first row, which is the table heading) corresponds to the number and order of person elements in the source document. And, as you can see, the table column values correspond to the values of the givenName and familyName elements in the source document.

The META HTML element in Example B-3 is automatically added to the result of the transformation, according to XSLT's serialization rules for HTML. The XSLT processor does not always have control over (or responsibility for) serialization, but when it does, it must output a META element that indicates the document's character encoding. See http://www.w3.org/TR/xslt#section-HTML-Output-Method for the precise rules.

Office 2003 XML
Office 2003 XML
ISBN: 0596005385
EAN: 2147483647
Year: 2003
Pages: 135

Similar book on Amazon

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