xslt排序和基于排序的节点总和..?

时间:2013-06-20 18:43:49

标签: 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 :(得分:1)

这个答案应该作为分组问题处理,而不是路径问题。

我注意到您的“预期结果”与您的输入不符,因此我修改了您的输入以符合您的预期结果。

考虑以下解决方案。鉴于您已将其标记为XSLT 1.0,您需要使用Muenchen分组方法。

t:\ftemp>type policies.xml
<?xml version="1.0" encoding="UTF-8"?>
<policies>
<policyResult xmlns="" xmlns:ns2="http://ws.faithlife.com/resonant">
                <PolicyNumber>003050669</PolicyNumber>
                <AgentNumber>005266</AgentNumber>
                <AnnualPremium>12000.0</AnnualPremium>
</policyResult>
<policyResult xmlns="" xmlns:ns2="http://ws.faithlife.com/resonant">
                <PolicyNumber>0001235</PolicyNumber>
                <AgentNumber>005267</AgentNumber>
                <AnnualPremium>11000.0</AnnualPremium>
</policyResult>
<policyResult xmlns="" xmlns:ns2="http://ws.faithlife.com/resonant">
                <PolicyNumber>0001234</PolicyNumber>
                <AgentNumber>005267</AgentNumber>
                <AnnualPremium>11000.0</AnnualPremium>
</policyResult>
<policyResult xmlns="" xmlns:ns2="http://ws.faithlife.com/resonant">
                <PolicyNumber>003050671</PolicyNumber>
                <AgentNumber>005266</AgentNumber>
                <AnnualPremium>12000.0</AnnualPremium>
</policyResult>
</policies>
t:\ftemp>xslt policies.xml policies.xsl

Agent Number: 005266
Policy Number: 003050669 Premium: 12000.0
Policy Number: 003050671 Premium: 12000.0
Total for agent: 24000
Agent Number: 005267
Policy Number: 0001234 Premium: 11000.0
Policy Number: 0001235 Premium: 11000.0
Total for agent: 22000
Total for all: 46000
t:\ftemp>type policies.xsl
<?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"/>

<!--establish the key for the grouping-->
<xsl:key name="agent" match="policyResult" use="AgentNumber"/>

<xsl:template match="policies">
  <!--perform the grouping per agent-->
  <xsl:for-each
    select="policyResult[generate-id(.)=
                         generate-id(key('agent',AgentNumber)[1])]">
    <!--sort the groups by AgentNumber-->
    <xsl:sort select="AgentNumber"/>
Agent Number: <xsl:value-of select="AgentNumber"/>
    <!--revisit policies for agent-->
    <xsl:for-each select="key('agent',AgentNumber)">
      <!--sort groups by policy number-->
      <xsl:sort select="PolicyNumber"/>
Policy Number: <xsl:value-of select="PolicyNumber"/> Premium: <xsl:value-of
                             select="AnnualPremium"/>
    </xsl:for-each>
Total for agent: <xsl:value-of
                      select="sum(key('agent',AgentNumber)/AnnualPremium)"/>
  </xsl:for-each>
Total for all: <xsl:value-of select="sum(policyResult/AnnualPremium)"/>
</xsl:template>

</xsl:stylesheet>
t:\ftemp>

我会把XSL-FO留给你。