Transformaciones XSLT, XPath y XQuery en XML de Bailes

Classified in Arts and Humanities

Written on in English with a size of 4.04 KB

Transformación XSLT a Formato HTML

Plantilla XSLT para representar los datos de la academia de baile en una tabla HTML:

<xsl:template match="/">
  <html>
    <head>
      <title>Bailes</title>
    </head>
    <body>
      <h2 style="text-align:center">
        LISTADO DE BAILES ACADEMIA <xsl:value-of select="academia/nombre"/>
        CURSO <xsl:value-of select="academia/bailes/@year"/>
      </h2>
      <table border="1" style="margin:auto;">
        <tr>
          <th>Identificador</th>
          <th>Nombre</th>
          <th>Precio</th>
          <th>Plazas</th>
          <th>Fechas</th>
          <th>Profesorado</th>
          <th>Sala</th>
        </tr>
        <xsl:apply-templates select="academia/bailes/baile"/>
      </table>
    </body>
  </html>
</xsl:template>

<xsl:template match="baile">
  <tr>
    <td><xsl:value-of select="@id"/></td>
    <td><xsl:value-of select="nombre"/></td>
    <td>
      <ul>
        <li><strong>Cuota:</strong> <xsl:value-of select="precio/@cuota"/></li>
        <li><strong>Moneda:</strong> <xsl:value-of select="precio/@moneda"/></li>
        <li><strong>Importe:</strong> <xsl:value-of select="precio/importe"/></li>
      </ul>
    </td>
    <td><xsl:value-of select="plazas"/></td>
    <td><xsl:value-of select="fechas/@comienzo"/> - <xsl:value-of select="fechas/@fin"/></td>
    <td>
      <ul>
        <xsl:for-each select="profesor">
          <li><xsl:value-of select="."/></li>
        </xsl:for-each>
      </ul>
    </td>
    <td><xsl:value-of select="sala"/></td>
  </tr>
</xsl:template>

Transformación XSLT de Estructura XML

Plantilla para reestructurar y filtrar la información del documento original XML:

<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<xsl:template match="academia">
  <academia>
    <xsl:copy-of select="nombre"/>
    <xsl:apply-templates select="bailes/baile"/>
  </academia>
</xsl:template>

<xsl:template match="baile">
  <baile>
    <xsl:copy-of select="nombre"/>
    <xsl:copy-of select="plazas"/>
    <xsl:copy-of select="fechas"/>
  </baile>
</xsl:template>

Consultas XPath

Selección y filtrado de nodos en el documento XML:

  • 1.1: //bailes/baile[sala=1]/@id
  • 1.0: //bailes/baile[profesor="Laura Garcés"]
  • 1.2: //bailes/baile[position()=2]/profesor/text()
  • 1.3: //bailes/baile[count(profesor) > 1 and plazas < 40]/nombre/text()
  • 1.4: //bailes/baile[last()]/profesor[last()]/preceding-sibling::*

Consultas XQuery

Bailes con Cuota Mensual en Euros

<bailes-cuota-mensual-euro>
{
  for $b in doc("bailes.xml")/academia/bailes/baile
  where $b/precio/@cuota="mensual" and $b/precio/@moneda="euro"
  return
    <baile>
      { $b/nombre }
      <precio>
        { string($b/precio/importe) }
      </precio>
      { $b/fechas }
    </baile>
}
</bailes-cuota-mensual-euro>

Agrupación de Profesores y sus Bailes

<profesores>
{
  for $p in distinct-values(doc("bailes.xml")//profesor)
  order by $p
  return
    <profesor nombre="{data($p)}">
      {
        for $b in doc("bailes.xml")//baile[profesor=$p]
        order by $b/nombre
        return
          <baile>{$b/@id, $b/nombre}</baile>
      }
    </profesor>
}
</profesores>

Related entries: