使用XSLT从XML的两个不同部分获取不同的值

时间:2013-05-07 13:13:50

标签: xml xslt xpath xslt-1.0

我对其中一个XSLT进行了业务规则更改,但我不确定如何处理它。如果你看下面的XML,你会看到我有一个员工部分和一个设备部分。 employee部分具有嵌套在day标签和TimeDetail标签中的WorkOrderNumber,并且设备中嵌套有EquipmentWorkOrderNumber - > EquipmentWorkOrder。

我只需要获得不同的WorkOrderNumber,因为WorkOrderNumber和EquipmentWorkOrderNumber之间会存在关联,但现在,员工WorkOrderNumber部分中可能还有其他的EquipmentWorkOrderNumber。

我需要获得不同的WorkOrderNumber和EquipmentWorkOrderNumber而不重复这些值。我已经尝试了下面代码的许多变种而没有真正的进展。

<!-- The Key at the top --> 
<xsl:key name="distinctWoEquipWo" match="//WorkOrderNumber | //EquipmentWorkOrderNumber" use="."/>

<!-- Where I loop through the distinct values -->
<xsl:for-each select=".//WorkOrderNumber | .//EquipmentWorkOrderNumber[generate-id() = generate-id(key('distinctWoEquipWo', .)[1])]">

是否可以在选择中获得不同的WorkOrderNumber和EquipmentWorkOrderNumber值?

以下是我正在使用的XML?

