基于排序的节点的排序,分组和总和?

时间:2013-06-23 12:41:58

标签: xslt xslt-1.0

 <fo:block start-indent="5mm">  
    <xsl:for-each select="//policyResult[not(AgentNumber=preceding-sibling::policyResult/AgentNumber)]" >    
    <xsl:variable name="agentNum"><xsl:value-of select="AgentNumber"></xsl:value-of>  
    </xsl:variable>                 
    <xsl:for-each select="//policyResult[AgentNumber=$agentNum]">    
<xsl:sort select="agentnumber"/>
<xsl:sort select="policynumber"/>
    <fo:table border-style="" width="100%" border-spacing="4">                          
    <fo:table-body>    
    <fo:table-row border-before-style="solid">   
    <fo:table-cell>   
    <fo:block>
           <fo:inline>Pol Number</fo:inline><xsl:value-of select="PolicyNumber"/>    
    </fo:block>    
    </fo:table-cell>        
    </fo:table-body>    
    </fo:table> 

我写了一些这样的代码来过滤重复的节点。 在这里我的问题是我必须按升序排序.xml看起来像这样

    <policyResult xmlns="" xmlns:ns2="http://ws.faithlife.com/resonant">
                                   <producttype>risk</producttype>                            <PolicyNumber>003050669</PolicyNumber>    
                    <AgentNumber>005262</AgentNumber>
                    <AnnualPremium>12000.0</AnnualPremium>                  
    </policyResult>
    <policyResult xmlns="" xmlns:ns2="http://ws.faithlife.com/resonant">
<producttype>wealth</producttype>                   
<PolicyNumber>003050669</PolicyNumber>
                    <AgentNumber>005282</AgentNumber>
                    <AnnualPremium>12000.0</AnnualPremium>                  
    </policyResult>

    <policyResult xmlns="" xmlns:ns2="http://ws.faithlife.com/resonant">
<producttype>risk</producttype>
                    <PolicyNumber>003050670</PolicyNumber>
                    <AgentNumber>005262</AgentNumber>
                    <AnnualPremium>12000.0</AnnualPremium>      

    </policyResult>
    <policyResult xmlns="" xmlns:ns2="http://ws.faithlife.com/resonant">
<producttype>wealth</producttype>
                    <PolicyNumber>003050671</PolicyNumber>
                    <AgentNumber>005263</AgentNumber>
                    <AnnualPremium>12000.0</AnnualPremium>                      
    </policyResult>
    <policyResult xmlns="" xmlns:ns2="http://ws.faithlife.com/resonant">
<producttype>wealth</producttype>
                    <PolicyNumber>003050668</PolicyNumber>
                    <AgentNumber>005265</AgentNumber>
                    <AnnualPremium>12000.0</AnnualPremium>                  
    </policyResult>
    <policyResult xmlns="" xmlns:ns2="http://ws.faithlife.com/resonant">
<producttype>wealth</producttype>
                    <PolicyNumber>003050668</PolicyNumber>
                    <AgentNumber>005265</AgentNumber>
                    <AnnualPremium>12000.0</AnnualPremium>                  
    </policyResult>

我要做的是按升序过滤AgentNumber,AnnualPremium。并且需要将每个代理的Annualpremium和所有Annualpremium的总和相加。

输出应该是这样的。 如果代理人 数字重复我们只需按升序显示一次

产品类型:风险

代理人编号005266

policynumber 003050669 anuual premium 12000

policynumber 003050671 anuual premium 12000

该年度总保费 - 该代理人的保费金额,即24000

代理人编号005267

policynumber 0001234 anuual premium 11000

policynumber 0001235 anuual premium 11000 年度总保费 - 该代理人的保险费,即22000

产品类型:财富

代理人编号005266

policynumber 003050669 anuual premium 12000

policynumber 003050671 anuual premium 12000

该年度总保费 - 该代理人的保费金额,即24000

代理人编号005267

policynumber 0001234 anuual premium 11000

policynumber 0001235 anuual premium 11000 年度总保费 - 该代理人的保险费,即22000

最后我们需要显示所有代理商的年度总保费

总保费--- 24000 + 22000 = 46000

请帮助我......提前谢谢

需要这种输出..

1 个答案:

答案 0 :(得分:0)

