XSLT Reference
accumulator-before()
Returns the value of a named accumulator computed before processing the current node in streaming mode.
accumulator-before(name)Description
accumulator-before() returns the value of a named accumulator as it was just before the current node was processed. Accumulators are XSLT 3.0 constructs that compute running values as the processor traverses a document — similar to a running total or state machine. The “before” value reflects the accumulator state prior to applying any accumulator rule for the current node.
The function is used inside xsl:accumulator-rule actions and in template rules that access accumulator state. The name argument is a string literal matching the name attribute of an xsl:accumulator declaration.
For the function to be available in a template, the template must declare the accumulator in its use-accumulators attribute (or via xsl:use-accumulators).
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
name | xs:string | Yes | The name of the accumulator to read. |
Return value
The declared return type of the named accumulator — the value computed just before the current node is entered.
Examples
Running total accumulator
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<sales>
<sale amount="100"/>
<sale amount="250"/>
<sale amount="75"/>
</sales>
Stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" indent="yes"/>
<xsl:accumulator name="running-total" initial-value="xs:decimal(0)">
<xsl:accumulator-rule match="sale" select="$value + xs:decimal(@amount)"/>
</xsl:accumulator>
<xsl:template match="/sales">
<report>
<xsl:apply-templates select="sale" use-accumulators="running-total"/>
</report>
</xsl:template>
<xsl:template match="sale" use-accumulators="running-total">
<sale amount="{@amount}" before="{accumulator-before('running-total')}"/>
</xsl:template>
</xsl:stylesheet>
Output:
<report>
<sale amount="100" before="0"/>
<sale amount="250" before="100"/>
<sale amount="75" before="350"/>
</report>
Comparing before and after values
Stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="xml" indent="yes"/>
<xsl:accumulator name="count" initial-value="xs:integer(0)">
<xsl:accumulator-rule match="sale" select="$value + 1"/>
</xsl:accumulator>
<xsl:template match="/sales">
<xsl:apply-templates select="sale" use-accumulators="count"/>
</xsl:template>
<xsl:template match="sale" use-accumulators="count">
<item seq-before="{accumulator-before('count')}" seq-after="{accumulator-after('count')}"/>
</xsl:template>
</xsl:stylesheet>
Output:
<item seq-before="0" seq-after="1"/>
<item seq-before="1" seq-after="2"/>
<item seq-before="2" seq-after="3"/>
Notes
accumulator-before()reads the accumulator value before the node’s rule fires;accumulator-after()reads it after.- The accumulator must be declared with
xsl:accumulatorat the top level and listed in the template’suse-accumulatorsattribute. - Accumulators are primarily designed for streaming, but they also work in non-streaming transformations.
- The initial value is used as the “before” value for the first matched node.