WebReference.com - Excerpt from Inside XSLT, Chapter 2, Part 3 (3/5) | WebReference

WebReference.com - Excerpt from Inside XSLT, Chapter 2, Part 3 (3/5)

To page 1To page 2current pageTo page 4To page 5
[previous] [next]

Inside XSLT

Matching Elements in Templates

To indicate what node or nodes you want to work on in a template, XSLT supports various ways of matching or selecting nodes. You set the match attribute of <xsl:template> to a pattern that matches the name of the node or nodes you want to work with. Chapter 3, which is on templates, shows you how to create patterns. For example, the pattern "/" stands for the root node. The pattern "*" matches any element node. The pattern "PLANET" matches all <PLANET> element nodes, and so on.

To get started, I'll create a short example that replaces the root node-and therefore the whole document-with an HTML page. The first thing I do is create a template with the <xsl:template> element, setting the match attribute to the pattern to match "/":

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

When the root node is matched, the template is applied to that node. In this case, I want to replace the root node with an HTML document, so I just include that HTML document directly as the content of the <xsl:template> element:

Listing 2.2: A Trivial Transformation

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <HTML>
      <HEAD>
        <TITLE>
          A trivial transformation
        </TITLE>
      </HEAD>
      <BODY>
        This transformation has replaced
        the entire document.
      </BODY>
    </HTML>
  </xsl:template>
</xsl:stylesheet>

And that's all it takes; by using the <xsl:template> element, I've set up a rule in the stylesheet. When the XSL processor reads the document, the first node it sees is the root node. This rule matches that root node, so the XSL processor copies the literals to the result tree that will give us the HTML doc and replace it with the HTML document, producing the following result:

<HTML>
  <HEAD>
    <TITLE>
      A trivial transformation
    </TITLE>
  </HEAD>
  <BODY>
    This transformation has replaced
    the entire document.
  </BODY>
</HTML>

This example illustrates a first, rudimentary transformation. So far, it has created only a simple stylesheet with one <xsl:template> element, and that element contains only a literal result element. All this example has done is to replace the whole XML document with an HTML document, which is not too exciting. Next, we'll see how recursive processing works with the <xsl:apply-templates> element.


To page 1To page 2current pageTo page 4To page 5
[previous] [next]

Created: September 26, 2001
Revised: September 26, 2001


URL: http://webreference.com/authoring/languages/xml/insidexslt/chap2/3/3.html