Re: [xsl] Preferred declarative approach for outputting tallies based on complex triggers

Subject: Re: [xsl] Preferred declarative approach for outputting tallies based on complex triggers
From: David Carlisle <davidc@xxxxxxxxx>
Date: Thu, 10 Apr 2014 13:00:19 +0100
On 10/04/2014 12:44, David Rudel wrote:
After I wrote that post I realized that, in some sense, I might be
asking the wrong question... Might one claim that if I'm still talking
about "state variables" and the like that I'm not really embracing the
declarative paradigm anyway?


Well quite I was going to ask what you mean by
"declarative/non-declarative and "updating state variables" in an XSLT
system.


Perhaps I should be asking myself "How do
I do the analysis I want to do without resorting to a subsystem of
triggers and state functions?"

I suppose one step in that direction would be to reduce dependence on
triggers (at least) by using different values for the @mode attribute.
Perhaps this would extend to reducing the number of state variables in
general that I kept track of.

To give a specific example question, consider a source with the
following format:

<items>
<item id="31" value="3"/>
<item id="21" value="1"/>
....
</items>

Each <item> element has an @id and an @value. There can be duplicates
of both. An example question to answer is: "Given a set of @id values,
an item is "special" if it has one of the @ids in this set AND it is
the first such item with that particular @id. Find the subsequence of
<items> lying between the first "special" item (if any) and the last
special item (if any). For this subsequence, return various values
like "how many total items are in the subsequence?" "How many
'special' items are in this subsequence?" "What is the average @value
for all items in this subsequene? What is the average @value for the
'special' values? ...



I'd do something like something (untested)

<xsl:variable name="sids" select="31,35"/>
<xsl:variable name="a" select="(item[1],item[@id=$sids][1])[last()]"/>
<xsl:variable name="b" select="(item[last()],item[@id=$sids][last()])[last()]"/>
<xsl:variable name="s" select="$a|item[$a&lt;&lt;.][.&lt;&lt;$b]|$b"/>


no of items <xsl:value-of select="count($s)"/>
no of specials <xsl:value-of select="count($s[@id=$sids])/>
avg  <xsl:value of select="sum($s/@value) div count($s)"/>

I'm not sure what part you'd consider to be "triggers" or "state variables"

David

Current Thread