A Simple Template Approach

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

Tip

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"> ...

Get Office 2003 XML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.