Template rules

Template rules are templates for the transformation of the source tree into the result tree. They consist of two parts, one of them being referred to as pattern (search pattern) and the other as template (substitution pattern), which is operated when the search pattern has a corresponding match in the source tree.

Template composition

template composition

In order to demonstrate the mode of operation of templates, the stylesheet example will be extended by several small templates.

The stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output encoding="iso-8859-1" />
    <xsl:template match="CollectionOfPoems">
        <html>
            <body>
                <xsl:apply-templates/>       (1)
            </body>
        </html>
    </xsl:template>
    <xsl:template match="Author">
        <br/>
        <h4>
            <xsl:apply-templates/>
        </h4>
    </xsl:template>
    <xsl:template match="FirstName">
        <xsl:apply-templates/>
        <xsl:text> </xsl:text>               (2)
    </xsl:template>
    <xsl:template match="LastName">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="Title">
        <h2>
            <xsl:apply-templates/>
        </h2>
    </xsl:template>
    <xsl:template match="Strophe">
        <p>
            <xsl:apply-templates/>
        </p>
    </xsl:template>
    <xsl:template match="Verse">
        <xsl:apply-templates/>
        <br/>
    </xsl:template>
</xsl:stylesheet>

(1) The content of the <CollectionOfPoems> element including all its sub-elements with their contents is copied into this position. The processor searches for sub-elements all the way to the very character basis and for potentially matching templates. Since in this example templates are formulated for all subordinated elements, the content of these templates will be copied in sequence into the target structure. If there was no matching template for a subordinated element in the order of the source document, the processor would nonetheless copy the mere content of this element.

(2) Here, with <xsl:text> a space character is copied into the target document.

Browser view of the target document:

Browser view: templates

Exercise 1

a) Write templates for the Poems.xml file. All occurring elements should be supported.
In addition, a headline called "Collection of poems" should be generated appearing at the beginning of the generated document.

b) Why is the text of an element displayed although no template was defined for it?

c) Delete all author data and enter your name as the author.

> > to the solutions of exercise 1

   

Named templates

In the given example the templates relate to elements (nodes in the XML hierarchy of the source document). They are specified with the <xsl:template> element and the match attribute. Templates can also be specified independently of such nodes. The match attribute is replaced by the name attribute with an optional name, e.g. Original. The <xsl:template> element applies for both template versions. The versions are called up differently: node-related templates are called up with the <xsl:apply-templates> element. Named templates are called up with the <xsl:call-template> element and with the name attribute, e.g. <xsl:call-template name="Original">. The match attribute (for <xsl:apply-templates>) effects that a search pattern is looked for in the XML document and the template is only executed in case a match occurs. The name attribute (for <xsl:call-template>) effects that there is only a search for the template with the same name in the stylesheet, which is executed at the position of the call.

The stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output encoding="iso-8859-1" version="1.0"/>
    <xsl:template match="CollectionOfPoems">
        <html>
            <body>
                <h1>Collection of poems</h1>
                <p>
                   <xsl:call-template name="Title"/>
                </p>
            </body>
        </html>
    </xsl:template>
    <xsl:template name="Title">
        <br/>Title: <xsl:apply-templates select="Poem/Title"/>
    </xsl:template>
    <xsl:template match="Title">
        <h4>
            <xsl:apply-templates/>
        </h4>
    </xsl:template>
</xsl:stylesheet>
Exercise 2

a) What is the reason for the supplementation of the select attribute in the <xsl:apply-templates select="Poem/Title"/> line?

b) Rewrite your stylesheet with the result that the first name and the last name of the author also appear.
Remark: The OR operation of several elements can be achieved with "|".

c) What happens if an element is not listed?

> > to the solutions of exercise 2

   

<< back next >>

 


Copyright © dpunkt.verlag GmbH 2004
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "XSL-FO in der Praxis" ("XSL-FO in practice") is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.

dpunkt.verlag GmbH, Ringstraße 19B, 69115 Heidelberg, fon 06221-14830, fax 06221-148399, hallo@dpunkt.de