Diferencia entre revisiones de «Escenario viños con XSLT»
Ir a la navegación
Ir a la búsqueda
(Página creada con « = Enunciado = <syntaxhighlight lang=XML> <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="file:06.xslt"?> <viños> <viño DO="Rioja"> <…») |
|||
| (No se muestran 6 ediciones intermedias del mismo usuario) | |||
| Línea 2: | Línea 2: | ||
= Enunciado = | = Enunciado = | ||
| + | <div class="toccolours mw-collapsible" style="width:700px; overflow:auto;" > | ||
<syntaxhighlight lang=XML> | <syntaxhighlight lang=XML> | ||
<?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||
| Línea 73: | Línea 74: | ||
</viños> | </viños> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| + | </div> | ||
= Cuestións = | = Cuestións = | ||
| Línea 79: | Línea 81: | ||
Solo muestra el primer valor... hay que iterar | Solo muestra el primer valor... hay que iterar | ||
| + | |||
| + | <div class="toccolours mw-collapsible mw-collapsed" style="width:700px; overflow:auto;"> | ||
| + | <syntaxhighlight lang=XSL style="border: 3px dashed blue;> | ||
| + | <?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> | ||
| + | </syntaxhighlight> | ||
| + | </div> | ||
Referencias: | Referencias: | ||
| + | <div style="column-count:2;-moz-column-count:2;-webkit-column-count:2"> | ||
* [https://www.w3schools.com/xml/xsl_templates.asp XSL Templates] | * [https://www.w3schools.com/xml/xsl_templates.asp XSL Templates] | ||
* [https://www.w3schools.com/xml/xsl_value_of.asp XSL value of] | * [https://www.w3schools.com/xml/xsl_value_of.asp XSL value of] | ||
| + | </div> | ||
== Exemplo de for-each == | == Exemplo de for-each == | ||
Iteramos... xa temos os valores. Poderíamos ordenar?== | Iteramos... xa temos os valores. Poderíamos ordenar?== | ||
| + | |||
| + | <div class="toccolours mw-collapsible mw-collapsed" style="width:700px; overflow:auto;"><syntaxhighlight lang=XSL style="border: 3px dashed blue;> | ||
| + | <?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> | ||
| + | </syntaxhighlight> | ||
| + | </div> | ||
Referencias: | Referencias: | ||
| + | <div style="column-count:2;-moz-column-count:2;-webkit-column-count:2"> | ||
* https://www.w3schools.com/xml/xsl_for_each.asp | * https://www.w3schools.com/xml/xsl_for_each.asp | ||
* https://nwalsh.com/docs/tutorials/xsl/xsl/foil31.html | * https://nwalsh.com/docs/tutorials/xsl/xsl/foil31.html | ||
| + | </div> | ||
== Exemplo de sort == | == Exemplo de sort == | ||
Ordenamos... poderíamos escoller so algúns valores? | Ordenamos... poderíamos escoller so algúns valores? | ||
| + | |||
| + | <div class="toccolours mw-collapsible mw-collapsed" style="width:700px; overflow:auto;"><syntaxhighlight lang=XSL style="border: 3px dashed blue;> | ||
| + | <?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> | ||
| + | </syntaxhighlight> | ||
| + | </div> | ||
'''CURIOSIDADES''' | '''CURIOSIDADES''' | ||
| Línea 102: | Línea 190: | ||
Referencias: | Referencias: | ||
| + | <div style="column-count:3;-moz-column-count:3;-webkit-column-count:3"> | ||
* https://www.w3schools.com/xml/ref_xsl_el_sort.asp | * https://www.w3schools.com/xml/ref_xsl_el_sort.asp | ||
* https://stackoverflow.com/questions/4684664/sorting-with-xslt | * https://stackoverflow.com/questions/4684664/sorting-with-xslt | ||
* https://nwalsh.com/docs/tutorials/xsl/xsl/foil39.html | * https://nwalsh.com/docs/tutorials/xsl/xsl/foil39.html | ||
| + | </div> | ||
== xsl-if == | == xsl-if == | ||
Para mostrar so os que cumpren unha condición. Pero se queremos o equivalente a unha switch/case en moitas linguaxes? | Para mostrar so os que cumpren unha condición. Pero se queremos o equivalente a unha switch/case en moitas linguaxes? | ||
| + | |||
| + | <div class="toccolours mw-collapsible mw-collapsed" style="width:700px; overflow:auto;"><syntaxhighlight lang=XSL style="border: 3px dashed blue;> | ||
| + | <?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> | ||
| + | </syntaxhighlight> | ||
| + | </div> | ||
Referencias | Referencias | ||
| − | * https://www.w3schools.com/xml/xsl_if.asp | + | <div style="column-count:2;-moz-column-count:2;-webkit-column-count:2"> |
| + | * [https://www.w3schools.com/xml/xsl_if.asp xsl-if] | ||
| + | </div> | ||
== xsl-choose == | == xsl-choose == | ||
| Línea 117: | Línea 238: | ||
Para múltiples test condionais! | Para múltiples test condionais! | ||
| − | No exemplo amosarase para darlle formato (cor de fondo a certas filas da táboa) | + | No exemplo amosarase para darlle formato (cor de fondo a certas filas da táboa). |
| + | |||
| + | <div class="toccolours mw-collapsible mw-collapsed" style="width:700px; overflow:auto;"><syntaxhighlight lang=XSL style="border: 3px dashed blue;> | ||
| + | <?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 > 30"> | ||
| + | <td bgcolor="#FF0000"> | ||
| + | <xsl:value-of select="prezo"/> | ||
| + | </td> | ||
| + | </xsl:when> | ||
| + | <xsl:when test="prezo < 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> | ||
| + | </syntaxhighlight> | ||
| + | </div> | ||
Referencia: | Referencia: | ||
| − | * https://www.w3schools.com/xml/xsl_elementref.asp | + | <div style="column-count:2;-moz-column-count:2;-webkit-column-count:2"> |
| + | * [https://www.w3schools.com/xml/xsl_elementref.asp XSLT Elements Reference] | ||
| + | </div> | ||
== apply-templates == | == apply-templates == | ||
| Línea 126: | Línea 293: | ||
Selecciona un conxunto de nodos do documento de entrada e instrúe o procesador para aplicar as plantillas indicadas | Selecciona un conxunto de nodos do documento de entrada e instrúe o procesador para aplicar as plantillas indicadas | ||
| − | + | <div class="toccolours mw-collapsible mw-collapsed" style="width:700px; overflow:auto;"><syntaxhighlight lang=XSL style="border: 3px dashed blue;> | |
| − | + | <?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> | ||
| + | </syntaxhighlight> | ||
| + | </div> | ||
| + | |||
| + | Referencias | ||
| + | <div style="column-count:2;-moz-column-count:2;-webkit-column-count:2"> | ||
| + | * [https://www.w3schools.com/xml/xsl_apply_templates.asp xsl-apply-templates] | ||
| + | * [https://www.w3schools.com/xml/ref_xsl_el_apply-templates.asp xsl-apply-templates reference] | ||
| + | </div> | ||
| − | = | + | = Referencias = |
Revisión actual del 13:56 28 feb 2023
Sumario
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 > 30">
<td bgcolor="#FF0000">
<xsl:value-of select="prezo"/>
</td>
</xsl:when>
<xsl:when test="prezo < 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