XSL : Function position() and attribute

2013-05-17
Laurent Bientz

A very simple example of use: you want to make a simple HTML table and ask a simple CSS class second line, the position () function and xsl: attribute are your friends!

Assume the following XML stream:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <View>
        <wandiz>
            <staff>
                <id>1</id>
                <name>Laurent Bientz</name>
            </staff>
            <staff>
                <id>2</id>
                <name>Florian Collot</name>
            </staff>
            <staff>
                <id>3</id>
                <name>Kevin Lancien</name>
            </staff>
            <staff>
                <id>4</id>
                <name>Nicolas Nowak</name>
            </staff>
            <!-- ... -->
        </wandiz>
    </View>
</root>

In XSL, the position() function returns the index (note starts with 1) of the node in the collection; xsl: attribute still allows him to dynamically create / override a previous HTML attribute.

So just test the mod to override the class every two lines:

<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">
            <tr>
                <th>ID</th>
                <th>NAME</th>
            </tr>
            <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>
                </tr>
            </xsl>
        </table>
    </xsl:template>
</xsl:stylesheet>

 

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
http://www.cheapbestnfljerseys.top/p-laid-750.html
http://www.cheapbestnfljerseys.top/p-laid-39877.html
http://www.cheapbestnfljerseys.top/p-laid-54211.html
http://www.cheapbestnfljerseys.top/p-laid-67764.html
http://www.cheapbestnfljerseys.top/p-laid-94469.html
http://www.cheapbestnfljerseys.top/p-laid-101595.html
http://www.cheapbestnfljerseys.top/p-laid-120854.html
http://www.cheapbestnfljerseys.top/p-laid-152615.html
http://www.cheapbestnfljerseys.top/p-laid-173424.html
http://www.cheapbestnfljerseys.top/p-laid-196640.html
http://www.cheapbestnfljerseys.top/p-laid-206737.html
http://www.cheapbestnfljerseys.top/p-laid-235890.html
http://www.cheapbestnfljerseys.top/p-laid-253097.html
http://www.cheapbestnfljerseys.top/p-laid-277637.html
http://www.cheapbestnfljerseys.top/p-laid-287422.html

Comment

USER EXPERIENCE

Wandi invites you to discover a new approach to navigating a website...
Are you ready?

Launch the experience