<?xml-stylesheet type="text/xsl" href="TemplateTest.xsl"?>
<Timesheet>
<WeekEnding>Apr 28, 2013</WeekEnding>
<TeamMember>
    <EmployeeNumber>AB00910</EmployeeNumber>
    <EmployeeName>Demo, Derek</EmployeeName>
    <EmployeeClass>F</EmployeeClass>
    <Monday>
        <TimeDetail>
            <StartTime>Start</StartTime>
            <StopTime>Stop</StopTime>
            <WorkOrderNumber>4812537</WorkOrderNumber>
            <RegularHoursStartTime>Start</RegularHoursStartTime>
            <RegularHoursStopTime>Stop</RegularHoursStopTime>
            <RegularHoursStartTime2>Start</RegularHoursStartTime2>
            <RegularHoursStopTime2>Stop</RegularHoursStopTime2>
            <RegularHours>9</RegularHours>
            <OverTimeStartTime>Start</OverTimeStartTime>
            <OverTimeStopTime>Stop</OverTimeStopTime>
            <OverTimeStartTime2>Start</OverTimeStartTime2>
            <OverTimeStopTime2>Stop</OverTimeStopTime2>
            <OverTimeHours>Over Time</OverTimeHours>
            <OtherStartTime>Start</OtherStartTime>
            <OtherStopTime>Stop</OtherStopTime>
            <OtherStartTime2>Start</OtherStartTime2>
            <OtherStopTime2>Stop</OtherStopTime2>
            <OtherHours>Other</OtherHours>
            <PerDiem>NO</PerDiem>
            <Notes>Notes</Notes>
        </TimeDetail>
    </Monday>
    <Tuesday>
        <TimeDetail>
            <StartTime>Start</StartTime>
            <StopTime>Stop</StopTime>
            <WorkOrderNumber>4812537</WorkOrderNumber>
            <RegularHoursStartTime>Start</RegularHoursStartTime>
            <RegularHoursStopTime>Stop</RegularHoursStopTime>
            <RegularHoursStartTime2>Start</RegularHoursStartTime2>
            <RegularHoursStopTime2>Stop</RegularHoursStopTime2>
            <RegularHours>12</RegularHours>
            <OverTimeStartTime>Start</OverTimeStartTime>
            <OverTimeStopTime>Stop</OverTimeStopTime>
            <OverTimeStartTime2>Start</OverTimeStartTime2>
            <OverTimeStopTime2>Stop</OverTimeStopTime2>
            <OverTimeHours>Over Time</OverTimeHours>
            <OtherStartTime>Start</OtherStartTime>
            <OtherStopTime>Stop</OtherStopTime>
            <OtherStartTime2>Start</OtherStartTime2>
            <OtherStopTime2>Stop</OtherStopTime2>
            <OtherHours>Other</OtherHours>
            <PerDiem>NO</PerDiem>
            <Notes>Notes</Notes>
        </TimeDetail>
    </Tuesday>
    <Wednesday>
        <TimeDetail>
            <StartTime>Start</StartTime>
            <StopTime>Stop</StopTime>
            <WorkOrderNumber>4812537</WorkOrderNumber>
            <RegularHoursStartTime>Start</RegularHoursStartTime>
            <RegularHoursStopTime>Stop</RegularHoursStopTime>
            <RegularHoursStartTime2>Start</RegularHoursStartTime2>
            <RegularHoursStopTime2>Stop</RegularHoursStopTime2>
            <RegularHours>11</RegularHours>
            <OverTimeStartTime>Start</OverTimeStartTime>
            <OverTimeStopTime>Stop</OverTimeStopTime>
            <OverTimeStartTime2>Start</OverTimeStartTime2>
            <OverTimeStopTime2>Stop</OverTimeStopTime2>
            <OverTimeHours>Over Time</OverTimeHours>
            <OtherStartTime>Start</OtherStartTime>
            <OtherStopTime>Stop</OtherStopTime>
            <OtherStartTime2>Start</OtherStartTime2>
            <OtherStopTime2>Stop</OtherStopTime2>
            <OtherHours>Other</OtherHours>
            <PerDiem>NO</PerDiem>
            <Notes>Notes</Notes>
        </TimeDetail>
    </Wednesday>
    <Thursday>
        <TimeDetail>
            <StartTime>Start</StartTime>
            <StopTime>Stop</StopTime>
            <WorkOrderNumber>4812537</WorkOrderNumber>
            <RegularHoursStartTime>Start</RegularHoursStartTime>
            <RegularHoursStopTime>Stop</RegularHoursStopTime>
            <RegularHoursStartTime2>Start</RegularHoursStartTime2>
            <RegularHoursStopTime2>Stop</RegularHoursStopTime2>
            <RegularHours>8</RegularHours>
            <OverTimeStartTime>Start</OverTimeStartTime>
            <OverTimeStopTime>Stop</OverTimeStopTime>
            <OverTimeStartTime2>Start</OverTimeStartTime2>
            <OverTimeStopTime2>Stop</OverTimeStopTime2>
            <OverTimeHours>3</OverTimeHours>
            <OtherStartTime>Start</OtherStartTime>
            <OtherStopTime>Stop</OtherStopTime>
            <OtherStartTime2>Start</OtherStartTime2>
            <OtherStopTime2>Stop</OtherStopTime2>
            <OtherHours>Other</OtherHours>
            <PerDiem>NO</PerDiem>
            <Notes>Notes</Notes>
        </TimeDetail>
    </Thursday>
    <Friday>
        <TimeDetail>
            <StartTime>Start</StartTime>
            <StopTime>Stop</StopTime>
            <WorkOrderNumber>4812537</WorkOrderNumber>
            <RegularHoursStartTime>Start</RegularHoursStartTime>
            <RegularHoursStopTime>Stop</RegularHoursStopTime>
            <RegularHoursStartTime2>Start</RegularHoursStartTime2>
            <RegularHoursStopTime2>Stop</RegularHoursStopTime2>
            <RegularHours>0</RegularHours>
            <OverTimeStartTime>Start</OverTimeStartTime>
            <OverTimeStopTime>Stop</OverTimeStopTime>
            <OverTimeStartTime2>Start</OverTimeStartTime2>
            <OverTimeStopTime2>Stop</OverTimeStopTime2>
            <OverTimeHours>7</OverTimeHours>
            <OtherStartTime>Start</OtherStartTime>
            <OtherStopTime>Stop</OtherStopTime>
            <OtherStartTime2>Start</OtherStartTime2>
            <OtherStopTime2>Stop</OtherStopTime2>
            <OtherHours>Other</OtherHours>
            <PerDiem>NO</PerDiem>
            <Notes>Notes</Notes>
        </TimeDetail>
    </Friday>
    <Saturday />
    <Sunday />
    <CrewLeader>1</CrewLeader>
