UP PREVIOUS NEXT   Technologies Internet et Education, © TECFA
  2. Introduction à XSLT

2. Introduction à XSLT

But de XSLT
Utilisation (mécanisme de base):
  1. Définir des règles qui disent comment transformer un "noeud" (element, tag) et ses sous-éléments
  2. Organiser l'application de ces règles
Compléménts à XSLT
Les feuilles de style XSLT
Documentation

2.1 Entêtes des fichiers XSL

A. Définition d'un fichier XSL

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
....
</xsl:stylesheet>

B. Association d'un fichier XSL à un fichier XML

L'association peut se faire dans le fichier XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet href="project.xsl" type="text/xsl"?>
    • On présuppose ici que soit le serveur (par exemple Cocoon) soit le client (par exemple IE5) comprenne cette instruction
    • Il existe d'autres méthodes d'association (server-side) comme un "GET Url" avec des paramètres ...

2.2 Principe de fonctionnement de XSL

<commentaire> xxxx </commentaire>

pourrait donner:

<DL>
     <DT>Commentaire  </DT>
     <DD> xxxx </DD>
 </DL>

2.3 Un simple exemple XSLT

Exemple 2-1: Sensibilisation XML + XSLT

Le fichier XML (sans association de la feuille de style)
<?xml version="1.0"?>
<page>
 <title>Hello Cocoon friend</title>
 <content>Here is some content :)  </content> 
 <comment>Written by DKS/Tecfa, adapted from S.M./the Cocoon samples </comment>
</page>
Le "fichier" XHTML résultant que l'on désire obtenir
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><title>Hello Cocoon friend</title></head><body bgcolor="#ffffff">
 <h1 align="center">Hello Cocoon friend</h1>
 <p align="center"> Here is some content :) </p> 
 <hr> Written by DKS/Tecfa, adapted from S.M./the Cocoon samples
  </body></html>
Le fichier XSL
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="page">
.....
   <html> <head> <title> <xsl:value-of select="title"/> 
</title> </head>
    <body bgcolor="#ffffff">
     <xsl:apply-templates/>
    </body>
   </html>
  </xsl:template>
 
  <xsl:template match="title">
   <h1 align="center"> <xsl:apply-templates/>
 </h1>
  </xsl:template>
 
  <xsl:template match="content">
   <p align="center"> <xsl:apply-templates/> </p>
  </xsl:template>
 
  <xsl:template match="comment">
   <hr /> <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>

UP PREVIOUS NEXT -- TIE