Soluciones a los ejercicios de XSLT

Solución del ejercicio 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="antologia">
    <html>
      <body><h1>Antología</h1> 
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="autor">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="nombre">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="apellido">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="titulo">
    <h2>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="estrofa">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="verso">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="poema">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="negrilla">
    <b>
      <xsl:apply-templates/>
    </b>
  </xsl:template>
  <xsl:template match="subtitulo">
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
</xsl:stylesheet>

Solución del ejercicio 1b:

Con ayuda del elemento <xsl:apply-templates> se buscan otras plantillas que concuerden con los subelementos. Si no se encuentra ninguna se copiará el contenido (texto).

Solución del ejercicio 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="antologia">
    <html>
      <body><h1>Antología</h1> 
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="autor">
    <br/>
    <h4><!-- sunombre suapellido --></h4>
  </xsl:template>
  <xsl:template match="nombre">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="apellido">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="titulo">
    <h2>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="estrofa">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="verso">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="poema">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="negrilla">
    <b>
      <xsl:apply-templates/>
    </b>
  </xsl:template>
  <xsl:template match="subtitulo">
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
</xsl:stylesheet>

Solución del ejercicio 2a:

De esta manera se selecciona sólo una parte determinada del árbol y se ignoran todos los demás subelementos.

Solución del ejercicio 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="antologia">
    <html>
      <body>
        <h1>Antología</h1>
        <p>
          <xsl:call-template name="titulo"/>
          <xsl:call-template name="nombre"/>
        </p>
      </body>
    </html>
  </xsl:template>
  <xsl:template name="titulo">
    <br/>
Título: <xsl:apply-templates select="poema/titulo"/>
  </xsl:template>
  <xsl:template match="titulo">
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template name="nombre">
    <br/>
Autor: <xsl:apply-templates select="poema/autor/nombre | poema/autor/apellido"/>
  </xsl:template>
  <xsl:template match="titulo">
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="nombre">
    <br/>
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="apellido">
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>

Solución del ejercicio 2c:

Los elementos que no figuren en la lista de elementos seleccionados no se copiarán en el documento de salida.

Solución del ejercicio 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="antologia">
    <html>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="autor">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="nombre">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="apellido">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="titulo">
    <h2>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="estrofa">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="verso">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="poema">
  Año de publicación: 
    <xsl:value-of select="@añodepublicacion"/>
    <br/><xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>

Solución del ejercicio 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="antologia">
    <html>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="autor">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="nombre">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="apellido">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="titulo">
    <h2>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="estrofa">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="verso">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="poema">
    <br/>
    <xsl:if test="@idioma='el'">El poema está en griego.
    <xsl:apply-templates/>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

Solución del ejercicio 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="//poema">
      <xsl:sort order="ascending" data-type="number" select="@añodepublicacion"/>
      <xsl:sort order="ascending" data-type="text" select="autor/apellido"/>
      <xsl:apply-templates/>
    </xsl:for-each>
  </xsl:template>
  <xsl:template match="autor">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="nombre">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="apellido">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="titulo">
    <h2>
        <xsl:apply-templates/>
    </h2>
    <p>Año de publicación: <xsl:value-of select="parent::poema/@añodepublicacion"/></p>
  </xsl:template>
  <xsl:template match="estrofa">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="verso">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="poema">
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>
Solución del ejercicio 5a en la vista del navegador (Fragmento):

Vista del navegador: solución 5a

Solución del ejercicio 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>Índice</h1>
    <p>
      <xsl:for-each select="//poema">
        <xsl:sort order="ascending" data-type="number" select="@añodepublicacion"/>
        <xsl:sort order="ascending" data-type="text" select="autor/apellido"/>
        <a href="#{titulo}">
          <xsl:value-of select="titulo"/>
        </a><br/>
      </xsl:for-each>
    </p>
    <xsl:for-each select="//poema">
      <xsl:sort order="ascending" data-type="number" select="@añodepublicacion"/>
      <xsl:sort order="ascending" data-type="text" select="autor/apellido"/>
      <xsl:apply-templates/>
    </xsl:for-each>
  </xsl:template>
  <xsl:template match="autor">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="apellido">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="apellido">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="titulo">
    <h2>
      <a name="{.}"/>
      <xsl:apply-templates/>
    </h2>
    <p>Año de publicación: <xsl:value-of select="parent::poema/@añodepublicacion"/></p>
  </xsl:template>
  <xsl:template match="estrofa">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="verso">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="poema">
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>
Solución del ejercicio 5b en la vista del navegador:

