Piccolo tutorial su eXtensible Stylesheet Language Transformation (XSLT)
XSLT è una tecnologia sviluppata dal W3C che permette, a partire da dati in formato XML, di generare documenti nei più disparati formati. L'esempio che vedremo in questo breve tutorial genera a partire da un file XML una pagina XHTML 1.0 Strict.
Ammettiamo di avere un file XML che contiene i nostri appunti. Il contenuto del file XML รจ il seguente:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="transformation.xsl"?>
<notes>
<note>
<headingLevel1>Titolo livello 1 olé</headingLevel1>
<paragraph>
Questa è la <strong>prima</strong> nota
<emphatized>scritta</emphatized> con
<link url="http://www.w3.org/XML/" urllang="en-EN">XML</link>.
</paragraph>
<list type="ordered">
<list-item>
elemento 1 di una lista <strong>ordinata</strong>
</list-item>
<list-item>elemento 2 <emphatized>corsivo</emphatized></list-item>
<list-item>
sito del <link url="http://www.w3.org/" urllang="en-EN">W3C</link>
</list-item>
<list-item>
<headingLevel3>Titolo livello 3 olé</headingLevel3>
<list type="unordered">
<list-item>
elemento 1 di una lista <strong>non ordinata</strong>
</list-item>
<list-item>
elemento 2 <emphatized>corsivo</emphatized>
</list-item>
</list>
</list-item>
</list>
</note>
</notes>
Ora ciò che dobbiamo fare è creare una trasformazione XSLT che generi una pagina XHTML gestendo correttamente le lettere accentate e tutti gli elementi: titoli, paragrafi, liste e via dicendo. La trasformazione è la seguente:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:text disable-output-escaping="yes">
<![CDATA[
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
]]>
</xsl:text>
<xsl:text disable-output-escaping="yes">
<![CDATA[
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="it" lang="it">
]]>
</xsl:text>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Appunti</title>
</head>
<body>
<xsl:apply-templates/>
</body>
<xsl:text disable-output-escaping="yes">
<![CDATA[
</html>
]]>
</xsl:text>
</xsl:template>
<!-- Every text must be escaped-->
<xsl:template match="text()">
<xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:template>
<!-- Heading levels -->
<xsl:template match="headingLevel1">
<h1><xsl:value-of select="." disable-output-escaping="yes"/></h1>
</xsl:template>
<xsl:template match="headingLevel2">
<h2><xsl:value-of select="." disable-output-escaping="yes"/></h2>
</xsl:template>
<xsl:template match="headingLevel3">
<h3><xsl:value-of select="." disable-output-escaping="yes"/></h3>
</xsl:template>
<xsl:template match="headingLevel4">
<h4><xsl:value-of select="." disable-output-escaping="yes"/></h4>
</xsl:template>
<xsl:template match="headingLevel5">
<h5><xsl:value-of select="." disable-output-escaping="yes"/></h5>
</xsl:template>
<xsl:template match="headingLevel6">
<h6><xsl:value-of select="." disable-output-escaping="yes"/></h6>
</xsl:template>
<!-- paragraphs -->
<xsl:template match="paragraph">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>
<!-- Lists -->
<xsl:template match="//list[@type='ordered']">
<ol>
<xsl:apply-templates/>
</ol>
</xsl:template>
<xsl:template match="//list[@type='unordered']">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="list/list-item">
<li>
<xsl:apply-templates/>
</li>
</xsl:template>
<!-- Commons -->
<xsl:template match="//strong">
<strong><xsl:value-of select="." disable-output-escaping="yes"/></strong>
</xsl:template>
<xsl:template match="//emphatized">
<em><xsl:value-of select="." disable-output-escaping="yes"/></em>
</xsl:template>
<xsl:template match="//link">
<a>
<xsl:attribute name="href"><xsl:value-of select='@url'/></xsl:attribute>
<xsl:attribute name="hreflang"><xsl:value-of select='@urllang'/></xsl:attribute>
<xsl:value-of select="."/>
</a>
</xsl:template>
</xsl:stylesheet>
A questo punto basterà aprire il file XML con un browser che supporta lato client le trasformazioni XSLT (in questo caso, strano a dirsi, tutti i browser tranne Firefox) per vedere il risultato. Se invece volete generare un file XHTML sul vostro filesystem allora potete usare Xalan o strumenti simili.
Alcune note
Il template
<xsl:template match="text()">
<xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:template>
serve a trasformare il testo correttamente. Se mancasse questo template le entità all'interno dei tag con figli non sarebbero trasformate correttamente.
Il template
<xsl:template match="//link">
<a>
<xsl:attribute name="href"><xsl:value-of select='@url'/></xsl:attribute>
<xsl:attribute name="hreflang"><xsl:value-of select='@urllang'/></xsl:attribute>
<xsl:value-of select="."/>
</a>
</xsl:template>
per la gestione dei link fa vedere come vanno gestiti gli attributi.
Potete anche scaricare l'esempio.
Scritto da Elia Contini il 23 Maggio 2009 alle 18:54
Egidio il 24 Maggio 2009 alle 13:08 ha scritto:
Germano il 24 Maggio 2009 alle 16:26 ha scritto:
<xsl:template match="text()">
<xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:template>
potresti cortesemente spiegarmelo ?
saluti,
germano
Elia il 24 Maggio 2009 alle 16:27 ha scritto:
Ti serve un trapianto di cervello :-D

