Given this XML:
<root>
<row>
<time>08:00</time>
<sales>800</sales>
</row>
<row>
<time>08:15</time>
<sales>815</sales>
</row>
<row>
<time>08:30</time>
<sales>830</sales>
</row>
<row>
<time>08:45</time>
<sales>845</sales>
</row>
<row>
<time>11:00</time>
<sales>1100</sales>
</row>
<row>
<time>11:45</time>
<sales>1145</sales>
</row>
<row>
<time>14:15</time>
<sales>1415</sales>
</row>
<row>
<time>14:30</time>
<sales>1430</sales>
</row>
</root>
I am trying to find a way to XSLT transform by summarizing sales by 30 minute intervals. I can summarize by hourly intervals by 60 minutes using MUENCHIAN method, but I cannot use it for 30 minute since I need a custom function to do so (but I cannot use XSLT 2.0, nor .Net's custom functions). Please help!
The expected output is this:
30 minute
08:00 $1600
08:30 $1675
11:00 $1100
11:30 $1145
14:00 $1415
14:30 $1430
Solution 1.
This transformation:
when applied on the provided XML document:
produces the wanted, correct result:
Solution 2:
when this transformation is applied on the same XML document, again the wanted, correct result is produced:
Explanation:
In the variable
$vhalfHrs
we have elements whose values are the starting time of every half-hour period during the day.In the template that matches every
row
, the$vStart
variable is set ti this half-hour start-time, in which thetime
of the current node (row
) falls into.The variable
$vprecRow
is set to the preceding sibling (row
) of the currentrow
.If the time of the preceding
row
is not later than the start-half-hour-time (in $vStart), then the current
row` is the first one in this half-hour period.We output the starting half-hour period time.
We calculate and output the sum of all
row
elements whosetime
is in this same half-hour time period. They are following siblings of the currentrow
and theirtime
is not greater or equal the start of the next half-hour period.Solution 3 (XSLT 2.0):
when this transformation is applied on the same XML document as above, the same wanted, correct result is produced:
Explanation:
We are using
<xsl:for-each-group>
with thegroup-adjacent
attribute set as an expression calculating the position of the 1/2 hour period in which arow/time
is.Heavy use of the standard functions
current-group()
andcurrent-grouping-key()
.