Die Lösungen zu den XSLT-Aufgaben

Lösung zur Aufgabe 1a:

<?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="Gedichtsammlung">
    <html>
      <body><h1>Gedichtsammlung</h1> 
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="Autor">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="Vorname">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="Nachname">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="Titel">
    <h2>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="Strophe">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="Vers">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="Gedicht">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="Fett">
    <b>
      <xsl:apply-templates/>
    </b>
  </xsl:template>
  <xsl:template match="Untertitel">
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
</xsl:stylesheet>

Lösung zur Aufgabe 1b:

Mit Hilfe des Elements <xsl:apply-templates> werden weitere Templates gesucht, die auf Unterelemente passen. Wird keines gefunden wird der Inhalt (Text) kopiert.

Lösung zur Aufgabe 1c:

<?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="Gedichtsammlung">
    <html>
      <body><h1>Gedichtsammlung</h1> 
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="Autor">
    <br/>
    <h4><!-- IhrVorname IhrNachname --></h4>
  </xsl:template>
  <xsl:template match="Vorname">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="Nachname">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="Titel">
    <h2>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="Strophe">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="Vers">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="Gedicht">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="Fett">
    <b>
      <xsl:apply-templates/>
    </b>
  </xsl:template>
  <xsl:template match="Untertitel">
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
</xsl:stylesheet>

Lösung zur Aufgabe 2a:

Dadurch wurde nur ein bestimmter Teil des Baumes selektiert. Alle anderen Unterelemente werden ignoriert.

Lösung zur Aufgabe 2b:

<?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="Gedichtsammlung">
    <html>
      <body>
        <h1>Gedichtsammlung</h1>
        <p>
          <xsl:call-template name="Titel"/>
          <xsl:call-template name="Name"/>
        </p>
      </body>
    </html>
  </xsl:template>
  <xsl:template name="Titel">
    <br/>
Titel: <xsl:apply-templates select="Gedicht/Titel"/>
  </xsl:template>
  <xsl:template match="Titel">
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template name="Name">
    <br/>
Autor: <xsl:apply-templates select="Gedicht/Autor/Vorname | Gedicht/Autor/Nachname"/>
  </xsl:template>
  <xsl:template match="Titel">
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="Vorname">
    <br/>
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="Nachname">
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>

Lösung zur Aufgabe 2c:

Elemente, die in der Liste der selektierten Elemente nicht aufgeführt werden, werden nicht in das Zieldokument übernommen.

Lösung zur Aufgabe 3:

<?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="Gedichtsammlung">
    <html>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="Autor">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="Vorname">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="Nachname">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="Titel">
    <h2>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="Strophe">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="Vers">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="Gedicht">
  Erscheinungsjahr: 
    <xsl:value-of select="@Erscheinungsjahr"/>
    <br/><xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>

Lösung zur Aufgabe 4:

<?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="Gedichtsammlung">
    <html>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="Autor">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="Vorname">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="Nachname">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="Titel">
    <h2>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="Strophe">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="Vers">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="Gedicht">
    <br/>
    <xsl:if test="@Sprache='el'">Das Gedicht ist auf Griechisch.
    <xsl:apply-templates/>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

Lösung zur Aufgabe 5a:

<?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="/">
    <xsl:for-each select="//Gedicht">
      <xsl:sort order="ascending" data-type="number" select="@Erscheinungsjahr"/>
      <xsl:sort order="ascending" data-type="text" select="Autor/Nachname"/>
      <xsl:apply-templates/>
    </xsl:for-each>
  </xsl:template>
  <xsl:template match="Autor">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="Vorname">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="Nachname">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="Titel">
    <h2>
        <xsl:apply-templates/>
    </h2>
    <p>Erscheinungsjahr: <xsl:value-of select="parent::Gedicht/@Erscheinungsjahr"/></p>
  </xsl:template>
  <xsl:template match="Strophe">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="Vers">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="Gedicht">
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>
Browser-Ansicht zur Lösung von Aufgabe 5a (Ausschnitt):

Browser-Ansicht: Lösung 5a

Lösung zur Aufgabe 5b:

<?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="/">
    <h1>Inhaltsverzeichnis</h1>
    <p>
      <xsl:for-each select="//Gedicht">
        <xsl:sort order="ascending" data-type="number" select="@Erscheinungsjahr"/>
        <xsl:sort order="ascending" data-type="text" select="Autor/Nachname"/>
        <a href="#{Titel}">
          <xsl:value-of select="Titel"/>
        </a><br/>
      </xsl:for-each>
    </p>
    <xsl:for-each select="//Gedicht">
      <xsl:sort order="ascending" data-type="number" select="@Erscheinungsjahr"/>
      <xsl:sort order="ascending" data-type="text" select="Autor/Nachname"/>
      <xsl:apply-templates/>
    </xsl:for-each>
  </xsl:template>
  <xsl:template match="Autor">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="Vorname">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="Nachname">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="Titel">
    <h2>
      <a name="{.}"/>
      <xsl:apply-templates/>
    </h2>
    <p>Erscheinungsjahr: <xsl:value-of select="parent::Gedicht/@Erscheinungsjahr"/></p>
  </xsl:template>
  <xsl:template match="Strophe">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="Vers">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="Gedicht">
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>
Browser-Ansicht zur Lösung von Aufgabe 5b:

