我在使用XSLT 1.0进行转换时发现了逻辑上实现的难度。这是我的要求:我需要在员工工作的时间上定义以下逻辑。 1.一天以上的一切都会在一天之内定期使用。 2.如果连续工作3天,则第3天小时将是一倍。
这是一个示例:
输入:
<Input>
<Record>
<EmpId>1</EmpId>
<Date></Date>
<Hours></Hours>
</Record>
<Record>
<EmpId>1</EmpId>
<Date>12/1/2012</Date>
<Hours>6</Hours>
</Record>
<Record>
<EmpId>1</EmpId>
<Date>12/1/2012</Date>
<Hours>4</Hours>
</Record>
<Record>
<EmpId>1</EmpId>
<Date>12/2/2012</Date>
<Hours>4</Hours>
</Record>
<Record>
<EmpId>1</EmpId>
<Date>12/2/2012</Date>
<Hours></Hours>
</Record>
<Record>
<EmpId>1</EmpId>
<Date>12/3/2012</Date>
<Hours>3</Hours>
</Record>
<Record>
<EmpId>2</EmpId>
<Date>12/1/2012</Date>
<Hours>4</Hours>
</Record>
<Record>
<EmpId>2</EmpId>
<Date>12/1/2012</Date>
<Hours></Hours>
</Record>
</Input>
输出:
<Output>
<Record>
<EmployeeId>1</EmployeeId>
<Detail>
<Date>12/1/2012</Date>
<RegHours>8</Reghours>
<OTHours>2</OTHours>
</Detail>
<Detail>
<Date>12/2/2012</Date>
<RegHours>4</Reghours>
</Detail>
<Detail>
<Date>12/3/2012</Date>
<DTHours>3</DThours>
</Detail>
</Record>
<Record>
<EmployeeId>2</EmployeeId>
<Detail>
<Date>12/1/2012</Date>
<RegHours>4</Reghours>
</Detail>
<Detail>
</Record>
</Output>
将感谢任何帮助。
谢谢..Abhi
此转换:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kRecByEmpId" match="Record[Date/node() and Hours/node()]"
use="EmpId"/>
<xsl:key name="kRecByEmpIdDate" match="Record[Date/node() and Hours/node()]"
use="concat(EmpId,'+',Date)"/>
<xsl:template match=
"Record[generate-id()=generate-id(key('kRecByEmpId',EmpId)[1])]">
<Record>
<EmployeeId>
<xsl:value-of select="EmpId"/>
</EmployeeId>
<xsl:apply-templates mode="datetime" select=
"key('kRecByEmpId', EmpId)
[generate-id()
=
generate-id(key('kRecByEmpIdDate',
concat(EmpId,'+',Date)
)[1]
)
]
"/>
</Record>
</xsl:template>
<xsl:template match="Record" mode="datetime">
<Detail>
<xsl:copy-of select="Date"/>
<xsl:variable name="vTotal" select=
"sum(key('kRecByEmpIdDate', concat(EmpId,'+',Date))/Hours)"/>
<RegHours>
<xsl:value-of select="8*($vTotal >= 8) + $vTotal*(not($vTotal >= 8))"/>
</RegHours>
<xsl:if test="$vTotal > 8">
<OTHours>
<xsl:value-of select="$vTotal -8"/>
</OTHours>
</xsl:if>
</Detail>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
应用于提供的XML文档:
<Input>
<Record>
<EmpId>1</EmpId>
<Date></Date>
<Hours></Hours>
</Record>
<Record>
<EmpId>1</EmpId>
<Date>12/1/2012</Date>
<Hours>6</Hours>
</Record>
<Record>
<EmpId>1</EmpId>
<Date>12/1/2012</Date>
<Hours>4</Hours>
</Record>
<Record>
<EmpId>1</EmpId>
<Date>12/2/2012</Date>
<Hours>4</Hours>
</Record>
<Record>
<EmpId>1</EmpId>
<Date>12/2/2012</Date>
<Hours></Hours>
</Record>
<Record>
<EmpId>1</EmpId>
<Date>12/3/2012</Date>
<Hours>3</Hours>
</Record>
<Record>
<EmpId>2</EmpId>
<Date>12/1/2012</Date>
<Hours>4</Hours>
</Record>
<Record>
<EmpId>2</EmpId>
<Date>12/1/2012</Date>
<Hours></Hours>
</Record>
</Input>
产生想要的,正确的结果:
<Record>
<EmployeeId>1</EmployeeId>
<Detail>
<Date>12/1/2012</Date>
<RegHours>8</RegHours>
<OTHours>2</OTHours>
</Detail>
<Detail>
<Date>12/2/2012</Date>
<RegHours>4</RegHours>
</Detail>
<Detail>
<Date>12/3/2012</Date>
<RegHours>3</RegHours>
</Detail>
</Record>
<Record>
<EmployeeId>2</EmployeeId>
<Detail>
<Date>12/1/2012</Date>
<RegHours>4</RegHours>
</Detail>
</Record>