Martin Hinze
Martin Hinze

Reputation: 71

How to access values from previous and next element in sorted xsl:for-each loop

I am using a xsl:for-each loop to sort elements according to their @id-attribute. I need to get the @id-attributes of the previous and next element in the loop.

I've been trying around with the preceding-sibling:: and following-sibling axis, to no avail. I also tried

<xsl:variable name="current_pos" select="position()"/>
<xsl:value-of select="(//chapter)[($current_pos - 1)]/id>

but this returns the attribute values of the unsorted data.

Sample XML data:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <chapter id="t19"/>
    <chapter id="a23"/>
    <chapter id="c-0"/>
    <chapter id="d42"/>
    <chapter id="c-1"/>
</root>

Sample XSLT style sheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="3.0">

    <xsl:template match="/root">
        <xsl:for-each select="chapter">
            <xsl:sort select="@id"/>
            <xsl:variable name="current_id" select="@id"/>
            Chapter id: <xsl:value-of select="$current_id"/>
            Sorted position: <xsl:value-of select="position()"/>
            Sorted predecessor chapter id: ? <!-- no idea but most important -->
            Sorted follower chapter id: ? <!-- no idea but most important -->
            <xsl:text>&#xa;</xsl:text>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

The result I would need:

Chapter id: a23
Sorted position: 1
Sorted predecessor chapter id: none 
Sorted follower chapter id: c-0

Chapter id: c-0
Sorted position: 2
Sorted predecessor chapter id: a23 
Sorted follower chapter id: c-1     

Chapter id: c-1
Sorted position: 3
Sorted predecessor chapter id: c-0 
Sorted follower chapter id: d42

Chapter id: d42
Sorted position: 4
Sorted predecessor chapter id: c-1 
Sorted follower chapter id: t19

Chapter id: t19
Sorted position: 5
Sorted predecessor chapter id: d42 
Sorted follower chapter id: none

Upvotes: 1

Views: 2719

Answers (1)

Tim C
Tim C

Reputation: 70648

You can't get the previous and/or following elements in a xsl:for-each loop, because strictly speaking xsl:for-each is not a loop, but a mapping construct.

What you could do as save the results of the sort in a variable, like so....

<xsl:variable name="chapters" as="element()*">
     <xsl:perform-sort select="chapter">
        <xsl:sort select="@id"/>
    </xsl:perform-sort>
</xsl:variable>

You could then use xsl:for-each on this, and access the previous and next values in the chapters variable itself.

<xsl:value-of select="$chapters[position() = $current_pos - 1]/@id" />

Try this XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="3.0">

    <xsl:template match="/root">
        <xsl:variable name="chapters" as="element()*">
            <xsl:perform-sort select="chapter">
                <xsl:sort select="@id"/>
            </xsl:perform-sort>
        </xsl:variable>
        <xsl:for-each select="$chapters">
            <xsl:variable name="current_id" select="@id"/>
            <xsl:variable name="current_pos" select="position()"/>
            Chapter id: <xsl:value-of select="$current_id"/>
            Sorted position: <xsl:value-of select="position()"/>
            Sorted predecessor chapter id: <xsl:value-of select="$chapters[position() = $current_pos - 1]/@id" />
            Sorted follower chapter id: ? <xsl:value-of select="$chapters[position() = $current_pos + 1]/@id" />
            <xsl:text>&#xa;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 2

Related Questions