Loops

As demonstrated in the previous sections, contents of the source document can be copied into the target document via the <xsl:apply-templates> element. Often only certain elements are to be copied from the source document. In such cases, the usage of the <xsl:for-each> element is advisable, for instance for the generation of tables of contents. In the following example we generate such a table of contents by using loops:

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>
            <h2>Table Of Contents</h2>
            <xsl:for-each select="Poem">                 (1)
                <a>
                   <xsl:attribute name="href">#<xsl:value-of select="@YearOfPublication"/>
                   </xsl:attribute>                      (2)
                   <xsl:value-of select="Title"/>
                </a>
                <br/>
            </xsl:for-each>
            <xsl:apply-templates/>
        </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>
  </xsl:template>
  <xsl:template match="LastName">
           <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="Title">
      <h2>
          <a>
             <xsl:attribute name="name">                 (3)
             <xsl:value-of select="parent::Poem/@YearOfPublication"/>
             </xsl:attribute>
          </a>
          <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 <xsl:for-each> element generates a loop. For each appropriate expression of the select attribute a run is generated. In practice loops are an option in order to make choices. Often they are used for tables of contents and registers, in which certain information of the entire document has to be collected and restructured.

(2) In our example the loop is used to display all poem titles and to generate an internal reference to them in the corresponding position in the document. In doing so, the <xsl:attribute> element generates attributes in the parent element, in this example for the <a> element. The obligatory name attribute gives its name to the attribute in the target document, here the href attribute, which determines the target of the link in HTML. In the target document the content of the <xsl:attribute> element becomes the value of the href attribute. The # character is used in HTML in order to refer to an anchor which is situated in the same document. Here, the name of the anchor is determined by the year of publication, which means the content of the YearOfPublication attribute.

(3) The applied principle corresponds to the procedure explained above. At this position the required anchors are generated. Here the HTML element <a> is also used.

The target document:

<html>
    <body>
        <h2>Table Of Contents</h2>
        <a href="#1905">Der Panther</a>                   (2)
        <br />
        <a href="#1799">Zauberlehrling</a>                (2)
        <br />
        <a href="#1945">Moritat von Mackie Messer</a>     (2)
        <br />
        <a href="#1890">Pst!</a>                          (2)
        <br />
        <a href="#-750">Ιλιάδα</a>                        (2)
        <br />
        <br />
        <h4>Rainer Maria Rilke</h4>
        <h2>
            <a name="1905" />Der Panther</h2>Im Jardin des Plantes, Paris <p>Sein Blick ist vom Vorübergehn der Stäbe<br />so müd geworden, daß er nichts mehr ält.<br />Ihm ist, als ob es tausend Stäbe gäbe<br />und hinter tausend Stäben keine Welt.<br />              (3)
        </p>
[...]

Browser view of the target document:

Browser view: example - for-each

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