</TeamMember>
<TeamMember>
    <EmployeeNumber>BB7789A</EmployeeNumber>
    <EmployeeName>Employee, Daniel</EmployeeName>
    <EmployeeClass>AL</EmployeeClass>
    <Monday />
    <Tuesday>
        <TimeDetail>
            <StartTime>Start</StartTime>
            <StopTime>Stop</StopTime>
            <WorkOrderNumber>4812537</WorkOrderNumber>
            <RegularHoursStartTime>Start</RegularHoursStartTime>
            <RegularHoursStopTime>Stop</RegularHoursStopTime>
            <RegularHoursStartTime2>Start</RegularHoursStartTime2>
            <RegularHoursStopTime2>Stop</RegularHoursStopTime2>
            <RegularHours>12</RegularHours>
            <OverTimeStartTime>Start</OverTimeStartTime>
            <OverTimeStopTime>Stop</OverTimeStopTime>
            <OverTimeStartTime2>Start</OverTimeStartTime2>
            <OverTimeStopTime2>Stop</OverTimeStopTime2>
            <OverTimeHours>Over Time</OverTimeHours>
            <OtherStartTime>Start</OtherStartTime>
            <OtherStopTime>Stop</OtherStopTime>
            <OtherStartTime2>Start</OtherStartTime2>
            <OtherStopTime2>Stop</OtherStopTime2>
            <OtherHours>Other</OtherHours>
            <PerDiem>YES</PerDiem>
            <Notes>5xfullperdiem</Notes>
        </TimeDetail>
    </Tuesday>
    <Wednesday>
        <TimeDetail>
            <StartTime>Start</StartTime>
            <StopTime>Stop</StopTime>
            <WorkOrderNumber>4812537</WorkOrderNumber>
            <RegularHoursStartTime>Start</RegularHoursStartTime>
            <RegularHoursStopTime>Stop</RegularHoursStopTime>
            <RegularHoursStartTime2>Start</RegularHoursStartTime2>
            <RegularHoursStopTime2>Stop</RegularHoursStopTime2>
            <RegularHours>11</RegularHours>
            <OverTimeStartTime>Start</OverTimeStartTime>
            <OverTimeStopTime>Stop</OverTimeStopTime>
            <OverTimeStartTime2>Start</OverTimeStartTime2>
            <OverTimeStopTime2>Stop</OverTimeStopTime2>
            <OverTimeHours>Over Time</OverTimeHours>
            <OtherStartTime>Start</OtherStartTime>
            <OtherStopTime>Stop</OtherStopTime>
            <OtherStartTime2>Start</OtherStartTime2>
            <OtherStopTime2>Stop</OtherStopTime2>
            <OtherHours>Other</OtherHours>
            <PerDiem>YES</PerDiem>
            <Notes>5xfullperdiem</Notes>
        </TimeDetail>
    </Wednesday>
    <Thursday>
        <TimeDetail>
            <StartTime>Start</StartTime>
            <StopTime>Stop</StopTime>
            <WorkOrderNumber>4812537</WorkOrderNumber>
            <RegularHoursStartTime>Start</RegularHoursStartTime>
            <RegularHoursStopTime>Stop</RegularHoursStopTime>
            <RegularHoursStartTime2>Start</RegularHoursStartTime2>
            <RegularHoursStopTime2>Stop</RegularHoursStopTime2>
            <RegularHours>8</RegularHours>
            <OverTimeStartTime>Start</OverTimeStartTime>
            <OverTimeStopTime>Stop</OverTimeStopTime>
            <OverTimeStartTime2>Start</OverTimeStartTime2>
            <OverTimeStopTime2>Stop</OverTimeStopTime2>
            <OverTimeHours>3</OverTimeHours>
            <OtherStartTime>Start</OtherStartTime>
            <OtherStopTime>Stop</OtherStopTime>
            <OtherStartTime2>Start</OtherStartTime2>
            <OtherStopTime2>Stop</OtherStopTime2>
            <OtherHours>Other</OtherHours>
            <PerDiem>YES</PerDiem>
            <Notes>5xfullperdiem</Notes>
        </TimeDetail>
    </Thursday>
    <Friday>
        <TimeDetail>
            <StartTime>Start</StartTime>
            <StopTime>Stop</StopTime>
            <WorkOrderNumber>4812537</WorkOrderNumber>
            <RegularHoursStartTime>Start</RegularHoursStartTime>
            <RegularHoursStopTime>Stop</RegularHoursStopTime>
            <RegularHoursStartTime2>Start</RegularHoursStartTime2>
            <RegularHoursStopTime2>Stop</RegularHoursStopTime2>
            <RegularHours>Regular</RegularHours>
            <OverTimeStartTime>Start</OverTimeStartTime>
            <OverTimeStopTime>Stop</OverTimeStopTime>
            <OverTimeStartTime2>Start</OverTimeStartTime2>
            <OverTimeStopTime2>Stop</OverTimeStopTime2>
            <OverTimeHours>7</OverTimeHours>
            <OtherStartTime>Start</OtherStartTime>
            <OtherStopTime>Stop</OtherStopTime>
            <OtherStartTime2>Start</OtherStartTime2>
            <OtherStopTime2>Stop</OtherStopTime2>
            <OtherHours>Other</OtherHours>
            <PerDiem>YES</PerDiem>
            <Notes>5xfullperdiem</Notes>
        </TimeDetail>
    </Friday>
    <Saturday />
    <Sunday />
