Meunchian分组与3键的concat

时间:2011-05-26 02:26:14

标签: xslt-1.0

我知道这可能很简单,但我通过查看类似的问题尝试了很多答案。具有3个键的concat的meunchian分组工作正常,但是如果输入不需要分组则该怎么办。我在下面提供了示例xsl。当我运行转换时得到的输出如果只有一个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>
    <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'/>
    </CLT>
</PRV>

应用于此输入的xsl,

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

所需的输出

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

1 个答案:

答案 0 :(得分:0)

获得预期结果的最简单更改是让您的密钥包含generate-id(..)

<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>
  <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"/>
  </CLT>
</PRV>

请注意,对于第一个Claimid="1,2"节点下的第一个CLM节点,这会产生CLT,就像我假设的那样。