O'Reilly logo

Office 2003 XML by Evan Lenz, Mary McRae, Simon St. Laurent

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required