Browser-Ansicht: Lösung 5b

Lösung zur Aufgabe 6a:

<?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="Gedichtsammlung">
    <html>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="Autor">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="Vorname">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="Nachname">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="Titel">
    <h2>
      <xsl:number format="I" level="any"/>
      <xsl:text> </xsl:text>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="Strophe">
    <p>
      <b>Strophe <xsl:number/>
      </b>
      <br/>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="Vers">
    <b>Vers <xsl:number level="multiple" count="//Gedicht | //Gedicht/Strophe | //Gedicht/Strophe/Vers" format="I.1.1"/></b>
    <br/>
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="Gedicht">
    <br/>
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>
Browser-Ansicht zur Lösung von Aufgabe 6a:

Browser-Ansicht: Lösung 6a

Lösung zur Aufgabe 6b:

<?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="Gedichtsammlung">
    <html>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="Autor">
    <br/>
    <table>
      <col width="30px"/>
      <tbody>
        <tr>
          <td style="color:#G9G9G9; font-size:9pt">
            <xsl:number count="//Titel | //Vers | //Autor | //Untertitel" format="1" level="any"/>
          </td>
          <td style="font-size:12pt; font-weight:bold">
            <xsl:apply-templates/>
          </td>
        </tr>
      </tbody>
    </table>
  </xsl:template>
  <xsl:template match="Vorname">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="Nachname">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="Titel">
    <table>
      <col width="30px"/>
      <tbody>
        <tr>
          <td style="color:#G9G9G9; font-size:9pt">
            <xsl:number count="//Titel | //Vers | //Autor | //Untertitel" format="1" level="any"/>
          </td>
          <td style="font-size:18pt; font-weight:bold">
            <xsl:apply-templates/>
          </td>
        </tr>
      </tbody>
    </table>
  </xsl:template>
  <xsl:template match="Untertitel">
    <table>
      <col width="30px"/>
      <tbody>
        <tr>
          <td style="color:#G9G9G9; font-size:9pt">
            <xsl:number count="//Titel | //Vers | //Autor | //Untertitel" format="1" level="any"/>
          </td>
          <td style="font-size:14pt; font-weight:bold">
            <xsl:apply-templates/>
          </td>
        </tr>
      </tbody>
    </table>
  </xsl:template>
  <xsl:template match="Strophe">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="Vers">
    <table>
      <col width="30px"/>
      <tbody>
        <tr>
          <td style="color:#G9G9G9; font-size:9pt">
            <xsl:number count="//Titel | //Vers | //Autor | //Untertitel" format="1" level="any"/>
          </td>
          <td style="font-size:10pt">
            <xsl:apply-templates/>
          </td>
        </tr>
      </tbody>
    </table>
  </xsl:template>
  <xsl:template match="Gedicht">
    <br/>
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>
Browser-Ansicht zur Lösung von Aufgabe 6b:

Browser-Ansicht: Lösung 6b

Lösung zur Aufgabe 7:

<?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="Gedichtsammlung">
    <html>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="Autor">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="Vorname">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="Nachname">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="Titel">
    <h2>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="Strophe">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="Vers">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="Gedicht">
    <xsl:variable name="Jahr">
      <xsl:value-of select="starts-with(@Erscheinungsjahr,'-')"/>
    </xsl:variable>
    <div>
      <xsl:if test="$Jahr='true'">
        <xsl:attribute name="style">background-color:red</xsl:attribute>
      </xsl:if>
      <br/>
      <xsl:apply-templates/>
    </div>
  </xsl:template>
</xsl:stylesheet>
Browser-Ansicht zur Lösung von Aufgabe 7 (Ausschnitt):

Browser-Ansicht: Lösung 7

Lösung zur Aufgabe 8a:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="Lösung8b.xslt"/>  
  <xsl:output encoding="iso-8859-1" version="1.0"/>
  <xsl:template match="Gedichtsammlung">
    <html>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Lösung zur Aufgabe 8b:

<?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="Autor">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="Vorname">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="Nachname">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="Titel">
    <h2>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="Strophe">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="Vers">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="Gedicht">
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>
Tipp der data2type-Redaktion:
Zum Thema XSLT bieten wir auch folgende Schulungen zur Vertiefung und professionellen Fortbildung an:

Copyright © dpunkt.verlag GmbH 2004
Für Ihren privaten Gebrauch dürfen Sie die Online-Version ausdrucken.
Ansonsten unterliegt dieses Kapitel aus dem Buch "XSL-FO in der Praxis" denselben Bestimmungen, wie die gebundene Ausgabe: Das Werk einschließlich aller seiner Teile ist urheberrechtlich geschützt. Alle Rechte vorbehalten einschließlich der Vervielfältigung, Übersetzung, Mikroverfilmung sowie Einspeicherung und Verarbeitung in elektronischen Systemen.

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