0

我有一个项目,我需要在 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>

4

2 回答 2

3

这个 XSLT 2.0 xpath 表达式将计算不同的工单...

count(fn:distinct-values(/*/*/Tuesday/*/WorkOrderNumber))

这是 XSLT 1.0 等价物...

count(/*/*/Tuesday/*/WorkOrderNumber[not(. = following::WorkOrderNumber)])
于 2012-08-15T13:39:45.717 回答
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。

于 2012-08-14T19:59:21.097 回答