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-->
<HEAD
<TITLE>XSLT</TITLE>
<META content="text/html" charset="iso-8859-1"/>
</HEAD>
<!--Body-->
<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>
AccueilProgrammation XSL-XSLT > Exemple XSLT< PrécédentSuivant >