XSL : Knowing the position of a node in a collection XML

2013-05-17
Laurent Bientz

Imagine an XML stream composed of a collection of the same family (eg staff) and you want to know XSL in the position of a node in this position from a predicate where ([] in XPath).

Let the flow of XML of previous post slightly modified:

<?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>
            <staff>
                <id>5</id>
                <name>Jacques De Lamballerie</name>
            </staff>
            <staff>
                <id>6</id>
                <name>Katty Dida</name>
            </staff>
            <staff xp="iOs">
                <id>7</id>
                <name>Loic Couharde</name>
            </staff>
            <staff>
                <id>8</id>
                <name>Julien Quievreux</name>
            </staff>
            <staff>
                <id>9</id>
                <name>Jean-Baptiste Chateaux</name>
            </staff>
            <!-- ... -->
        </wandiz>
    </View>
</root>

Here, we want to know the position of Loic Couharde in wandiz/staff collection from its xp "iOS"

<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="whereIsSteveJobs">
        <xsl:variable name="iOsPosition" select="count(//View/wandiz/staff[@xp = 'iOs']/preceding-sibling::*)+1" />
        Position n°<xsl:value-of select="$iOsPosition" /> in /wandiz/staff
    </xsl:template>
</xsl:stylesheet>


 

Prodan - 2013-09-02 03:51:42
Your example worked like a charm. Works great for an upcoming project/requirement I have. What I need now is an example of how to limit the number of feeds. I am not a programmer, so very explicit examples apprecited. Thanks for your work.
Outlet - 2013-09-04 09:18:43
Pretty! This was a really wonderful article. Thank you for supplying this information.
toms - 2013-09-12 05:04:26
Hello there! I know this is kind of off topic but I was wondering if you knew where I could find a captcha plugin for my comment form? I'm using the same blog platform as yours and I'm having difficulty finding one? Thanks a lot!
XRumerTest - 2017-05-13 13:29:07
Hello. And Bye.

Comment

USER EXPERIENCE

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

Launch the experience