PROPÓSITO
Introducir el nivel XML de la plataforma tecnológica Web Semántica.
INTRODUCCIÓN
El siguiente ejemplo muestra un documento XML representando un stock de libros y cds distribuidos entre dos tiendas.
<?xml version="1.0" encoding="ISO-8859-1" ?> <stock>
<shop address="Reina Mercedes, 11">
<book>
<title lang="eng">Harry Potter and the Deathly Hallows</title>
<author>J. K. Rowling</author>
<year>2007</year>
<price>20.99</price>
</book>
<book>
<title lang="eng">XSLT 2.0 and XPath 2.0 Programmer's Reference</title>
<author>Michael Kay</author>
<year>2008</year>
<price>37.95</price>
</book>
<cd>
<title>London Calling</title>
<author>The Clash</author>
<year>1979</year>
<price>23.95</price>
</cd>
<book>
<title lang="spa">El alma está en el cerebro</title>
<author>Eduard Punset</author>
<year>2007</year>
<price>11.95</price>
</book>
<cd>
<title>In rainbows</title>
<author>Radiohead</author>
<year>2007</year>
<price>8.99</price>
</cd>
</shop>
<shop address="República Argentina, 15">
<book>
<year>1998</year>
<price>5.99</price>
</book> <book>
<title lang="spa">Caos y orden</title>
<author>Antonio Escohotado</author>
<year>1999</year>
<price>22.00</price>
</book> <cd>
<title>London Calling</title>
<author>The Clash</author>
<year>1979</year>
<price>23.95</price>
</cd>
</shop>
</stock>
Un documento XML consta de un prólogo y un conjunto de elementos. Ejemplo de prólogo:
<?xml version="1.0" encoding="ISO-8859-1" ?>
Ejemplo de elemento: <cd>
<title>London Calling</title>
<author>The Clash</author>
<year>1979</year>
<price>23.95</price> </cd>
Un elemento puede contener atributos. El siguiente ejemplo muestra el atributo lang para el elemento title:
DOCUMENTOS XML BIEN FORMADOS
Un documento XML se dice bien formado si éste es sintácticamente correcto.
Todo documento XML bien formado debe tener un elemento más externo denominado root. Cada elemento tiene una etiqueta de apertura y otra de cierre. Las etiquetas no se cruzan. Los atributos e un elemento tienen nombres únicos.
Los documentos bien formados pueden presentarse en forma de árbol.
LOCALIZANDO PARTES DEL DOCUMENTO XML: XPATH
El contenido del documento XML puede ser seleccionado mediante expresiones Xpath. Ejemplos de expresiones Xpath:
Todos los libros en inglés con precio menor a 20 euros: //book/title[@lang=”eng”]/../self::node()[price<20]
Todos los atributos: //@*
Todos los nodos con atributos: //node()[@*]
Todos los títulos de libros: //book/title
El primer libro de cada tienda: //book[1]
Último libro de 1999 de la última tienda: //shop[last()]/book[year=1999][last()]
Libros en inglés del año 2007
//book[year=2007]/title[@lang=”eng”]
Libros del año 2008 y cds del año 2007 //book[year=2008|]/cd[year=2007]
Libros que valgan entre 10 y 30 euros //book[price>10 and price<30]
Suma de los precios de libros anteriores a 2004: sum(//book[year<2004]/price)
PROCESANDO DOCUMENTOS XML: XSLT
Los documentos XML admiten transformaciones XSLT. La transformación XSLT permite generar un documento XML de salida desde un documento XML de entrada.
Ejemplo 1.
Documento de entrada:
<?xml version="1.0" ?> <precipitaciones>
<registro>
<lugar>Sevilla</lugar> <fecha>07/09/2007</fecha> <litros-m2>10</litros-m2> </registro>
<registro>
<lugar>Huelva</lugar> <fecha>07/09/2007</fecha> <litros-m2>20</litros-m2> </registro>
<registro>
<lugar>Cadiz</lugar> <fecha>17/10/2007</fecha> <litros-m2>15</litros-m2> </registro>
<registro>
<lugar>Almeria</lugar> <fecha>17/10/2007</fecha> <litros-m2>05</litros-m2> </registro>
<registro>
<lugar>Granada</lugar> <fecha>18/10/2007</fecha> <litros-m2>25</litros-m2> </registro>
<registro>
<lugar>Sevilla</lugar> <fecha>21/12/2007</fecha> <litros-m2>36</litros-m2> </registro>
<registro>
<lugar>Huelva</lugar> <fecha>21/12/2007</fecha> <litros-m2>41</litros-m2> </registro>
</precipitaciones>
Transformación: Generar un documento XML de muestre las precipitaciones de una determinada ciudad especificada mediante una variable en el propio fichero de
<?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template name="t1" match="/precipitaciones">
<xsl:variable name="lugar">Sevilla</xsl:variable>
<precipitaciones>
<lugar><xsl:value-of select="$lugar"/></lugar>
<xsl:apply-templates select="registro"></xsl:apply-templates>
</precipitaciones> </xsl:template>
<xsl:template name="t2" match="registro">
<xsl:variable name="lugar">Sevilla</xsl:variable> <xsl:if test="./lugar = $lugar">
<registro>
<fecha><xsl:value-of select="fecha"></xsl:value-of></fecha>
<litros-m2><xsl:value-of select="litros-m2" ></xsl:value-of></litros-m2> </registro> </xsl:if> </xsl:template> </xsl:stylesheet> Documento de salida:
<?xml version="1.0" encoding="UTF-8"?> <precipitaciones>
<lugar>Sevilla</lugar>
<registro> <fecha>07/09/2007</fecha> <litros-m2>10</litros-m2> </registro> <registro> <fecha>21/12/2007</fecha> <litros-m2>36</litros-m2> </registro> </precipitaciones>
Ejemplo 2.
Documento de entrada: el mismo documento del ejemplo 1.
Transformación: Generar un documento XML con los registros con precipitaciones mayores o iguales a 25.
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml"/>
<xsl:template match="/precipitaciones">
<precipitaciones>
<xsl:apply-templates select="registro"></xsl:apply-templates>
</precipitaciones> </xsl:template>
<xsl:template match="registro">
<xsl:variable name="cantidad">25</xsl:variable>
<xsl:if test="./litros-m2 >= $cantidad">
<registro>
<lugar><xsl:value-of select="lugar"/></lugar>
<fecha><xsl:value-of select="fecha"/></fecha>
<litros-m2><xsl:value-of select="litros-m2"/></litros-m2>
</registro> </xsl:if> </xsl:template> </xsl:stylesheet> Documento de salida:
<?xml version="1.0" encoding="UTF-8"?> <precipitaciones>
<registro>
<lugar>Granada</lugar>
<fecha>18/10/2007</fecha>
<litros-m2>25</litros-m2>
</registro>
<registro>
<lugar>Sevilla</lugar>
<fecha>21/12/2007</fecha>
<litros-m2>36</litros-m2>
</registro>
<registro>
<lugar>Huelva</lugar>
<fecha>21/12/2007</fecha>
<litros-m2>41</litros-m2>
</registro> </precipitaciones>
Ejemplo 3.
Documento de entrada: el mismo documento del ejemplo 1.
Transformación: Generar un documento XML con los primeros 6 registros ordenados por precipitación de mayor a menor.
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml"/>
<xsl:template match="/precipitaciones">
<precipitaciones>
<xsl:apply-templates select="registro">
<xsl:sort select="litros-m2" data-type="number"
order="descending"/>
</xsl:apply-templates>
</precipitaciones> </xsl:template>
<xsl:template match="registro">
<xsl:variable name="numRegistros">6</xsl:variable>
<xsl:if test="position() <= $numRegistros">
<registro>
<lugar><xsl:value-of select="lugar"/></lugar>
<fecha><xsl:value-of select="fecha"/></fecha>
<litros-m2><xsl:value-of select="litros-m2"/></litros-m2>
</registro> </xsl:if> </xsl:template> </xsl:stylesheet> Documento de salida:
<?xml version="1.0" encoding="UTF-8"?> <precipitaciones>
<registro>
<lugar>Huelva</lugar>
<fecha>21/12/2007</fecha>
<litros-m2>41</litros-m2>
</registro>
<registro>
<lugar>Sevilla</lugar>
<fecha>21/12/2007</fecha>
<litros-m2>36</litros-m2>
</registro>
<registro>
<lugar>Granada</lugar>
<fecha>18/10/2007</fecha>
<litros-m2>25</litros-m2>
</registro>
<registro>
<lugar>Huelva</lugar>
<fecha>07/09/2007</fecha>
<litros-m2>20</litros-m2>
</registro>
<registro>
<lugar>Cadiz</lugar>
<fecha>17/10/2007</fecha>
</registro>
<registro>
<lugar>Sevilla</lugar>
<fecha>07/09/2007</fecha>
<litros-m2>10</litros-m2>
</registro> </precipitaciones>
TRABAJO PROPUESTO
Considere el documento XML sobre el stock de libros y cds.
Escriba una expresión Xpath que seleccione todos los libros y cds con precio mayor a 14 euros. Escriba una transformación XSLT que genere un documento XML de salida conteniendo los libros y cds con precio mayor a 14 euros.
Compruebe sus respuestas en un entorno de desarrollo formado por Eclipse + OrangeVolt. Instale el entorno siguiendo los siguientes pasos:
(a) Instalar Eclipse IDE for Java EE Developers.
(b) Añadir el plugin OrangeVolt (http://eclipsexslt.sourceforge.net/update-site). (c) Seleccionar Saxon-B.8.9 como compilador de ficheros XSLT en Eclipse.