TechWeb
Accueil Codage HTML CSS DESIGN XML LaboCSS

XSL générique

On peut définir une feuille de style générique qui s'applique à toute hiérarchie XML. Il suffit de définir une règle générique pour tout élément de la hiérarchie et une règle générique pour tout attribut.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="html"/>

<xsl:template match="/">

<html>

<head>

<title>

transformation

</title>

<style type="text/css">

p,blockquote {margin-top:0px;margin-bottom:0px;}

</style>

</head>

<body>

<xsl:apply-templates select="*"/>

</body>

</html>

</xsl:template>

 

<xsl:template match="*[count(*)>0]">

<blockquote>

<strong>

<xsl:value-of select="position()"/>

<xsl:text>

-

</xsl:text>

<xsl:value-of select="name()"/>

</strong>

<xsl:text>

(

</xsl:text>

<xsl:apply-templates select="@*"/>

<xsl:text>

)

</xsl:text>

<span style="color:red;">

<xsl:text>

</xsl:text>

<xsl:value-of select="count(*)"/>

<xsl:text>

enfants

</xsl:text>

</span>

<p> </p>

<xsl:text>

{

</xsl:text>

<xsl:apply-templates select="*"/>

<xsl:text>

}

</xsl:text>

</blockquote>

</xsl:template>

 

<xsl:template match="*[count(*)=0]">

<blockquote>

<strong>

<xsl:value-of select="position()"/>

<xsl:text>

-

</xsl:text>

<xsl:value-of select="name()"/>

</strong>

<xsl:text>

(

</xsl:text>

<xsl:apply-templates select="@*"/>

<xsl:text>

) :

</xsl:text>

<span style="color:blue;">

<xsl:value-of select="."/>

</span>

</blockquote>

</xsl:template>


 

<xsl:template match="@*">

<span style="color:green;">

<xsl:value-of select="name()"/>

<xsl:text>

:

</xsl:text>

<xsl:value-of select="."/>

<xsl:text>

</xsl:text>

</span>

</xsl:template>

</xsl:stylesheet>

 

Voir l'application sur le fichier ecole.xml

 

Valid XHTML 1.0 Strict Valid CSS!

Copyright Gabriel Braun 2007