Escenario viños con XSLT

De MediaWiki
Ir a la navegación Ir a la búsqueda

Enunciado

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="file:06.xslt"?>
<viños>
  <viño DO="Rioja">
    <nome>Marqués de Murrieta</nome>
    <tipo>Reserva</tipo>
    <prezo>16</prezo>
  </viño>
  <viño DO="Ribera del Duero">
    <nome>Loes Collection</nome>
    <tipo>Reserva</tipo>
    <prezo>34</prezo>
  </viño>
  <viño DO="Ribera del Guadiana">
    <nome>Acilates 2013</nome>
    <tipo>Reserva</tipo>
    <prezo>15</prezo>
  </viño>
  <viño DO="Rioja">
    <nome>Faustino</nome>
    <tipo>Crianza</tipo>
    <prezo>5</prezo>
  </viño>
  <viño DO="Ribera del Duero">
    <nome>Rolland Galarreta</nome>
    <tipo>Crianza</tipo>
    <prezo>16</prezo>
  </viño>
  <viño DO="Ribera del Guadiana">
    <nome>Palacio Quemado Primicia</nome>
    <tipo>Crianza</tipo>
    <prezo>6</prezo>
  </viño>
  <viño DO="Rioja">
    <nome>Castillo Ygay 1986</nome>
    <tipo> Gran Reserva</tipo>
    <prezo>495</prezo>
  </viño>
  <viño DO="Ribera del Duero">
    <nome>Alonso del Yerro 2012</nome>
    <tipo>Crianza</tipo>
    <prezo>21</prezo>
  </viño>
  <viño DO="Ribera del Guadiana">
    <nome>Palacio Quemado Reserva 2011</nome>
    <tipo>Reserva</tipo>
    <prezo>9</prezo>
  </viño>
  <viño DO="Rioja">
    <nome>Barón de Ley 2011</nome>
    <tipo>Reserva</tipo>
    <prezo>9</prezo>
  </viño>
  <viño DO="Ribera del Duero">
    <nome>Corral del Obispo</nome>
    <tipo>Crianza</tipo>
    <prezo>8</prezo>
  </viño>
  <viño DO="Ribera del Guadiana">
    <nome>Carabal Macareno 2007 Magnum 1'5L</nome>
    <tipo>Crianza</tipo>
    <prezo>32</prezo>
  </viño>
  <viño DO="Ribera del Guadiana">
    <nome>Jaloco</nome>
    <tipo>Gran Reserva</tipo>
    <prezo>***</prezo>
  </viño>
</viños>

Cuestións

Exemplo de value-of e template

Solo muestra el primer valor... hay que iterar

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <html>
      <body>
        <h1>Os meus viños</h1>
        <table border="1">
          <tr bgcolor="#aabbcc">
            <th>Nome</th>
            <th>Prezo</th>
          </tr>
          <tr>
            <td>
              <xsl:value-of select="viños/viño/nome"/>
            </td>
            <td>
              <xsl:value-of select="viños/viño/prezo"/>
            </td>
          </tr>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Referencias:

Exemplo de for-each

Iteramos... xa temos os valores. Poderíamos ordenar?==

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <h1>Os meus viños</h1>
    <table border="1">
      <tr bgcolor="#aabbcc">
        <th>Nome</th>
        <th>Prezo</th>
      </tr>
      <xsl:for-each select="viños/viño">
      <tr>
        <td><xsl:value-of select="nome" /></td>
        <td><xsl:value-of select="prezo" /></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Referencias:

Exemplo de sort

Ordenamos... poderíamos escoller so algúns valores?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <h1>Os meus viños</h1>
    <table border="1">
      <tr bgcolor="#aabbcc">
        <th>Nome</th>
        <th>Prezo</th>
      </tr>
      <xsl:for-each select="viños/viño">
      <xsl:sort select="prezo" order="descending" data-type="number"/>
      <tr>
        <td><xsl:value-of select="nome" /></td>
        <td><xsl:value-of select="prezo" /></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>
CURIOSIDADES
Qué pasa se non poñemos o data type? 
Cal é a orde por defecto? 
Para qué serve case-order?

Referencias:

xsl-if

Para mostrar so os que cumpren unha condición. Pero se queremos o equivalente a unha switch/case en moitas linguaxes?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <h1>Os meus viños</h1>
    <table border="1">
      <tr bgcolor="#aabbcc">
        <th>Nome</th>
        <th>Prezo</th>
      </tr>
      <xsl:for-each select="viños/viño">
      <xsl:sort select="prezo" order="descending" data-type="number"/>
      <xsl:if test="prezo>10">
      <tr>
        <td><xsl:value-of select="nome" /></td>
        <td><xsl:value-of select="prezo" /></td>
      </tr>
      </xsl:if>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Referencias

xsl-choose

Para múltiples test condionais!

No exemplo amosarase para darlle formato (cor de fondo a certas filas da táboa).

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <html>
      <body>
        <h1>Os meus viños</h1>
        <table border="1">
          <tr bgcolor="#aabbcc">
            <th>Nome</th>
            <th>Prezo</th>
          </tr>
          <xsl:for-each select="viños/viño">
            <tr>
              <td>
                <xsl:value-of select="nome"/>
              </td>
              <xsl:choose>
                <xsl:when test="prezo &gt; 30">
                  <td bgcolor="#FF0000">
                    <xsl:value-of select="prezo"/>
                  </td>
                </xsl:when>
                <xsl:when test="prezo &lt; 10">
                  <td bgcolor="#008000">
                    <xsl:value-of select="prezo"/>
                  </td>
                </xsl:when>
                <xsl:otherwise>
                  <td bgcolor="#FFFF00">
                    <xsl:value-of select="prezo"/>
                  </td>
                </xsl:otherwise>
              </xsl:choose>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Referencia:

apply-templates

Selecciona un conxunto de nodos do documento de entrada e instrúe o procesador para aplicar as plantillas indicadas

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
  <html>
  <body>
  <h1>Os meus viños</h1>
  <xsl:apply-templates/>
  </body>
  </html>
</xsl:template>

<xsl:template match="viño">
  <p>
  <xsl:apply-templates select="nome"/>
  <xsl:apply-templates select="prezo"/>
  </p>
</xsl:template>

<xsl:template match="nome">
  Nome: <span style="color:#ff0000">
  <xsl:value-of select="."/></span>
  <br />
</xsl:template>

<xsl:template match="prezo">
  Prezo: <span style="color:#00ff00">
  <xsl:value-of select="."/></span>
  <br />
</xsl:template>

</xsl:stylesheet>

Referencias

Referencias