xsl-list
[Top] [All Lists]

Re: [xsl] [Accumulators] Another stupid question

2020-10-06 17:33:50
It's fundamental to accumulators that the value of an accumulator at a 
particular node in a document is a pure function of the preceding nodes in that 
document (or in the case of an "after" value, the descendant nodes), and 
nothing else (other than constant data such as global variables). You can't 
inject values into this calculation in the way you are trying to do. 
Accumulators were invented, of course, for streaming, and represent "things 
that are remembered from earlier in the stream".

Your private:block element is matched by a different "accumulator instance" 
from the one that matches the page element; an accumulator instance corresponds 
to one document, and accumulator instances for different documents don't 
interact. Otherwise, there would be a terrible dependency on order of 
processing.

Saxon evaluates unstreamed accumulators lazily: the first time you evaluate 
accumulator-before, or accumulator-after, on any node in a particular document, 
the accumulator values are computed for all nodes in that document.

Michael Kay
Saxonica


On 6 Oct 2020, at 21:58, Christophe Marchand cmarchand(_at_)oxiane(_dot_)com 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> 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";
  xmlns:xs="http://www.w3.org/2001/XMLSchema";
  xmlns:private="top:marchand"
  exclude-result-prefixes="#all"
  version="3.0">

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

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

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

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

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

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

Thanks in advance,
Christophe

[1] : 18.2.2 https://www.w3.org/TR/xslt-30/#applicability-of-accumulators 
list item 1

--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>