Vista del navegador: solución 5b

Solución del ejercicio 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="antologia">
    <html>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="autor">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="nombre">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="apellido">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="titulo">
    <h2>
      <xsl:number format="I" level="any"/>
      <xsl:text> </xsl:text>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="estrofa">
    <p>
      <b>Estrofa <xsl:number/>
      </b>
      <br/>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="Vers">
    <b>Verso <xsl:number level="multiple" count="//poema| //poema/estrofa| //poema/estrofa/verso" format="I.1.1"/></b>
    <br/>
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="poema">
    <br/>
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>
Solución del ejercicio 6a en la vista del navegador:

Vista del navegador: solución 6a

Solución del ejercicio 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="antologia">
    <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="//titulo | //verso| //autor | //subtitulo" 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="nombre">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="apellido">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="titulo">
    <table>
      <col width="30px"/>
      <tbody>
        <tr>
          <td style="color:#G9G9G9; font-size:9pt">
            <xsl:number count="//titulo | //verso | //autor | //subtitulo" 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="subtitulo">
    <table>
      <col width="30px"/>
      <tbody>
        <tr>
          <td style="color:#G9G9G9; font-size:9pt">
            <xsl:number count="//titulo | //verso | //autor | //subtitulo" 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="estrofa">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="verso">
    <table>
      <col width="30px"/>
      <tbody>
        <tr>
          <td style="color:#G9G9G9; font-size:9pt">
            <xsl:number count="//titulo | //verso | //autor | //subtitulo" format="1" level="any"/>
          </td>
          <td style="font-size:10pt">
            <xsl:apply-templates/>
          </td>
        </tr>
      </tbody>
    </table>
  </xsl:template>
  <xsl:template match="poema">
    <br/>
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>
Solución del ejercicio 6b en la vista del navegador:

Vista del navegador: solución 6b

Solución del ejercicio 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="antologia">
    <html>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="autor">
    <br/>
    <h4>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  <xsl:template match="nombre">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="apellido">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="titulo">
    <h2>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="estrofa">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="verso">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="poema">
    <xsl:variable name="año">
      <xsl:value-of select="starts-with(@añodepublicacion,'-')"/>
    </xsl:variable>
    <div>
      <xsl:if test="$año='true'">
        <xsl:attribute name="style">background-color:red</xsl:attribute>
      </xsl:if>
      <br/>
      <xsl:apply-templates/>
    </div>
  </xsl:template>
</xsl:stylesheet>
Solución del ejercicio 7 en la vista del navegador (Fragmento):

Vista del navegador: solución 7

Solución del ejercicio 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="solucion8b.xslt"/>  
  <xsl:output encoding="iso-8859-1" version="1.0"/>
  <xsl:template match="antologia">
    <html>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Solución del ejercicio 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="nombre">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>
  <xsl:template match="apellido">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="titulo">
    <h2>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  <xsl:template match="estrofa">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="verso">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xsl:template match="poema">
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>

© Derechos de autor 2004, dpunkt.verlag GmbH
El usuario podrá imprimir la versión online. La copia será exclusivamente para uso personal. Por lo demás el presente capítulo del libro "XSL-FO in der Praxis" está sometido a los mismos términos y condiciones que la versión impresa. La presente obra está protegida en su totalidad por la ley de propiedad intelectual. Reservados todos los derechos, incluyendo los derechos de reproducción, traducción, microfilmación, así como el almacenamiento y procesamiento en sistemas electrónicos.

dpunkt.verlag GmbH, Ringstraße 19B, 69115 Heidelberg, teléfono + 49 (0) 6221-14830, fax +49 (0) 6221-148399, hallo(at)dpunkt.de