尝试了所有方法,但无法使用xsl stylesheet删除重复的XML行



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

我马上就可以这样写for-each-group

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

你的想法是足够的声音-问题似乎是,你正在比较ResourceName与TaskName,这将永远不会匹配:

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

follow up…如果您想纠正这个错误(并继续使用/xslt 1.0),请尝试如下操作:

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

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

从某个封闭的上下文中运行此模板

最新更新