This is a simplified version of an my actual data (gps tracking points). I have a list of times in ascending order.
<gpx>
<trk>
<trkseg>
<trkpt>
<time>2000-01-01T15:25:00Z</time>
</trkpt>
<trkpt>
<time>2000-01-01T15:26:00Z</time>
</trkpt>
<trkpt>
<time>2000-01-01T15:27:00Z</time>
</trkpt>
</trkseg>
</trk>
<trk>
<trkseg>
<trkpt>
<time>2000-01-01T15:28:00Z</time>
</trkpt>
<trkpt>
<time>2000-01-01T15:29:00Z</time>
</trkpt>
</trkseg>
<trkseg>
<trkpt>
<time>2000-01-01T16:00:00Z</time>
</trkpt>
<trkpt>
<time>2000-01-01T16:01:00Z</time>
</trkpt>
</trkseg>
</trk>
</gpx>
I want to detect the "stops" and group the times that are contiguous. For example, with a "stop" being an absence of data for 5 minutes or more, I would get
<gpx >
<trk>
<trkseg>
<trkpt>
<time>2000-01-01T15:25:00Z</time>
</trkpt>
<trkpt>
<time>2000-01-01T15:26:00Z</time>
</trkpt>
<trkpt>
<time>2000-01-01T15:27:00Z</time>
</trkpt>
<trkpt>
<time>2000-01-01T15:28:00Z</time>
</trkpt>
<trkpt>
<time>2000-01-01T15:29:00Z</time>
</trkpt>
</trkseg>
</trk>
<trk>
<trkseg>
<trkpt>
<time>2000-01-01T16:00:00Z</time>
</trkpt>
<trkpt>
<time>2000-01-01T16:01:00Z</time>
</trkpt>
</trkseg>
</trk>
</gpx>
This is a good use case for a tumbling window clause in XQuery 3.0/3.1:
Result is
So if your XSLT processor for instance is Saxon 9 which also supports XQuery you could consider doing it with XQuery instead of XSLT.
If you want to use it with XSLT then here is an XSLT 3.0 stylesheet that can be run with Saxon 9.8 (any edition) or Altova (2017 release) that tries to use
xsl:iterate
to emulate the XQuery approach of a tumbling window computation shown above:If you need to do it in XSLT 2.0 one way is to write a function that recursively processes the dateTime sequence item by item and returns a group respectively
trk
element once it finds a dateTime having a greater difference than the allowed limit (so basically implements the XQuery checkend $e next $n when $n - $e gt $stopDuration
):