Tecnologías XML / XSLT / Recetario XSLT / Transformar XML en HTML / Generar marcos
(Extracto de «XSLT Kochbuch» de Sal Mangano)
Problema
Desea generar HTML estructurado mediante marcos.
Solución
Como en la receta Crear documentos enlazados, utilice modos para recorrer varias veces el XML. Cree primero un frameset con dos marcos. El pequeño de la izquierda contiene nombres de vendedores como enlaces que activan contenido en el principal. Este muestra las cifras del vendedor seleccionado. Al abrir la página aparece un marco principal predeterminado:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:param name="URL"/>
<xsl:template match="/">
<xsl:apply-templates select="*" mode="frameset"/>
<xsl:apply-templates select="*" mode="salespeople_frame"/>
<xsl:apply-templates select="*" mode="sales_frames"/>
</xsl:template>
<!-- = = = = = = = = = = = = = = = = = = = = = = = = = -->
<!-- Erzeuge Frameset-Container (mode ="frameset") -->
<!-- = = = = = = = = = = = = = = = = = = = = = = = = = -->
<xsl:template match="salesBySalesperson" mode="frameset">
<!-- Nicht standardkonformes Saxon-xsl:document! -->
<xsl:document href="index.html">
<html>
<head>
<title>Salesperson Frameset</title>
</head>
<frameset rows="100%" cols="25%, 75%" border="0">
<frame name="salespeople" src="salespeople_frame.html" noresize=""/>
<frame name="mainFrame" src="default_sales.html" noresize=""/>
</frameset>
<body bgcolor="#FFFFFF" text="#000000"></body>
</html>
</xsl:document>
</xsl:template>
<!-- = = = = = = = = = = = = = = = = = = = = = = = = = -->
<!-- Erzeuge salespeople_frame.html (mode = "salespeople_frame") -->
<!-- = = = = = = = = = = = = = = = = = = = = = = = = = -->
<xsl:template match="salesBySalesperson" mode="salespeople_frame">
<!-- Nicht standardkonformes Saxon-xsl:document! -->
<xsl:document href="salespeople_frame.html">
<html>
<head>
<title>Salespeople</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<table>
<tbody>
<xsl:apply-templates mode="index"/>
</tbody>
</table>
</body>
</html>
</xsl:document>
</xsl:template>
<xsl:template match="salesperson" mode="index">
<tr>
<td>
<a href="{concat(@name,'.html')}" target="mainFrame">
<xsl:value-of select="@name"/>
</a>
</td>
</tr>
</xsl:template>
<!-- = = = = = = = = = = = = = = = = = = = = = = = = = -->
<!-- Erzeuge @name.html (mode = "content") -->
<!-- = = = = = = = = = = = = = = = = = = = = = = = = = -->
<xsl:template match="salesperson" mode="sales_frames">
<xsl:document href="default_sales.html">
<html>
<head>
<title>Default</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<h1><center>Sales By Salesperson</center></h1>
<br/>
Click on a salesperson on the left to load his or her sales figures.
</body>
</html>
</xsl:document>
<xsl:document href="{concat(@name,'.html')}">
<html>
<head>
<title><xsl:value-of select="@name"/></title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<h1><center>Sales By Salesperson</center></h1>
<h2><xsl:value-of select="@name"/></h2>
<table border="1" cellpadding="3">
<tbody>
<tr>
<th>SKU</th>
<th>Sales (in US $)</th>
</tr>
<xsl:apply-templates mode="content"/>
</tbody>
</table>
</body>
</html>
</xsl:document>
</xsl:template>
<xsl:template match="product" mode="content">
<tr>
<td><xsl:value-of select="@sku"/></td>
<td align="right"><xsl:value-of select="@totalSales"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>Discusión
Los marcos dividen una página en secciones lógicas, aunque los bordes visibles o redimensionables parecen anticuados. Esta hoja no sirve para transformación cliente: genera archivos HTML separados para el frameset, la lista izquierda y cada vendedor.
Esta solución genera dinámicamente un número abierto de páginas desde un XML. XSLT cliente con marcos puede ser más sencillo: cree un frameset cuyos marcos contengan documentos XML distintos con sus propias transformaciones:
<html>
<head>
<title>Frameset</title>
</head>
<frameset rows="100%" cols="25%, 75%" border="0">
<frame name="leftFrame" src="left.xml" noresize="">
<frame name="mainFrame" src="main.xml" noresize="">
</frameset>
<body bgcolor="#FFFFFF" text="#000000"></body>
</html>Se necesitan documentos XML distintos; el mecanismo es xml-stylesheet: una instrucción de procesamiento por archivo. El contenido puede compartir una hoja de estilo si se utiliza un enlace xinclude procesado mediante una plantilla XSLT.
left.xml tiene este aspecto:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="application/xml" href="left.xsl"?>
<xi:include href="salesBySalesperson.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>main.xml es similar:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="application/xml" href="main.xsl"?>
<xi:include href="salesBySalesperson.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>left.xsl y main.xsl contienen una plantilla que procesa el elemento xinclude mediante la función document:
<!-- left.xsl -->
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="xi:include" xmlns:xi="http://www.w3.org/2001/XInclude">
<xsl:for-each select="document(@href,.)">
<xsl:apply-templates/>
</xsl:for-each>
</xsl:template>
<xsl:template match="salesBySalesperson">
<html>
<head>
<title>Salespeople</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<table>
<tbody>
<xsl:apply-templates/>
</tbody>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="salesperson">
<tr>
<td>
<a href="{concat('main.xml#',@name)}" target="mainFrame">
<xsl:value-of select="@name"/></a>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
<!-- main.xsl -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="xi:include" xmlns:xi="http://www.w3.org/2001/XInclude">
<xsl:for-each select="document(@href)">
<xsl:apply-templates/>
</xsl:for-each>
</xsl:template>
<xsl:template match="salesBySalesperson">
<html>
<head>
<title>Sales By Salesperson</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="salesperson">
<h1><a name="{@name}"><center>Sales By Salesperson</center></a></h1>
<h2><xsl:value-of select="@name"/></h2>
<table border="1" cellpadding="3">
<tbody>
<tr>
<th>SKU</th>
<th>Sales (in US $)</th>
</tr>
<xsl:apply-templates />
</tbody>
</table>
<div style="padding-top:1000"/>
</xsl:template>
<xsl:template match="product">
<tr>
<td><xsl:value-of select="@sku"/></td>
<td align="right"><xsl:value-of select="@totalSales"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>En main.xsl, la hoja principal, genere anclas con nombre para cada vendedor, separadas por abundante espacio mediante <div style="padding-top: 1000"/>. En left.xsl, genere enlaces a esas anclas. Esto simula aproximadamente el ejemplo de varias páginas.
Véase también
El libro de Jeni Tennison XSLT and XPath on the Edge (M&T, 2001) estudia detalladamente XSLT cliente con marcos y muestra cómo JavaScript permite resultados más elaborados.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
Puede imprimir la versión en línea para uso privado.
Para cualquier otro uso, este capítulo de «XSLT Kochbuch» está sujeto a las mismas condiciones que la edición encuadernada. La obra y todas sus partes están protegidas por derechos de autor. Todos los derechos reservados, incluidos reproducción, traducción, microfilmación, almacenamiento y tratamiento en sistemas electrónicos.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de