Example 7: XSLT Transformation
Convert an XML document into HTML by applying an XSLT stylesheet
🎯 What does this example cover?
XSLT (eXtensible Stylesheet Language Transformations) es un lenguaje que describe cómo transformar un documento XML en otro formato: HTML, texto plano o incluso otro XML. Este ejemplo aplica de verdad una hoja de estilo XSLT sobre el listado de libros usando la extensión XSLTProcessor de PHP, generando una tabla HTML. Puedes elegir el orden de los resultados, que se controla mediante un parámetro XSLT.
Configure the transformation
<?xml version="1.0" encoding="UTF-8"?>
<biblioteca>
<libro>
<titulo>El Quijote</titulo>
<autor>Miguel de Cervantes</autor>
<anio>1605</anio>
</libro>
<libro>
<titulo>Cien anios de soledad</titulo>
<autor>Gabriel Garcia Marquez</autor>
<anio>1967</anio>
</libro>
<libro>
<titulo>Orgullo y prejuicio</titulo>
<autor>Jane Austen</autor>
<anio>1813</anio>
</libro>
</biblioteca>
Resulting HTML (rendered)
| Title | Author | Year |
|---|---|---|
| El Quijote | Miguel de Cervantes | 1605 |
| Orgullo y prejuicio | Jane Austen | 1813 |
| Cien anios de soledad | Gabriel Garcia Marquez | 1967 |
📖 Code Explanation
Complete PHP Code
<?php
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xmlBiblioteca);
$xslDoc = new DOMDocument();
$xslDoc->loadXML($xslt);
$procesador = new XSLTProcessor();
$procesador->importStylesheet($xslDoc);
$procesador->setParameter('', 'orden', 'ascending');
echo $procesador->transformToXML($xmlDoc);
?>
📝 xsl:for-each and xsl:value-of
<xsl:value-of select="titulo"/>
</xsl:for-each>
What does it do? Iterates over each <libro> in the source XML and, for each one, extracts the value of titulo with xsl:value-of, generating an HTML table row.
📝 XSLT Parameters
$procesador->setParameter('', 'orden', 'descending');
What does it do? The <xsl:param> declares a variable that can be configured from outside the XSLT sheet. From PHP, setParameter() assigns it a value before transforming, letting you change the behavior (here, the order) without touching the XSLT.
💡 Concept: separating data, presentation and logic
The XML holds the data, the XSLT defines how to display it, and PHP simply orchestrates the transformation and passes parameters. This way you can change the presentation without touching the data.
🎯 Key Concepts Learned
XSLT
- Transforms XML into HTML, text or other XML
- Written in XML syntax
- Uses templates (
xsl:template)
xsl:sort and xsl:for-each
xsl:for-eachiterates over nodesxsl:sortsorts before iteratingdata-type="number"sorts numerically
XSLTProcessor in PHP
importStylesheet()loads the XSLTsetParameter()passes dynamic valuestransformToXML()runs the transformation
💪 Practical Exercise
Modify the transformation
- Switch between ascending and descending order and observe the resulting table
- Think about how you'd add one more column (for example, a row number) to the generated table
- How would you sort alphabetically by
autorinstead of byanio? (hint: change theselectexpression inxsl:sortand removedata-type="number")
Sort alphabetically by author
<xsl:sort select="autor" order="{$orden}"/>
<!-- Without data-type="number": it sorts as text, alphabetically -->