同样,您的输入数据与预期结果不符。今天我没有时间来纠正您的输入数据,但我已经编写了一个可以使用更正数据的转换。我用你给我的数据运行它。

在您对嵌套分组的新XSLT 1.0要求中,我发现使用基于变量的分组方法是最简单的。

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

<xsl:output method="text"/>

<xsl:template match="policies">
  <xsl:variable name="types" select="policyResult"/>
  <xsl:for-each select="$types">
    <xsl:sort select="producttype"/>
    <xsl:if test="generate-id(.)=
                  generate-id($types[producttype=current()/producttype][1])">
Product type: <xsl:value-of select="producttype"/>
      <xsl:variable name="agents"
                    select="$types[producttype=current()/producttype]"/>
      <xsl:for-each select="$agents">
        <xsl:sort select="AgentNumber"/>
        <xsl:if test="generate-id(.)=
                   generate-id($agents[AgentNumber=current()/AgentNumber][1])">
Agent number: <xsl:value-of select="AgentNumber"/>
          <xsl:for-each select="$agents[AgentNumber=current()/AgentNumber]">
Policy number: <xsl:value-of select="PolicyNumber"/> Premium: <xsl:value-of
                             select="AnnualPremium"/>
          </xsl:for-each>
Agent premium total: <xsl:value-of
      select="sum($agents[AgentNumber=current()/AgentNumber]/AnnualPremium)"/>
        </xsl:if>
      </xsl:for-each>
    </xsl:if>
  </xsl:for-each>
Premium total: <xsl:value-of select="sum( policyResult/AnnualPremium )"/>
</xsl:template>

</xsl:stylesheet>

将样式表应用于以下XML时:

<?xml version="1.0" encoding="UTF-8"?>
<policies>
  <policyResult xmlns="" xmlns:ns2="http://ws.faithlife.com/resonant">
    <producttype>risk</producttype>
    <PolicyNumber>003050669</PolicyNumber>
    <AgentNumber>005262</AgentNumber>
    <AnnualPremium>12000.0</AnnualPremium>
  </policyResult>
  <policyResult xmlns="" xmlns:ns2="http://ws.faithlife.com/resonant">
    <producttype>wealth</producttype>
    <PolicyNumber>003050669</PolicyNumber>
    <AgentNumber>005282</AgentNumber>
    <AnnualPremium>12000.0</AnnualPremium>
  </policyResult>
  <policyResult xmlns="" xmlns:ns2="http://ws.faithlife.com/resonant">
    <producttype>risk</producttype>
    <PolicyNumber>003050670</PolicyNumber>
    <AgentNumber>005262</AgentNumber>
    <AnnualPremium>12000.0</AnnualPremium>
  </policyResult>
  <policyResult xmlns="" xmlns:ns2="http://ws.faithlife.com/resonant">
    <producttype>wealth</producttype>
    <PolicyNumber>003050671</PolicyNumber>
    <AgentNumber>005263</AgentNumber>
    <AnnualPremium>12000.0</AnnualPremium>
  </policyResult>
  <policyResult xmlns="" xmlns:ns2="http://ws.faithlife.com/resonant">
    <producttype>wealth</producttype>
    <PolicyNumber>003050668</PolicyNumber>
    <AgentNumber>005265</AgentNumber>
    <AnnualPremium>12000.0</AnnualPremium>
  </policyResult>
  <policyResult xmlns="" xmlns:ns2="http://ws.faithlife.com/resonant">
    <producttype>wealth</producttype>
    <PolicyNumber>003050668</PolicyNumber>
    <AgentNumber>005265</AgentNumber>
    <AnnualPremium>12000.0</AnnualPremium>
  </policyResult>
</policies>

它产生以下输出:

Product type: risk
Agent number: 005262
Policy number: 003050669 Premium: 12000.0
Policy number: 003050670 Premium: 12000.0
Agent premium total: 24000
Product type: wealth
Agent number: 005263
Policy number: 003050671 Premium: 12000.0
Agent premium total: 12000
Agent number: 005265
Policy number: 003050668 Premium: 12000.0
Policy number: 003050668 Premium: 12000.0
Agent premium total: 24000
Agent number: 005282
Policy number: 003050669 Premium: 12000.0
Agent premium total: 12000
Premium total: 72000