XSLT按日期范围分组,并计算分组元素 - 可能吗?

时间:2014-06-23 19:37:22

标签: xml datetime xslt grouping xslt-2.0

这里有两个问题。 我的XML结构如下:

<root>
    <Detection>
        <Time>2000-01-01T00:12:00Z</Time>
    </Detection>
    <Detection>
        <Time>2000-01-01T00:34:00Z</Time>
    </Detection>
    <Detection>
        <Time>2000-01-01T02:33:00Z</Time>
    </Detection>
</root>

我想通过匹配基于日期范围的元素来转换此xml。具体而言,同一小时内发生的<Detection>s应该组合在一起,并替换为指定该小时的单个<Detection>,其中包含<Count>元素,该元素告诉我们有多少原始<Detection>s在这个新的<Detection>中。混乱?

基于以上输入的示例输出:

<root>
    <Detection>
    <!-- there were two detections occuring during the midnight hour -->
        <Time>2000-01-01T00:00:00Z</Time>
        <Count>2</Count>
    </Detection>
    <Detection>
    <!-- there was one detection during the 2 oclock hour -->
        <Time>2000-01-01T02:00:00Z</Time>
        <Count>1</Count>
    </Detection>
</root>

我刚刚开始使用XSLT。 xsl:for-each-group似乎是感兴趣的功能。难度似乎是从DateTime元素的值(例如00:12:00)导出每小时DateTime(例如00:00:00)。第二个困难是跟踪已分箱的元素数量。为了不让我简单地解决这个问题,我的问题很简单:

这可能(不是非常困难)与XSLT有关吗?

非常感谢任何进一步的输入或示例!

1 个答案:

答案 0 :(得分:2)

如果您只对小时部分感兴趣,并且没有向上/向下舍入到最接近的小时,只需在第一个{{1}之前'分组'时间部分}

:

然后,它不是'binning'元素的情况。 <xsl:for-each-group select="Detection" group-by="substring-before(Time, ':')"> 功能可用于获取您群组中的所有检测。例如

current-group()

试试这个XSLT

<Count><xsl:value-of select="count(current-group())" /></Count>
相关问题