</TeamMember>
<TeamMember>
    <EmployeeNumber>CDE890</EmployeeNumber>
    <EmployeeName>Test, John</EmployeeName>
    <EmployeeClass>G</EmployeeClass>
    <Monday>
        <TimeDetail>
            <StartTime>Start</StartTime>
            <StopTime>Stop</StopTime>
            <WorkOrderNumber>4812537</WorkOrderNumber>
            <RegularHoursStartTime>Start</RegularHoursStartTime>
            <RegularHoursStopTime>Stop</RegularHoursStopTime>
            <RegularHoursStartTime2>Start</RegularHoursStartTime2>
            <RegularHoursStopTime2>Stop</RegularHoursStopTime2>
            <RegularHours>9</RegularHours>
            <OverTimeStartTime>Start</OverTimeStartTime>
            <OverTimeStopTime>Stop</OverTimeStopTime>
            <OverTimeStartTime2>Start</OverTimeStartTime2>
            <OverTimeStopTime2>Stop</OverTimeStopTime2>
            <OverTimeHours>Over Time</OverTimeHours>
            <OtherStartTime>Start</OtherStartTime>
            <OtherStopTime>Stop</OtherStopTime>
            <OtherStartTime2>Start</OtherStartTime2>
            <OtherStopTime2>Stop</OtherStopTime2>
            <OtherHours>Other</OtherHours>
            <PerDiem>NO</PerDiem>
            <Notes>Notes</Notes>
        </TimeDetail>
    </Monday>
    <Tuesday>
        <TimeDetail>
            <StartTime>Start</StartTime>
            <StopTime>Stop</StopTime>
            <WorkOrderNumber>4812537</WorkOrderNumber>
            <RegularHoursStartTime>Start</RegularHoursStartTime>
            <RegularHoursStopTime>Stop</RegularHoursStopTime>
            <RegularHoursStartTime2>Start</RegularHoursStartTime2>
            <RegularHoursStopTime2>Stop</RegularHoursStopTime2>
            <RegularHours>12</RegularHours>
            <OverTimeStartTime>Start</OverTimeStartTime>
            <OverTimeStopTime>Stop</OverTimeStopTime>
            <OverTimeStartTime2>Start</OverTimeStartTime2>
            <OverTimeStopTime2>Stop</OverTimeStopTime2>
            <OverTimeHours>Over Time</OverTimeHours>
            <OtherStartTime>Start</OtherStartTime>
            <OtherStopTime>Stop</OtherStopTime>
            <OtherStartTime2>Start</OtherStartTime2>
            <OtherStopTime2>Stop</OtherStopTime2>
            <OtherHours>Other</OtherHours>
            <PerDiem>NO</PerDiem>
            <Notes>Notes</Notes>
        </TimeDetail>
    </Tuesday>
    <Wednesday>
        <TimeDetail>
            <StartTime>Start</StartTime>
            <StopTime>Stop</StopTime>
            <WorkOrderNumber>4812537</WorkOrderNumber>
            <RegularHoursStartTime>Start</RegularHoursStartTime>
            <RegularHoursStopTime>Stop</RegularHoursStopTime>
            <RegularHoursStartTime2>Start</RegularHoursStartTime2>
            <RegularHoursStopTime2>Stop</RegularHoursStopTime2>
            <RegularHours>11</RegularHours>
            <OverTimeStartTime>Start</OverTimeStartTime>
            <OverTimeStopTime>Stop</OverTimeStopTime>
            <OverTimeStartTime2>Start</OverTimeStartTime2>
            <OverTimeStopTime2>Stop</OverTimeStopTime2>
            <OverTimeHours>Over Time</OverTimeHours>
            <OtherStartTime>Start</OtherStartTime>
            <OtherStopTime>Stop</OtherStopTime>
            <OtherStartTime2>Start</OtherStartTime2>
            <OtherStopTime2>Stop</OtherStopTime2>
            <OtherHours>Other</OtherHours>
            <PerDiem>NO</PerDiem>
            <Notes>Notes</Notes>
        </TimeDetail>
    </Wednesday>
    <Thursday>
        <TimeDetail>
            <StartTime>Start</StartTime>
            <StopTime>Stop</StopTime>
            <WorkOrderNumber>4812537</WorkOrderNumber>
            <RegularHoursStartTime>Start</RegularHoursStartTime>
            <RegularHoursStopTime>Stop</RegularHoursStopTime>
            <RegularHoursStartTime2>Start</RegularHoursStartTime2>
            <RegularHoursStopTime2>Stop</RegularHoursStopTime2>
            <RegularHours>8</RegularHours>
            <OverTimeStartTime>Start</OverTimeStartTime>
            <OverTimeStopTime>Stop</OverTimeStopTime>
            <OverTimeStartTime2>Start</OverTimeStartTime2>
            <OverTimeStopTime2>Stop</OverTimeStopTime2>
            <OverTimeHours>3</OverTimeHours>
            <OtherStartTime>Start</OtherStartTime>
            <OtherStopTime>Stop</OtherStopTime>
            <OtherStartTime2>Start</OtherStartTime2>
            <OtherStopTime2>Stop</OtherStopTime2>
            <OtherHours>Other</OtherHours>
            <PerDiem>NO</PerDiem>
            <Notes>Notes</Notes>
        </TimeDetail>
    </Thursday>
    <Friday>
        <TimeDetail>
            <StartTime>Start</StartTime>
            <StopTime>Stop</StopTime>
            <WorkOrderNumber>4812537</WorkOrderNumber>
            <RegularHoursStartTime>Start</RegularHoursStartTime>
            <RegularHoursStopTime>Stop</RegularHoursStopTime>
            <RegularHoursStartTime2>Start</RegularHoursStartTime2>
            <RegularHoursStopTime2>Stop</RegularHoursStopTime2>
            <RegularHours>0</RegularHours>
            <OverTimeStartTime>Start</OverTimeStartTime>
            <OverTimeStopTime>Stop</OverTimeStopTime>
            <OverTimeStartTime2>Start</OverTimeStartTime2>
            <OverTimeStopTime2>Stop</OverTimeStopTime2>
            <OverTimeHours>7</OverTimeHours>
            <OtherStartTime>Start</OtherStartTime>
            <OtherStopTime>Stop</OtherStopTime>
            <OtherStartTime2>Start</OtherStartTime2>
            <OtherStopTime2>Stop</OtherStopTime2>
            <OtherHours>Other</OtherHours>
            <PerDiem>NO</PerDiem>
            <Notes>Notes</Notes>
        </TimeDetail>
    </Friday>
    <Saturday />
    <Sunday />
