我正在尝试根据下面的XML创建一些时间表。这是模拟数据,因此忽略了时间上的巨大差距。基本上当天代码匹配0时,如下所示,我需要在午夜(00:00)之后拉出所有值并丢弃其余值并增加日+ 1。但是,如果进出时间在午夜重叠,例如下面的例子中的1755-0115,那么我仍然需要在文件的同一天抓住时间,但硬编码1755为00:01并且仍然增加一天。午夜之前的其他一切仍然被丢弃。所有的积木总是按顺序排列,而最早的拳击(如0900)是早晨拳。
`<Employee>
<employee_id>123456</employee_id>
<day_code>0</day_code>
<day>12-01-18</day>
<block>
<in>0900</in>
<out>1526</out>
</block>
<block>
<in>1526</in>
<out>1526</out>
</block>
<block>
<in>1526</in>
<out>1740</out>
</block>
<block>
<in>1740</in>
<out>1755</out>
</block>
<block>
<in>1755</in>
<out>0115</out>
</block>
<block>
<in>0115</in>
<out>0315</out>
</block>
</Employee>`
所以当我拉出数据时,我需要它看起来像这样。
'<Employee_Schedules>
<in>12-02-18-T00:01</in>
<out>12-02-18-T01:15</out>
<block>
<in>12-02-18-T01:15</in>
<out>12-02-18-T03:15</out>
</block>
</Employee_Schedules>`
我正在考虑使用前面的兄弟功能检查两个块。检查第一个块的时间条目是否小于第二个块的输出时间条目,第二个条件是立即检查立即块的时间是否小于输出时间,但这不是很好。
任何帮助是极大的赞赏!
我“修复”了day_code
的格式以使用XSD / XPath yyyy-mm-dd
,然后简单地编写了两个模板,将in
或out
值与第一个block
子项中的值进行比较:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:output indent="yes"/>
<xsl:mode on-no-match="shallow-skip"/>
<xsl:template match="Employee[day_code = 0]">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="Employee[day_code = 0]/block[position() gt 1 and in > ../block[1]/in and out <= ../block[1]/in]">
<xsl:copy>
<xsl:variable name="date" as="xs:date" select="xs:date(../day)"/>
<xsl:variable name="new-date" as="xs:date" select="$date + xs:dayTimeDuration('P1D')"/>
<in>
<xsl:value-of select="dateTime($new-date, xs:time('00:01:00'))"/>
</in>
<out>
<xsl:value-of select="dateTime($new-date, xs:time(replace(out, '([0-9]{2})([0-9]{2})', '$1:$2:00')))"/>
</out>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="Employee[day_code = 0]/block[position() gt 1 and in <= ../block[1]/in]">
<xsl:copy>
<xsl:variable name="date" as="xs:date" select="xs:date(../day)"/>
<xsl:variable name="new-date" as="xs:date" select="$date + xs:dayTimeDuration('P1D')"/>
<in>
<xsl:value-of select="dateTime($new-date, xs:time(replace(in, '([0-9]{2})([0-9]{2})', '$1:$2:00')))"/>
</in>
<out>
<xsl:value-of select="dateTime($new-date, xs:time(replace(out, '([0-9]{2})([0-9]{2})', '$1:$2:00')))"/>
</out>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/94rmq5Z给出了结果
<Employee>
<block>
<in>2018-12-02T00:01:00</in>
<out>2018-12-02T01:15:00</out>
</block>
<block>
<in>2018-12-02T01:15:00</in>
<out>2018-12-02T03:15:00</out>
</block>
</Employee>
注意如果第一个块元素确实发生了变化,那么应该确定应该发生什么。有<in>0900</in>
但<out>0700</out>
。
示例是XSLT 3,但可以使用xsl:mode
声明在XSLT 2中使用
<xsl:template match="@* | node()">
<xsl:apply-templates/>
</xsl:template>