Name

[2.0] reverse()

Given a sequence of items, returns a sequence with the items in reverse order.

Syntax

item()* reverse(item()*)

Inputs

A sequence of items. If the sequence is the empty sequence, the empty sequence is returned.

Outputs

A sequence with the input items in reverse order.

Defined in

XQuery 1.0 and XPath 2.0 Functions and Operators section 15.1, “General Functions and Operators on Sequences.”

Example

Here is a short stylesheet that creates a sequence and prints it, and then prints the sequence again after invoking the reverse() function against it:

<?xml version="1.0"?>
<!-- reverse.xsl -->
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:datatest="http://www.oreilly.com">

  <xsl:output method="text"/>

  <xsl:template match="/">

    <xsl:variable name="testSequence" as="item()*">
      <xsl:sequence 
        select="(3, 4, 5, current-date(), current-time(), 8, 'blue',
                'red', xs:float(3.14), 42, xs:date('1995-04-21'))"/>
    </xsl:variable>
    
    <xsl:text>&#xA;Here is a test of the reverse() </xsl:text>
    <xsl:text>function:&#xA;</xsl:text>

    <xsl:text>&#xA;  Our original sequence is:&#xA;&#xA;    </xsl:text>
    <xsl:value-of select="$testSequence" separator="&#xA;    "/>

    <xsl:text>&#xA;&#xA;  Passing our sequence to reverse() </xsl:text>
    <xsl:text>gives us:&#xA;&#xA;    </xsl:text>

    <xsl:value-of select="reverse($testSequence)" separator="&#xA;    "/>
  </xsl:template>

</xsl:stylesheet>

Here are the results:

Here is a test of the reverse() function: Our ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.