使用EventTime在日期x和y之间选择节点

时间:2014-01-27 15:17:22

标签: xml xslt xslt-2.0

我有以下xml文件,其中包含17天滚动的时钟输入/输出打卡数据。 XSLT在每250个节点拆分文件。我想稍微进一步,只包括一拳“今天 - 3天和今天 - 10天之间的事件时间。如果冲击事件时间不在这些日期之间那么它将被忽略而不包括在新创建的文件。

我是xslt转换的新手,经过2天的Google搜索,我仍然不确定从哪里开始。

任何方向都会受到赞赏。

以下是XML文件的示例:

<?xml version="1.0" encoding="UTF-8"?>
<tns:TimeAndAttendance xmlns:tns="OptiLink.TimeAndAttendanceFileImporter" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <tns:Punch Direction="Out" EmployeeId="100607240" EventTime="2014-01-11 19:25:00" IsProductive="0" Unit="3004922051"/>
    <tns:Punch Direction="Out" EmployeeId="100627860" EventTime="2014-01-12 19:25:00" IsProductive="0" Unit="3005031600"/>
    <tns:Punch Direction="Out" EmployeeId="100678310" EventTime="2014-01-13 19:25:00" IsProductive="0" Unit="3005536000"/>
    <tns:Punch Direction="Out" EmployeeId="100679310" EventTime="2014-01-14 19:25:00" IsProductive="0" Unit="3006225500"/>
    <tns:Punch Direction="Out" EmployeeId="100675370" EventTime="2014-01-15 19:26:00" IsProductive="0" Unit="3000210801"/>
    <tns:Punch Direction="Out" EmployeeId="100615780" EventTime="2014-01-16 19:26:00" IsProductive="0" Unit="3000228501"/>
    <tns:Punch Direction="In" EmployeeId="100712780" EventTime="2014-01-17 19:26:00" IsProductive="0" Unit="3000231600"/>
    <tns:Punch Direction="Out" EmployeeId="100613280" EventTime="2014-01-18 19:26:00" IsProductive="0" Unit="3000314500"/>
    <tns:Punch Direction="Out" EmployeeId="100629300" EventTime="2014-01-19 19:26:00" IsProductive="0" Unit="3000314550"/>
    <tns:Punch Direction="In" EmployeeId="100725580" EventTime="2014-01-20 19:26:00" IsProductive="0" Unit="3000327400"/>
    <tns:Punch Direction="In" EmployeeId="100696010" EventTime="2014-01-21 19:26:00" IsProductive="0" Unit="3001931600"/>
    <tns:Punch Direction="Out" EmployeeId="100613280" EventTime="2014-01-22 19:26:00" IsProductive="0" Unit="3000314500"/>
    <tns:Punch Direction="Out" EmployeeId="100629300" EventTime="2014-01-23 19:26:00" IsProductive="0" Unit="3000314550"/>
    <tns:Punch Direction="In" EmployeeId="100725580" EventTime="2014-01-24 19:26:00" IsProductive="0" Unit="3000327400"/>
    <tns:Punch Direction="In" EmployeeId="100696010" EventTime="2014-01-25 19:26:00" IsProductive="0" Unit="3001931600"/>
    <tns:Punch Direction="In" EmployeeId="100725580" EventTime="2014-01-26 19:26:00" IsProductive="0" Unit="3000327400"/>
    <tns:Punch Direction="In" EmployeeId="100696010" EventTime="2014-01-27 19:26:00" IsProductive="0" Unit="3001931600"/>
</tns:TimeAndAttendance>

这是我的XSLT将文件拆分成许多文件:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tns="OptiLink.TimeAndAttendanceFileImporter" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xsl:param name="pPunches" select="250"/>
    <!-- Get the filename -->
    <xsl:variable name="filename" select="tokenize(base-uri(.), '/')[last()]"/>
    <!-- Split the filename using '\.' -->
    <xsl:variable name="filenamepart" select="tokenize($filename, '\.')"/>
    <!-- Remove the file extension -->
    <xsl:variable name="filenamepartfinal" select="$filenamepart[1]"/>
    <!-- Run the split -->
    <xsl:template match="tns:TimeAndAttendance">
        <xsl:for-each-group select="tns:Punch"
                                group-adjacent="(position()-1) idiv $pPunches">
            <xsl:result-document  href="TimeAndAttendance\{$filenamepartfinal}-{current-grouping-key()}.xml">
                <tns:TimeAndAttendance>
                    <xsl:copy-of select="current-group()"/>
                </tns:TimeAndAttendance>
            </xsl:result-document>
        </xsl:for-each-group>
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

您的EventTime值几乎都是正确的词汇形式,可以转换为xs:dateTime(他们只需要将这两个部分分隔为T而不是空格),所以你可以试试像

这样的东西
<xsl:template match="tns:TimeAndAttendance">
    <!-- xs:dateTime representing ten days ago -->
    <xsl:variable name="from" select="current-dateTime() - xs:dayTimeDuration('P10D')" as="xs:dateTime"/>
    <!-- xs:dateTime representing three days ago -->
    <xsl:variable name="to" select="current-dateTime() - xs:dayTimeDuration('P3D')" as="xs:dateTime"/>
    <!-- filter just the punches between $from and $to dates -->
    <xsl:for-each-group select="tns:Punch[
          xs:dateTime(translate(@EventTime, ' ', 'T')) gt $from and
          xs:dateTime(translate(@EventTime, ' ', 'T')) lt $to]"
        group-adjacent="(position()-1) idiv $pPunches">