Sponsored

XSLT Reference

reverse()

XSLT 2.0 sequence function

Returns the items of a sequence in reverse order.

Syntax
reverse(sequence)

Description

reverse() returns a new sequence containing all the items from the argument sequence in the opposite order. The first item of the result is the last item of the input, and the last item of the result is the first item of the input. If the input sequence is empty, the empty sequence is returned.

This function is purely order-based and does not sort or otherwise reorder items by value. It is useful when you need the last element of a node-set without using [last()], or when you need to iterate over a sequence in reverse without reversing the natural document order.

Parameters

ParameterTypeRequiredDescription
sequenceitem()*YesThe sequence to reverse.

Return value

item()* — the same items as the input, in reverse order.

Examples

Reversing a node sequence

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<steps>
  <step>Connect</step>
  <step>Authenticate</step>
  <step>Query</step>
  <step>Disconnect</step>
</steps>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/steps">
    <reversed>
      <xsl:for-each select="reverse(step)">
        <step><xsl:value-of select="."/></step>
      </xsl:for-each>
    </reversed>
  </xsl:template>
</xsl:stylesheet>

Output:

<reversed>
  <step>Disconnect</step>
  <step>Query</step>
  <step>Authenticate</step>
  <step>Connect</step>
</reversed>

Getting the last child efficiently

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<log>
  <entry>First</entry>
  <entry>Second</entry>
  <entry>Latest</entry>
</log>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>

  <xsl:template match="/log">
    <xsl:value-of select="reverse(entry)[1]"/>
  </xsl:template>
</xsl:stylesheet>

Output:

Latest

Notes

  • reverse() does not sort by value; it literally inverts the order of items as they appear in the sequence.
  • When applied to node sequences, the resulting order is no longer document order. Predicates and axes applied to a reversed sequence operate on the reversed order.
  • An equivalent in XSLT 1.0 requires <xsl:sort order="descending"/> or a recursive template.
  • reverse(reverse($seq)) is identical to $seq.

See also