尝试了一切,但无法使用xsl样式表删除重复的XML行

时间:2011-09-27 18:47:04

标签: xml xslt

作为xsl的新手,我尝试了这些帖子中的各种示例,但对于我的特定XML文件,我无法删除重复的“TaskName”行。我已成功删除多个“ResourceName”条目,但只希望给定任务在ResourceName下出现一次。

我非常感谢任何建议。这是我的xml(对于长文件很抱歉,但我认为需要清晰),我的xsl,我得到的输出,并显示我想要的输出。

XML来源:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="ResourceAssignments.xsl"?>
<ResourceAllocationReport>
   <DetailedData>
      <ResourceAllocation>
         <Group>Week Starting on 7/24/11</Group>
         <TaskResourceAllocation>
            <TaskName>DEV-23876 Task A</TaskName>
            <TaskDuration>5 days</TaskDuration>
            <ResourceName>John Smith</ResourceName>
            <AllocatedHours>2.400</AllocatedHours>
         </TaskResourceAllocation>
      </ResourceAllocation>
      <ResourceAllocation>
         <Group>Week Starting on 7/31/11</Group>
         <TaskResourceAllocation>
            <TaskName>DEV-23873 Task B</TaskName>
            <TaskDuration>2 days</TaskDuration>
            <ResourceName>John Smith</ResourceName>
            <AllocatedHours>4.000</AllocatedHours>
         </TaskResourceAllocation>
      </ResourceAllocation>
      <ResourceAllocation>
         <Group>Week Starting on 8/7/11</Group>
         <TaskResourceAllocation>
            <TaskName>DEV-23879 Task C</TaskName>
            <TaskDuration>10 days</TaskDuration>
            <ResourceName>John Smith</ResourceName>
            <AllocatedHours>1.600</AllocatedHours>
         </TaskResourceAllocation>
      </ResourceAllocation>
      <ResourceAllocation>
         <TaskResourceAllocation>
            <TaskName>DEV-22837  List edit</TaskName>
            <TaskDuration>10 days</TaskDuration>
            <ResourceName>Sam Adams</ResourceName>
            <AllocatedHours>5.000</AllocatedHours>
         </TaskResourceAllocation>
      </ResourceAllocation>
      <ResourceAllocation>
         <Group>Week Starting on 9/4/11</Group>
         <TaskResourceAllocation>
            <TaskName>DEV-19006  System Configuration Report</TaskName>
            <TaskDuration>12 days</TaskDuration>
            <ResourceName>Kimberly Clark</ResourceName>
            <AllocatedHours>7.000</AllocatedHours>
         </TaskResourceAllocation>
      </ResourceAllocation>
      <ResourceAllocation>
         <TaskResourceAllocation>
            <TaskName>DEV-19006  System Configuration Report</TaskName>
            <TaskDuration>12 days</TaskDuration>
            <ResourceName>Sam Adams</ResourceName>
            <AllocatedHours>7.000</AllocatedHours>
         </TaskResourceAllocation>
      </ResourceAllocation>
      <ResourceAllocation>
         <TaskResourceAllocation>
            <TaskName>DEV-22837  List edit</TaskName>
            <TaskDuration>10 days</TaskDuration>
            <ResourceName>Sam Adams</ResourceName>
            <AllocatedHours>8</AllocatedHours>
         </TaskResourceAllocation>
      </ResourceAllocation>
      <ResourceAllocation>
         <Group>Week Starting on 9/25/11</Group>
         <TaskResourceAllocation>
            <TaskName>DEV-15997  Change modifiers for components</TaskName>
            <TaskDuration>15 days</TaskDuration>
            <ResourceName>Kimberly Clark</ResourceName>
            <AllocatedHours>8.000</AllocatedHours>
         </TaskResourceAllocation>
      </ResourceAllocation>
      <ResourceAllocation>
         <Group>Week Starting on 10/2/11</Group>
         <TaskResourceAllocation>
            <TaskName>DEV-15997  Change modifiers for components</TaskName>
            <TaskDuration>15 days</TaskDuration>
            <ResourceName>Kimberly Clark</ResourceName>
            <AllocatedHours>2.000</AllocatedHours>
         </TaskResourceAllocation>
      </ResourceAllocation>
   </DetailedData>
</ResourceAllocationReport>

样式表:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html" indent="yes" /> 

  <xsl:key name="ResourceKey" match="TaskResourceAllocation" use="ResourceName" /> 

  <xsl:template match="ResourceAllocation"> 
    <body> 
      <xsl:apply-templates select="TaskResourceAllocation[generate-id(.) = 
                                   generate-id(key('ResourceKey',ResourceName)[1])]"/> 
    </body> 
  </xsl:template> 

  <xsl:template match="TaskResourceAllocation"> 
     <h2><xsl:value-of select="ResourceName" /></h2> 
     <table border="1">
        <xsl:for-each select="key('ResourceKey',ResourceName)"> 
          <xsl:if test="not(. = preceding::TaskName)">
             <tr> 
                <td> 
                   <xsl:value-of select="TaskName" /> 
                </td> 
                <td> 
                   <xsl:value-of select="TaskDuration" /> 
                </td> 
             </tr> 
          </xsl:if>
        </xsl:for-each> 
     </table> 
  </xsl:template> 
</xsl:stylesheet> 

输出(但我不想要重复的任务):

John Smith
DEV-23876 Task A 5 days 
DEV-23873 Task B 2 days 
DEV-23879 Task C 10 days 

Sam Adams
DEV-22837 List edit 10 days 
DEV-19006 System Configuration Report 12 days 
DEV-22837 List edit 10 days 

Kimberly Clark
DEV-19006 System Configuration Report 12 days 
DEV-15997 Change modifiers for components 15 days 
DEV-15997 Change modifiers for components 15 days 

期望的输出:

John Smith
DEV-23876 Task A 5 days 
DEV-23873 Task B 2 days 
DEV-23879 Task C 10 days 

Sam Adams
DEV-19006 System Configuration Report 12 days 
DEV-22837 List edit 10 days 

Kimberly Clark
DEV-19006 System Configuration Report 12 days 
DEV-15997 Change modifiers for components 15 days 

2 个答案:

答案 0 :(得分:0)

您的想法足够健全 - 问题似乎是您正在将ResourceName与TaskName进行比较,而TaskName永远不会匹配:

        <xsl:for-each select="key('ResourceKey',ResourceName)"> 
          <xsl:if test="not(. = preceding::TaskName)">

跟进...如果你想纠正这个(并保持w / xslt 1.0),尝试类似的事情:

<xsl:template match="TaskName[not(preceding::TaskName=.)]">
  ... desired output here ...
</xsl:template>

使用<xsl:apply-templates /> or <xsl:apply-templates select=".//TaskName" />

从一些封闭的上下文中运行此模板

答案 1 :(得分:0)

在我的头顶,你可以为每个小组做这样的事情

<xsl:for-each-group select="key('ResourceKey',ResourceName)" group-by="TaskName"> 
     <tr>   
       <td>   
          <xsl:value-of select="TaskName" />   
       </td>   
       <td>   
          <xsl:value-of select="TaskDuration" />   
       </td>   
     </tr>   
</xsl:for-each>