Exemple : Un programme XSLT pour générer du HTML

Fichier XML source

<?xml version="1.0" encoding="iso-8859-1"?>
<document titre="XSLT">
    
    <!--Première division-->    
    <div titre="XSLT : Un besoin">
        <paragraphe>XML est un format de <important>représentation</important> de l'information.</paragraphe>
        <paragraphe>XML n'est pas un format de présentation.</paragraphe>
    </div>
    
    <!--Seconde division-->    
    <div titre="XSLT : Un langage">
        <paragraphe>XSLT est un langage de <important>manipulation</important> de documents XML.</paragraphe>
        <paragraphe>XSLT est utilisé pour exporter une source XML sous un autre format, par exemple HTML.</paragraphe>
    </div>
</document>

Fichier HTML cible souhaité

<html>
    <head>
        <title>XSLT</title>
        <meta content="text/html" charset="iso-8859-1"/>
    </head>
    <body>
        <h1>XSLT : Un besoin</h1>
        <p>XML est un format de <B>représentation</B> de l'information.</p>
        <p>XML n'est pas un format de présentation.</p>
        <h1>XSLT : Un langage</h1>
        <p>XSLT est un langage de <B>manipulation</B> de documents XML.</p>
        <p>XSLT est utilisé pour exporter une source XML sous un autre format, par exemple HTML</p>
    </body>
</html>

Programme XSLT permettant la transformation

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" indent="yes" encoding="iso-8859-1"/>
    <!--1ère règle-->
    <xsl:template match="document">
        <html>
            <head>
                <title><xsl:value-of select="@titre"/></title>
                <meta content="text/html" charset="iso-8859-1"/>
            </head>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>
    <!--2nde règle-->
    <xsl:template match="div">
        <h1><xsl:value-of select="@titre"/></h1>
        <xsl:apply-templates/>
    </xsl:template>
    <!--3ème règle-->
    <xsl:template match="paragraphe">
        <p><xsl:apply-templates/></p>
    </xsl:template>
    <!--4ème règle-->
    <xsl:template match="important">
        <b><xsl:value-of select="."/></b>
    </xsl:template>
</xsl:stylesheet>
AccueilXPath et XSL-XSLT > Exemple XSLT< PrécédentSuivant >