XSL : Fonction position() et attribute

Laurent Bientz

Un exemple très simple d'utilisation : vous souhaitez réaliser un simple tableau HTML et poser une simple class CSS une ligne sur deux, la fonction position() et xsl:attribute sont vos amis !

Admettons le flux XML suivant :

<?xml version="1.0" encoding="UTF-8"?>
                <name>Laurent Bientz</name>
                <name>Florian Collot</name>
                <name>Kevin Lancien</name>
                <name>Nicolas Nowak</name>
            <!-- ... -->

En XSL, la fonction position() renvoie l'indice (attention commence par 1) du noeud dans la collection ; xsl:attribute permet quand à lui de dynamiquement créer/surcharger un attribut HTML le précédant.

Il suffit donc de tester le modulo afin de surcharger la class du tr toutes les 2 lignes :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl" xmlns="http://www.w3.org/1999/xhtml">
    <xsl:template name="showWandiz">
        <table class="team">
            <xsl:for-each select="//View/wandiz/staff">
                <tr class="light">
                    <xsl:if test="position() mod 2 = 0"><xsl:attribute name="class">dark</xsl:attribute></xsl:if>
                    <td><xsl:value-of select="id" /></td>
                    <td><xsl:value-of select="name" /></td>


Nelsi - 2013-09-02 03:16:40
Thank you for your post about XSLT date convertions. It was very ueusfl. I was looking for a way to convert a dates without using explicit Xpath date convertion functions and you gave just that. Thank you again.
IgnacioDext - 2016-08-18 21:08:54
Lakers #24 Kobe Bryant Black/Grey Women's Fadeaway Fashion Embroidered NBA Jersey
authentic jersey wholesale
real nfl jerseys
NHL Jerseys Tampa Bay Lightning 91 Steven Stamkos Black
cheap nfl authentic jerseys wholesale
nfl jersey cheap
nfl cheap apparel
cheap team jerseys
Nike Eagles #10 DeSean Jackson White Mens NFL Elite Jersey
Majestic Athletic MLB San Francisco Giants Womens Orange Blended T-Shirt 018
Top MLB New York Yankees Snapback Hat NU27 Cheap Sale
Manchester United #5 Marcos Rojo Black Long Sleeves Soccer Club Jersey
MLB Jerseys Milwaukee Brewers 8 Ryan Braun grey
MLB Jerseys New York Mets 45 Martinez white blue stripe
PrzemekUnlip - 2017-05-24 21:13:41
Witam wszystkich :) Jako świeżak na forum postanowiłem się przywitać :)
MyronLox - 2017-11-06 12:21:53
Never know where to discover an essay writing service with on the finest top quality - Study reviews to seek out the leading good quality service with consumer-friendly.We present on the web Expert Writing Guide for Academic Writing Services at all levels - Do not get stuck together with your papers - Let us assist you.
DoyleBinna - 2017-11-20 08:31:50
пригожий веб сайт http://xdoza.com/prostitutki/
MatthewLow - 2018-03-04 05:52:28
Wonderful website, how do u find all this information?I have read a few posts on your website and I love your style. Thanks a million, keep up the great work.
IbuxaTheat - 2019-10-13 01:39:44
Hi! I could have sworn I’ve been to this blog before but after looking at
a few of the posts I realized it’s new to me. Regardless,
I’m definitely happy I came across it and I’ll be bookmarking it and checking back frequently!
RobertOliynuk - 2019-11-26 02:44:33
NXPatrick - 2019-12-27 17:01:40
Hello. And Bye.
FUBrad - 2020-01-30 14:28:30
Hello. And Bye.
MixMen - 2020-02-02 23:50:16
hi guys :). I am looking for help for me and my girl. i am from France
Darnellneoni - 2020-02-19 03:15:20
belleprots - 2020-03-01 17:22:30
Hi :). I am from Netherlands and i don't know how can i disable my signature? Regards :)
Kristinasmise - 2020-03-18 20:01:54
Quelqu'un à la maison? :)
EdwardDit - 2020-03-24 23:44:14
Hello. And Bye.
Billystook - 2021-01-10 22:05:22
Если у кого то есть проблемы с мужским здоровьем то заходите по ссылке
Сиалис Приобрести с доставкой. Загляните на наш сайт


FrankBom - 2021-06-27 13:40:06
My brother suggested I might like this blog. He was entirely right. This post actually made my day. You can not imagine just how much time I had spent for this information! Thanks!

Apporter votre contribution

Expérience utilisateur

Wandi vous propose de découvrir une nouvelle approche de la navigation sur un site web...
Êtes-vous prêt ?

Démarrer l'expérience