mirror of
https://github.com/alexandrev/xslt-lab.git
synced 2026-09-18 19:43:16 +00:00
53e90ef86e
Full coverage of XSLT 1.0, 2.0 and 3.0 elements and XPath functions: - 59 XSLT elements (xsl:stylesheet → xsl:use-accumulators) - 170 XPath functions (1.0 node/string/numeric/boolean, 2.0 sequence/ date/QName/string, 3.0 HOF/map/array/JSON/streaming) Each page: description, parameters table, return value, 2 runnable Saxon examples with input XML + stylesheet + output, notes, cross-links. xsltCompletions.js: all 229 entries now have blogSlug for hover links. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2.9 KiB
2.9 KiB
title, description, date, version, versionLabel, category, syntax, tags
| title | description | date | version | versionLabel | category | syntax | tags | ||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| reverse() | Returns the items of a sequence in reverse order. | 2026-04-19T00:00:00Z | 2.0 | XSLT 2.0 | sequence function | 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
| Parameter | Type | Required | Description |
|---|---|---|---|
sequence |
item()* | Yes | The 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.