I have a project where I need to get and count distinct values in tags in an XML document. If you look at the XML, I am specifically trying to count the distinct number of WorkOrderNumbers for Tuesday. Later I will do Monday and so on. I have tried the following code.

<xsl:variable name="tmp" select="count(//Tuesday/TimeDetail/WorkOrderNumber[generate-id() = generate-id(key('distinctWorkOrder', .)[1])])"></xsl:variable>

This returns 2. I have also tried this

<xsl:variable name="tmp2" select="count(//Tuesday/TimeDetail/WorkOrderNumber[not(.=/preceding-sibling::WorkOrderNumber)])"></xsl:variable>

It returns 4. The correct answer is 3.

I have added the XML below. Can someone point out what I am doing wrong? Thanks a lot.

<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>

I have an answer to this. I also posted this on stackoverflow and someone there provided the answer. He said;

If you are looking for distinct elements for a given day, then you will need to ensure the name of the day element forms part of the key

<xsl:key name="distinctWorkOrder" match="TimeDetail" use="concat(local-name(..), '|', WorkOrderNumber)" />

So, this will use both the day and the work order number. Then you can count the distinct items for a given day like so

<xsl:variable name="tmp" select="count(//Tuesday/TimeDetail[generate-id()=generate-id(key('distinctWorkOrder', concat(local-name(..), '|', WorkOrderNumber))[1])])"></xsl:variable>

This works perfectly for me. Problem solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.