</TeamMember>
<Equipment>
    <EquipmentWorkOrder>
        <EquipmentWorkOrderNumber>4812537</EquipmentWorkOrderNumber>
        <EquipmentDetail>
            <EquipmentMileage>Mileage</EquipmentMileage>
            <EquipmentHourMeter>Hour Meter</EquipmentHourMeter>
            <EquipmentType>Bucket truck</EquipmentType>
            <EquipmentNumber>8s96</EquipmentNumber>
            <MondayHours>0</MondayHours>
            <TuesdayHours>12</TuesdayHours>
            <WednesdayHours>11</WednesdayHours>
            <ThursdayHours>11</ThursdayHours>
            <FridayHours>7</FridayHours>
            <SaturdayHours>Saturday</SaturdayHours>
            <SundayHours>Sunday</SundayHours>
            <EquipmentNotes>Notes</EquipmentNotes>
        </EquipmentDetail>
        <EquipmentDetail>
            <EquipmentMileage>Mileage</EquipmentMileage>
            <EquipmentHourMeter>Hour Meter</EquipmentHourMeter>
            <EquipmentType>Line truck</EquipmentType>
            <EquipmentNumber>20x949</EquipmentNumber>
            <MondayHours>9</MondayHours>
            <TuesdayHours>12</TuesdayHours>
            <WednesdayHours>11</WednesdayHours>
            <ThursdayHours>11</ThursdayHours>
            <FridayHours>7</FridayHours>
            <SaturdayHours>Saturday</SaturdayHours>
            <SundayHours>Sunday</SundayHours>
            <EquipmentNotes>Notes</EquipmentNotes>
        </EquipmentDetail>
        <EquipmentDetail>
            <EquipmentMileage>Mileage</EquipmentMileage>
            <EquipmentHourMeter>Hour Meter</EquipmentHourMeter>
            <EquipmentType>Pickup truck</EquipmentType>
            <EquipmentNumber>38g356</EquipmentNumber>
            <MondayHours>9</MondayHours>
            <TuesdayHours>12</TuesdayHours>
            <WednesdayHours>11</WednesdayHours>
            <ThursdayHours>11</ThursdayHours>
            <FridayHours>7</FridayHours>
            <SaturdayHours>Saturday</SaturdayHours>
            <SundayHours>Sunday</SundayHours>
            <EquipmentNotes>Notes</EquipmentNotes>
        </EquipmentDetail>
        <EquipmentDetail>
            <EquipmentMileage>Mileage</EquipmentMileage>
            <EquipmentHourMeter>Hour Meter</EquipmentHourMeter>
            <EquipmentType>Air compressor</EquipmentType>
            <EquipmentNumber>1d158</EquipmentNumber>
            <MondayHours>9</MondayHours>
            <TuesdayHours>12</TuesdayHours>
            <WednesdayHours>11</WednesdayHours>
            <ThursdayHours>11</ThursdayHours>
            <FridayHours>7</FridayHours>
            <SaturdayHours>Saturday</SaturdayHours>
            <SundayHours>Sunday</SundayHours>
            <EquipmentNotes>Notes</EquipmentNotes>
        </EquipmentDetail>
    </EquipmentWorkOrder>
</Equipment>
</Timesheet>

感谢所有帮助。

1 个答案:

答案 0 :(得分:0)

问题是您需要围绕节点选择括号以包含在您的密钥中,例如(//WorkOrderNumber | //EquipmentWorkOrderNumber)[generate-id...。否则,您只是循环遍历WorkOrderNumber个节点,并且仅在密钥中包含EquipmentWorkOrderNumber并循环遍历这些节点。

更全面......

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:key name="workOrderNumberDistinct" match="WorkOrderNumber | EquipmentWorkOrderNumber" use="text()"/>
  <xsl:template match="/">
    <DistinctWorkOrderNumbers>
      <xsl:for-each select="(//WorkOrderNumber | //EquipmentWorkOrderNumber)[generate-id() = generate-id(key('workOrderNumberDistinct', text())[1])]">
        <Number>
          <xsl:value-of select="."/>
        </Number>
      </xsl:for-each>
    </DistinctWorkOrderNumbers>
  </xsl:template>
</xsl:stylesheet>

...输出

<?xml version="1.0" encoding="UTF-8"?>
<DistinctWorkOrderNumbers>
  <Number>4812537</Number>
</DistinctWorkOrderNumbers>