Re: [xsl] [Accumulators] Another stupid question

Subject: Re: [xsl] [Accumulators] Another stupid question
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 6 Oct 2020 21:18:14 -0000
On 06.10.2020 22:57, Christophe Marchand cmarchand@xxxxxxxxxx wrote:
Hello !

I have another question with accumulators. I use accumulators to
calculate block height, and then to calculate block locations (y).

I have this XSL for a repro...

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

B <xsl:output method="xml" indent="true"/>

B <xsl:mode on-no-match="shallow-copy" use-accumulators="#all"/>

 B  <xsl:accumulator name="blockHeight" as="xs:double" initial-value="0.0">
 B B B  <xsl:accumulator-rule match="page">
 B B B B B  <xsl:message>Reset block height to 3.2</xsl:message>
 B B B B B  <xsl:sequence select="3.2"/>
 B B B  </xsl:accumulator-rule>
 B B B  <xsl:accumulator-rule match="private:block">
 B B B B B  <xsl:message>Adding block height {@id} : {@height}</xsl:message>
 B B B B B  <xsl:sequence select="$value + xs:double(@height)"/>
 B B B  </xsl:accumulator-rule>
 B  </xsl:accumulator>

 B  <xsl:template match="block">
 B B B  <xsl:variable name="height" as="xs:double" select="3.5"/>
 B B B  <!-- produce some output -->
 B B B  <xsl:variable name="TempTree" as="element(private:block)">
 B B B B B  <private:block height="{$height}"/>
 B B B  </xsl:variable>
 B B B  <xsl:apply-templates select="$TempTree"/>
 B B B  <xsl:copy>
 B B B B B  <xsl:attribute name="y"
select="accumulator-after('blockHeight')"/>
 B B B B B  <xsl:apply-templates select="node() | @*"/>
 B B B  </xsl:copy>
 B  </xsl:template>

 B  <xsl:template match="private:block">
 B B B  <xsl:message>private:block matched</xsl:message>
 B  </xsl:template>

 B  <xsl:template name="xsl:initial-template">
 B B B  <xsl:variable name="content" as="document-node()">
 B B B B B  <xsl:document>
 B B B B B B B  <doc>
 B B B B B B B B B  <page id="p1">
 B B B B B B B B B B B  <block id="b1">
 B B B B B B B B B B B B B  <!-- arbitrary content -->
 B B B B B B B B B B B  </block>
 B B B B B B B B B B B  <block id="b2">
 B B B B B B B B B B B B B  <!-- another arbitrary content -->
 B B B B B B B B B B B  </block>
 B B B B B B B B B  </page>
 B B B B B B B B B  <page id="p2">
 B B B B B B B B B B B  <block id="b3">
 B B B B B B B B B B B B B  <!-- arbitrary content -->
 B B B B B B B B B B B  </block>
 B B B B B B B B B B B  <block id="b4">
 B B B B B B B B B B B B B  <!-- another arbitrary content -->
 B B B B B B B B B B B  </block>
 B B B B B B B B B  </page>
 B B B B B B B  </doc>
 B B B B B  </xsl:document>
 B B B  </xsl:variable>
 B B B  <xsl:apply-templates select="$content"/>
 B  </xsl:template>
</xsl:stylesheet>

When I run this XSL on the sample XML, I have messages displayed :
"Reset block height to 3.2", "private:block matched", but never the
"Adding block height..."

According to specification [1], I expect private:block matches
accumulator rule, and accumulator value being augmented by block/@height.

What am I doing wrong ?

I wonder whether you have a node in a "tree" to which accumulators apply. With the "as" attribute on <xsl:variable name="TempTree" as="element(private:block)"> I am not sure the element is part of a tree.

Let's hear what others think.

Current Thread