我有一个项目,我需要在XML文档中获取和计算标记中的不同值。如果你看一下XML,我特意试着计算星期二不同的WorkOrderNumbers数。稍后我会做周一等等。我试过以下代码。
<xsl:variable name="tmp" select="count(//Tuesday/TimeDetail/WorkOrderNumber[generate-id() = generate-id(key('distinctWorkOrder', .)[1])])"></xsl:variable>
返回2.我也尝试了这个
<xsl:variable name="tmp2" select="count(//Tuesday/TimeDetail/WorkOrderNumber[not(.=/preceding-sibling::WorkOrderNumber)])"></xsl:variable>
它返回4.正确的答案是3。
我在下面添加了XML。有人可以指出我做错了什么吗?非常感谢。
<Timesheet>
<WeekEnding>Sep 18, 2011</WeekEnding>
<JobNumber>00010-5523</JobNumber>
<Customer>Test Company INC</Customer>
<TeamMember>
<EmployeeNumber>10566</EmployeeNumber>
<EmployeeName>Employee, One</EmployeeName>
<EmployeeClass>Class</EmployeeClass>
<Monday>
<TimeDetail>
<StartTime>Start</StartTime>
<StopTime>Stop</StopTime>
<WorkOrderNumber>14963</WorkOrderNumber>
<RegularHours>8</RegularHours>
<OverTimeHours>3</OverTimeHours>
<OtherHours>4 - Holiday</OtherHours>
<Notes>I took the afternoon off.</Notes>
</TimeDetail>
</Monday>
<Tuesday>
<TimeDetail>
<StartTime>Start</StartTime>
<StopTime>Stop</StopTime>
<WorkOrderNumber>14963</WorkOrderNumber>
<RegularHours>5</RegularHours>
<OverTimeHours>Over Time</OverTimeHours>
<OtherHours>Other</OtherHours>
<Notes>Notes</Notes>
</TimeDetail>
<TimeDetail>
<StartTime>Start</StartTime>
<StopTime>Stop</StopTime>
<WorkOrderNumber>15101</WorkOrderNumber>
<RegularHours>2</RegularHours>
<OverTimeHours>Over Time</OverTimeHours>
<OtherHours>Other</OtherHours>
<Notes>Notes</Notes>
</TimeDetail>
</Tuesday>
<Wednesday>
<TimeDetail>
<StartTime>Start</StartTime>
<StopTime>Stop</StopTime>
<WorkOrderNumber>14963</WorkOrderNumber>
<RegularHours>5</RegularHours>
<OverTimeHours>Over Time</OverTimeHours>
<OtherHours>Other</OtherHours>
<Notes>Notes</Notes>
</TimeDetail>
</Wednesday>
<Thursday>
<TimeDetail>
<StartTime>Start</StartTime>
<StopTime>Stop</StopTime>
<WorkOrderNumber>14963</WorkOrderNumber>
<RegularHours>5</RegularHours>
<OverTimeHours>Over Time</OverTimeHours>
<OtherHours>Other</OtherHours>
<Notes>Notes</Notes>
</TimeDetail>
</Thursday>
<Friday>
<TimeDetail>
<StartTime>Start</StartTime>
<StopTime>Stop</StopTime>
<WorkOrderNumber>14963</WorkOrderNumber>
<RegularHours>5</RegularHours>
<OverTimeHours>2</OverTimeHours>
<OtherHours>Other</OtherHours>
<Notes>Notes</Notes>
</TimeDetail>
</Friday>
<Saturday>
<TimeDetail>
<StartTime>Start</StartTime>
<StopTime>Stop</StopTime>
<WorkOrderNumber>14963</WorkOrderNumber>
<RegularHours>5</RegularHours>
<OverTimeHours>Over Time</OverTimeHours>
<OtherHours>Other</OtherHours>
<Notes>Notes</Notes>
</TimeDetail>
</Saturday>
<Sunday>
<TimeDetail>
<StartTime>Start</StartTime>
<StopTime>Stop</StopTime>
<WorkOrderNumber>14963</WorkOrderNumber>
<RegularHours>5</RegularHours>
<OverTimeHours>1</OverTimeHours>
<OtherHours>Other</OtherHours>
<Notes>Notes</Notes>
</TimeDetail>
</Sunday>
</TeamMember>
<TeamMember>
<EmployeeNumber>81135</EmployeeNumber>
<EmployeeName>Employee, Two</EmployeeName>
<EmployeeClass>Class</EmployeeClass>
<Monday>
<TimeDetail>
<StartTime>Start</StartTime>
<StopTime>Stop</StopTime>
<WorkOrderNumber>15100</WorkOrderNumber>
<RegularHours>5</RegularHours>
<OverTimeHours>Over Time</OverTimeHours>
<OtherHours>1 - Company Meetings</OtherHours>
<Notes>These are my notes.</Notes>
</TimeDetail>
<TimeDetail>
<StartTime>Start</StartTime>
<StopTime>Stop</StopTime>
<WorkOrderNumber>22965</WorkOrderNumber>
<RegularHours>7</RegularHours>
<OverTimeHours>Over Time</OverTimeHours>
<OtherHours>Other</OtherHours>
<Notes>Notes</Notes>
</TimeDetail>
<TimeDetail>
<StartTime>Start</StartTime>
<StopTime>Stop</StopTime>
<WorkOrderNumber>23570</WorkOrderNumber>
<RegularHours>Regular</RegularHours>
<OverTimeHours>1</OverTimeHours>
<OtherHours>Other</OtherHours>
<Notes>Notes</Notes>
</TimeDetail>
</Monday>
<Tuesday></Tuesday>
<Wednesday></Wednesday>
<Thursday>
<TimeDetail>
<StartTime>Start</StartTime>
<StopTime>Stop</StopTime>
<WorkOrderNumber>23591</WorkOrderNumber>
<RegularHours>7</RegularHours>
<OverTimeHours>Over Time</OverTimeHours>
<OtherHours>Other</OtherHours>
<Notes>Notes</Notes>
</TimeDetail>
<TimeDetail>
<StartTime>Start</StartTime>
<StopTime>Stop</StopTime>
<WorkOrderNumber>15100</WorkOrderNumber>
<RegularHours>1</RegularHours>
<OverTimeHours>1</OverTimeHours>
<OtherHours>Other</OtherHours>
<Notes>Test</Notes>
</TimeDetail>
</Thursday>
<Friday></Friday>
<Saturday></Saturday>
<Sunday></Sunday>
</TeamMember>
<TeamMember>
<EmployeeNumber>105779</EmployeeNumber>
<EmployeeName>Employee, Three</EmployeeName>
<EmployeeClass>W</EmployeeClass>
<Monday>
<TimeDetail>
<StartTime>Start</StartTime>
<StopTime>Stop</StopTime>
<WorkOrderNumber>19583</WorkOrderNumber>
<RegularHours>8</RegularHours>
<OverTimeHours>Over Time</OverTimeHours>
<OtherHours>Other</OtherHours>
<Notes>Notes</Notes>
</TimeDetail>
</Monday>
<Tuesday>
<TimeDetail>
<StartTime>Start</StartTime>
<StopTime>Stop</StopTime>
<WorkOrderNumber>15099</WorkOrderNumber>
<RegularHours>1</RegularHours>
<OverTimeHours>7</OverTimeHours>
<OtherHours>Other</OtherHours>
<Notes>Notes</Notes>
</TimeDetail>
</Tuesday>
<Wednesday>
<TimeDetail>
<StartTime>Start</StartTime>
<StopTime>Stop</StopTime>
<WorkOrderNumber>15099</WorkOrderNumber>
<RegularHours>1</RegularHours>
<OverTimeHours>7</OverTimeHours>
<OtherHours>Other</OtherHours>
<Notes>Notes</Notes>
</TimeDetail>
</Wednesday>
<Thursday>
<TimeDetail>
<StartTime>Start</StartTime>
<StopTime>Stop</StopTime>
<WorkOrderNumber>15099</WorkOrderNumber>
<RegularHours>1</RegularHours>
<OverTimeHours>7</OverTimeHours>
<OtherHours>Other</OtherHours>
<Notes>Notes</Notes>
</TimeDetail>
</Thursday>
<Friday>
<TimeDetail>
<StartTime>Start</StartTime>
<StopTime>Stop</StopTime>
<WorkOrderNumber>15099</WorkOrderNumber>
<RegularHours>1</RegularHours>
<OverTimeHours>7</OverTimeHours>
<OtherHours>Other</OtherHours>
<Notes>Notes</Notes>
</TimeDetail>
</Friday>
<Saturday></Saturday>
<Sunday></Sunday>
</TeamMember>
答案 0 :(得分:3)
此XSLT 2.0 xpath表达式将计算不同的工作订单...
count(fn:distinct-values(/*/*/Tuesday/*/WorkOrderNumber))
这是XSLT 1.0的等价物......
count(/*/*/Tuesday/*/WorkOrderNumber[not(. = following::WorkOrderNumber)])
答案 1 :(得分:1)
如果您要查找给定日期的不同元素,则需要确保日元素的名称构成键的一部分
<xsl:key name="distinctWorkOrder"
match="TimeDetail"
use="concat(local-name(..), '|', WorkOrderNumber)" />
因此,这将使用日期和工单号。然后你可以像这样计算给定日期的不同项目
<xsl:variable name="tmp"
select="count(//Tuesday/TimeDetail
[generate-id()
= generate-id(key('distinctWorkOrder', concat(local-name(..), '|', WorkOrderNumber))[1])])"/>
这应该给出答案3.