
| |
La plupart des navigateurs Internet n'affichent pas le code XML "brut de fonderie", il est généralement traité afin d'avoir un rendu plus estéthique, essayez d'afficher un XML dans Internet Explorer ou Firefox par exemple.
Ce traitement est fait à partir d'un XSLT, mais comme chaque navigateur a le sien, une réutilisation pure et simple pourrait poser des problèmes, ce XSLT permet de transformer tout document XML en une page HTML présentant le code XML sous une apparence plus lisible.
Le XSLT |
<? xml version="1.0" encoding="UTF-8"? >
< xsl : stylesheet version = " 1.0 " xmlns : xsl = " http://www.w3.org/1999/XSL/Transform " >
< xsl : output method = " html " version = " 1.0 " encoding = " UTF-8 " indent = " yes " / >
< xsl : variable name = " tab " select = " ' ' " / >
< xsl : template match = " / " >
< html >
< head >
< title > presentation xslt< / title >
< style type = " text/css " >
.PI
{
color:green;
}
.comment
{
color:gray;
}
.element
{
color:brown;
}
.attribut
{
color:red;
}
.text
{
color:black;
}
.signe
{
color:blue;
}
.marge
{
margin-left:15px;
}
< / style >
< / head >
< body >
< span class = " PI " > <? xml version="1.0" ? > < / span >
< xsl : apply-templates select = " node() " / >
< / body >
< / html >
< / xsl : template >
< xsl : template match = " *[not(node())] " >
< div class = " marge " >
< span class = " signe " > < < / span >
< span class = " element " >
< xsl : value-of select = " name() " / >
< / span >
< xsl : apply-templates select = " @* " / >
< span class = " signe " > />< / span >
< / div >
< / xsl : template >
< xsl : template match = " *[node()] " >
< div class = " marge " >
< span class = " signe " > < < / span >
< span class = " element " >
< xsl : value-of select = " name() " / >
< / span >
< xsl : apply-templates select = " @* " / >
< span class = " signe " > >< / span >
< xsl : apply-templates select = " node() " >
< / xsl : apply-templates >
< span class = " signe " > < / < / span >
< span class = " element " >
< xsl : value-of select = " name() " / >
< / span >
< span class = " signe " > >< / span >
< / div >
< / xsl : template >
< xsl : template match = " text() " >
< span class = " text " >
< xsl : value-of select = " . " / >
< / span >
< / xsl : template >
< xsl : template match = " comment() " >
< span class = " comment " > < / span >
< / xsl : template >
< xsl : template match = " @* " >
< span class = " attribut " >
< xsl : value-of select = " concat(' ',name()) " / >
< / span >
< span class = " signe " > ="< / span >
< span class = " text " >
< xsl : value-of select = " . " / >
< / span >
< span class = " signe " > "< / span >
< / xsl : template >
< xsl : template match = " processing-instruction() " >
< div class = " marge " >
< span class = " PI " > <? <xsl:value-of select="concat(name(),$tab,.)"/ > ?>< / span >
< / div >
< / xsl : template >
< / xsl : stylesheet >
|
|
| |
On a souvent besoin de filtrer certaines balises, ou certains attributs.
Parfois, ces filtres doivent s'appliquer sous condition (selon un paramètre passé au template par exemple), dans ce cas, la gestion est souvent ardue.
Voilà un exemple qui permettra de simplifier grandement ces cas problématiques.
Le XSLT |
<? xml version="1.0" encoding="UTF-8"? >
< xsl : stylesheet version = " 1.0 " xmlns : xsl = " http://www.w3.org/1999/XSL/Transform " >
< xsl : output method = " xml " version = " 1.0 " encoding = " UTF-8 " indent = " yes " / >
< xsl : key name = " Cle " match = " a " use = " concat(generate-id(.),b,c,d) " / >
< xsl : variable name = " A1 " select = " '1' " / >
< xsl : variable name = " A2 " / >
< xsl : variable name = " A3 " / >
< xsl : variable name = " B1 " >
< xsl : if test = " $A1 " >
< xsl : value-of select = " concat($A1,'*') " / >
< / xsl : if >
< / xsl : variable >
< xsl : variable name = " B2 " >
< xsl : if test = " $A2 " >
< xsl : value-of select = " concat($A2,'*') " / >
< / xsl : if >
< / xsl : variable >
< xsl : variable name = " B3 " >
< xsl : if test = " $A3 " >
< xsl : value-of select = " concat($A3,'*') " / >
< / xsl : if >
< / xsl : variable >
< xsl : template match = " / " >
< R >
< xsl : apply-templates select = " R/a[key('Cle', concat(generate-id(.),substring-before(concat($B1,string(./b),'*'),'*'),substring-before(concat($B2,string(./c),'*'),'*'),substring-before(concat($B3,string(./d),'*'),'*')) )] " > < / xsl : apply-templates >
< / R >
< / xsl : template >
< xsl : template match = " a " >
< test >
< xsl : value-of select = " @n " > < / xsl : value-of >
< / test >
< / xsl : template >
< / xsl : stylesheet >
|
|
| |
Avec une transformation XSLT on souhaite afficher les données par groupes de M dans un tableau à C colonnes.
M et C étant des paramètres donnés.
Exemple N=11, M=3, C=3 |
*************************
* fic1 * fic4 * fic7 *
* fic2 * fic5 * fic8 *
* fic3 * fic6 * fic9 *
*************************
* fic10 * * *
* fic11 * * *
* * * *
*************************
|
Le résultat est ici un tableau HTML est généré, mais on peut facilement le changer en autre chose.
De plus, une seconde solution existe, voir plus bas.
Le XML |
< fichiers >
< fichier nom = " fic1 " / >
< fichier nom = " fic2 " / >
< fichier nom = " fic3 " / >
< fichier nom = " fic4 " / >
< fichier nom = " fic5 " / >
< fichier nom = " fic6 " / >
< fichier nom = " fic7 " / >
< fichier nom = " fic8 " / >
< fichier nom = " fic9 " / >
< fichier nom = " fic10 " / >
< fichier nom = " fic11 " / >
< / fichiers >
|
Le XSLT |
<? xml version="1.0"? >
< xsl : stylesheet version = " 1.0 " xmlns : xsl = " http://www.w3.org/1999/XSL/Transform " >
< xsl : output method = " html " / >
< xsl : variable name = " M " select = " 3 " / >
< xsl : variable name = " C " select = " 3 " / >
< xsl : template match = " / " >
< html >
< body >
< table border = " 1 " >
< xsl : for-each select = " //fichier[position() mod ($M * $C) = 1] " >
< tr >
< xsl : for-each select = " .|following-sibling::fichier[position() mod $M = 0 and position() < $M * $C] " >
< td >
< xsl : for-each select = " .|following-sibling::fichier[position() < $M] " >
< xsl : value-of select = " @nom " / > < br / >
< / xsl : for-each >
< / td >
< / xsl : for-each >
< xsl : if test = " position() = last() " >
< xsl : call-template name = " cellules_vides " >
< xsl : with-param name = " nb " select = " $C - ceiling(count(.|following-sibling::fichier) div $M) " / >
< / xsl : call-template >
< / xsl : if >
< / tr >
< / xsl : for-each >
< / table >
< / body >
< / html >
< / xsl : template >
< xsl : template name = " cellules_vides " >
< xsl : param name = " nb " / >
< xsl : if test = " $nb >= 1 " >
< td / >
< xsl : call-template name = " cellules_vides " >
< xsl : with-param name = " nb " select = " $nb - 1 " / >
< / xsl : call-template >
< / xsl : if >
< / xsl : template >
< / xsl : stylesheet >
|
Le Résultat |
< html >
< body >
< table border = " 1 " >
< tr >
< td >
fic1
fic2
fic3
< / td >
< td >
fic4
fic5
fic6
< / td >
< td >
fic7
fic8
fic9
< / td >
< / tr >
< tr >
< td >
fic10
fic11
< / td >
< td > < / td >
< td > < / td >
< / tr >
< / table >
< / body >
< / html >
|
La seconde solution est plus concise, moyennant une bidouille afin de garder une feuille XSLT bien formée.
De plus elle ne gère pas les cellules vides et ne fonctionne pas dans le cas d'une transformation côté client :
Le XSLT |
<? xml version="1.0" encoding="UTF-8"? >
< xsl : stylesheet version = " 1.0 " xmlns : xsl = " http://www.w3.org/1999/XSL/Transform " >
< xsl : output method = " html " / >
< xsl : variable name = " M " select = " 3 " / >
< xsl : variable name = " C " select = " 3 " / >
< xsl : template match = " / " >
< html >
< body >
< table >
< xsl : apply-templates select = " //fichier " / >
< / table >
< / body >
< / html >
< / xsl : template >
< xsl : template match = " fichier " >
< xsl : if test = " position() mod ($M * $C)=1 " >
< xsl : text disable-output-escaping = " yes " > & lt ; tr& gt ; < / xsl : text >
< / xsl : if >
< xsl : if test = " position() mod $M=1 " >
< xsl : text disable-output-escaping = " yes " > & lt ; td& gt ; < / xsl : text >
< / xsl : if >
< xsl : value-of select = " concat(@nom,';') " / >
< xsl : if test = " position() mod $M=0 or position()=last() " >
< xsl : text disable-output-escaping = " yes " > & lt ; /td& gt ; < / xsl : text >
< / xsl : if >
< xsl : if test = " position() mod ($M * $C)=0 or position()=last() " >
< xsl : text disable-output-escaping = " yes " > & lt ; /tr& gt ; < / xsl : text >
< / xsl : if >
< / xsl : template >
< / xsl : stylesheet >
|
|
Consultez les autres pages sources
Les sources présentés sur cette page sont libres de droits,
et vous pouvez les utiliser à votre convenance. Par contre cette page de présentation de ces sources constitue une oeuvre intellectuelle protégée par les droits d'auteurs.
Copyright ©2006
Developpez LLC. Tous droits réservés Developpez LLC.
Aucune reproduction, même partielle, ne peut être faite de ce site et de
l'ensemble de son contenu : textes, documents et images sans l'autorisation
expresse de Developpez LLC. Sinon vous encourez selon la loi jusqu'à 3 ans
de prison et jusqu'à 300 000 E de dommages et intérêts.
Cette page est déposée à la SACD.
|