在xslt 1.0中具有3个属性的节点中进行分组

时间:2011-04-24 16:01:58

标签: xslt

我需要一些关于在xslt 1.0中对节点进行分组的帮助。我能够在xslt 2.0中使用for-each-group,但我不知道如何在1.0中执行此操作。下面显示的是示例xml,其中我想使用ServiceId,WorkerId和'ClaimId'属性的服务日期以及'Units'的总和来组合'CLM'节点。如果同一工作人员在同一天给出的服务不止一次,那么将这些“CLM”节点组合起来。

<?xml version="1.0" encoding="UTF-8"?>
<PRV ProviderId="100" PName="Giga health" Provsuv="1563">
    <CLT ClientId="4444" ClientFName="John" ClientLastName="Pulaski" Phone="56462561">
        <CLM Claimid="1"  DateOfService="01/02/2011" EndOfService="05/05/2011" ServiceId="S1" WorkerName="WORK1" WorkerId="6446" Unit= '5' Amount= '5000'/>
        <CLM Claimid="2"  DateOfService="01/02/2011" EndOfService="05/05/2011" ServiceId="S1" WorkerName="WORK1" WorkerId="6446" Unit= '6' Amount= '5000'/>
        <CLM Claimid="3"  DateOfService="01/02/2011" EndOfService="05/05/2011" ServiceId="S2" WorkerName="WORK1" WorkerId="2006" Unit= '7' Amount= '5000'/>
        <CLM Claimid="4"  DateOfService="01/03/2011" EndOfService="05/05/2011" ServiceId="S1" WorkerName="WOK2" WorkerId="6446" Unit= '3' Amount= '5000'/>
        <CLM Claimid="5"  DateOfService="01/03/2011" EndOfService="05/05/2011" ServiceId="S2" WorkerName="WORK2" WorkerId="6446" Unit= '8' Amount= '5000'/>
        <CLM Claimid="6" DateOfService="01/03/2011" EndOfService="05/05/2011" ServiceId="S2" WorkerName="WORK1" WorkerId="6446" Unit= '1' Amount= '5000'/>
    </CLT>

我的输出应该是

<?xml version="1.0" encoding="UTF-8"?>
<PRV ProviderId="100" PName="Giga health" Provsuv="1563">
    <CLT ClientId="4444" ClientFName="John" ClientLastName="Pulaski" Phone="56462561">
        <CLM Claimid="1,2"  DateOfService="01/02/2011" EndOfService="05/05/2011" ServiceId="S1" WorkerName="WORK1" WorkerId="6446" Unit= '11' Amount= '10000'/>
        <CLM Claimid="3"  DateOfService="01/02/2011" EndOfService="05/05/2011" ServiceId="S2" WorkerName="WORK1" WorkerId="2006" Unit= '7' Amount= '5000'/>
        <CLM Claimid="4"  DateOfService="01/03/2011" EndOfService="05/05/2011" ServiceId="S1" WorkerName="WOK2" WorkerId="6446" Unit= '3' Amount= '5000'/>
        <CLM Claimid="5,6"  DateOfService="01/03/2011" EndOfService="05/05/2011" ServiceId="S2" WorkerName="WORK2" WorkerId="6446" Unit= '9' Amount= '10000'/>
    </CLT>

我的输出xml应该具有'claimid'组合,并且要在特定'WorkerId'的相同DateofService上为相同的serviceId求和。提前谢谢。

1 个答案:

答案 0 :(得分:0)

此转化

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kCLMByAttribs" match="CLM" use=
 "concat(@ServiceId,'+',@WorkerId,'+',@DateOfService)"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match=
 "CLM[generate-id()
     =
      generate-id(key('kCLMByAttribs',
                      concat(@ServiceId,
                            '+',@WorkerId,
                            '+',@DateOfService)
                     )
                     [1]
                 )
     ]
 ">
  <xsl:copy>
   <xsl:copy-of select="@*"/>

   <xsl:variable name="vGroup" select=
   "key('kCLMByAttribs',
         concat(@ServiceId,
                '+',@WorkerId,
                '+',@DateOfService)
       )
   "/>

   <xsl:variable name="vClaimIds">
    <xsl:for-each select="$vGroup">
      <xsl:if test="not(position()=1)">
       <xsl:value-of select="','"/>
      </xsl:if>
      <xsl:value-of select="@Claimid"/>
    </xsl:for-each>
   </xsl:variable>

   <xsl:attribute name="Claimid">
    <xsl:value-of select="$vClaimIds"/>
   </xsl:attribute>

   <xsl:attribute name="Unit">
    <xsl:value-of select="sum($vGroup/@Unit)"/>
   </xsl:attribute>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="CLM"/>
</xsl:stylesheet>

应用于提供的XML文档

<PRV ProviderId="100" PName="Giga health"
Provsuv="1563">
    <CLT ClientId="4444" ClientFName="John"
      ClientLastName="Pulaski" Phone="56462561">
        <CLM Claimid="1"  DateOfService="01/02/2011"
          EndOfService="05/05/2011" ServiceId="S1"
          WorkerName="WORK1" WorkerId="6446"
          Unit= '5' Amount= '5000'/>
        <CLM Claimid="2"  DateOfService="01/02/2011"
         EndOfService="05/05/2011" ServiceId="S1"
         WorkerName="WORK1" WorkerId="6446"
         Unit= '6' Amount= '5000'/>
        <CLM Claimid="3"  DateOfService="01/02/2011"
         EndOfService="05/05/2011" ServiceId="S2"
         WorkerName="WORK1" WorkerId="2006"
         Unit= '7' Amount= '5000'/>
        <CLM Claimid="4"  DateOfService="01/03/2011"
         EndOfService="05/05/2011" ServiceId="S1"
         WorkerName="WOK2" WorkerId="6446"
         Unit= '3' Amount= '5000'/>
        <CLM Claimid="5"  DateOfService="01/03/2011"
         EndOfService="05/05/2011" ServiceId="S2"
         WorkerName="WORK2" WorkerId="6446"
         Unit= '8' Amount= '5000'/>
        <CLM Claimid="6" DateOfService="01/03/2011"
         EndOfService="05/05/2011" ServiceId="S2"
         WorkerName="WORK1" WorkerId="6446"
         Unit= '1' Amount= '5000'/>
    </CLT>
</PRV>

产生完全正确的结果

<PRV ProviderId="100" PName="Giga health" Provsuv="1563">
   <CLT ClientId="4444" ClientFName="John"
        ClientLastName="Pulaski" Phone="56462561">
      <CLM Claimid="1,2" DateOfService="01/02/2011"
       EndOfService="05/05/2011" ServiceId="S1"
       WorkerName="WORK1" WorkerId="6446"
       Unit="11" Amount="5000"/>
      <CLM Claimid="3" DateOfService="01/02/2011"
       EndOfService="05/05/2011" ServiceId="S2"
       WorkerName="WORK1" WorkerId="2006"
       Unit="7" Amount="5000"/>
      <CLM Claimid="4" DateOfService="01/03/2011"
       EndOfService="05/05/2011" ServiceId="S1"
       WorkerName="WOK2" WorkerId="6446"
       Unit="3" Amount="5000"/>
      <CLM Claimid="5,6" DateOfService="01/03/2011"
       EndOfService="05/05/2011" ServiceId="S2"
       WorkerName="WORK2" WorkerId="6446"
       Unit="9" Amount="5000"/>
   </CLT>
</PRV>

<强>解释

Muenchian grouping ,其中一个键是三个部分的串联。