2009-04-17 163 views
2

我有下面的XMLXsl:我如何根据总和进行分组和排序?

<ResultCollection> 
    <Result Id="551550" Pass="23" Fail="0" Owner="Dong"/> 
    <Result Id="551565" Pass="4" Fail="3" Owner="Dong"/> 
    <Result Id="551567" Pass="61" Fail="0" Owner="Mei"/> 
    <Result Id="551580" Pass="10" Fail="1" Owner="Dong"/> 
    <Result Id="551580" Pass="0" Fail="4" Owner="Sen"/> 
    <Result Id="551548" Pass="1" Fail="12" Owner="Sen"/> 
</ResultCollection> 

而且我有一个生成以下总结

Owner Total Pass Fail 
Dong 41 37 4 
Mei  61 61 0 
Sen  17 1 16 

我如何排序基于合格或不合格的这个结果的XSL?

我的XSL是这样

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:key name="FeatureOwner" match="Result" use="@Owner" /> 
    <xsl:template match="/"> 
     <html> 
     <head> 
      <title>Result Summary</title> 
     </head> 
     <body> 
       <table> 
        <tr> 
        <td> 
         <b>Owner</b></td> 
        <td> 
         <b>Total</b> 
        </td> 
        <td> 
         <b>Pass</b> 
        </td> 
        <td> 
         <b>Fail</b> 
        </td> 
        </tr> 
        <xsl:for-each select="ResultCollection/Result[generate-id(.) = generate-id(key('FeatureOwner', @Owner)[1])]"> 
        <xsl:variable name="varFeatureOwner"> 
         <xsl:value-of select="@Owner" /> 
        </xsl:variable> 
        <xsl:variable name="totFailures" select="sum(//ResultCollection/Result[@Owner=$varFeatureOwner]/@Fail)" /> 
        <xsl:variable name="totPass" select="sum(//ResultCollection/Result[@Owner=$varFeatureOwner]/@Pass)" /> 
        <xsl:variable name="total" select="$totPass+$totFailures" /> 
        <tr> 
         <td> 
          <xsl:value-of select="$varFeatureOwner"/> 
         </td> 
         <td> 
          <xsl:value-of select="$total"/> 
         </td> 
         <td> 
          <xsl:value-of select="$totPass"/> 
         </td> 
         <td> 
          <xsl:value-of select="$totFailures"/> 
         </td> 
         </tr> 
        </xsl:for-each> 
       </table> 
      </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 
+1

的代码是相当不完整的。请提供一份完整但尽可能最少的工作代码。 – 2009-04-17 01:40:08

回答

2

像这样的东西我已经重新构建你的代码,因为它缺少了很多):

这XSLT转换

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:param name="pSortBy" select="'Pass'"/> 

<xsl:key name="kResultByOwner" match="Result" 
    use="@Owner"/> 

    <xsl:template match="/"> 
     <table> 
      <xsl:for-each select= 
      "ResultCollection/Result 
       [generate-id(.) 
       = 
        generate-id(key('kResultByOwner', @Owner)[1]) 
        ]"> 
       <xsl:sort data-type="number" select= 
       "sum(key('kResultByOwner', @Owner) 
           /@*[name()=$pSortBy])"/> 

       <xsl:variable name="totFailures" select= 
       "sum(/ResultCollection/Result 
          [@Owner=current()/@Owner] 
           /@Fail 
       )" /> 
       <xsl:variable name="totPass" select= 
       "sum(/ResultCollection/Result 
          [@Owner=current()/@Owner] 
           /@Pass 
       )" /> 

       <xsl:variable name="total" select= 
        "$totPass+$totFailures" /> 
       <tr> 
        <td> 
         <xsl:value-of select="current()/@Owner"/> 
        </td> 
        <td> 
         <xsl:value-of select="$total"/> 
        </td> 
        <td> 
         <xsl:value-of select="$totPass"/> 
        </td> 
        <td> 
         <xsl:value-of select="$totFailures"/> 
        </td> 
       </tr> 
      </xsl:for-each> 
     </table> 
    </xsl:template> 
</xsl:stylesheet> 

当在最初提供的XML文档应用:

<ResultCollection> 
    <Result Id="551550" Pass="23" Fail="0" Owner="Dong"/> 
    <Result Id="551565" Pass="4" Fail="3" Owner="Dong"/> 
    <Result Id="551567" Pass="61" Fail="0" Owner="Mei"/> 
    <Result Id="551580" Pass="10" Fail="1" Owner="Dong"/> 
    <Result Id="551580" Pass="0" Fail="4" Owner="Sen"/> 
    <Result Id="551548" Pass="1" Fail="12" Owner="Sen"/> 
</ResultCollection> 

产生想要的结果):

 
Sen  17 1 16 
Dong  41 37 4 
Mei  61 61 0 

请注意以下几点

  1. 总和的值为@Pass或仅属于当前组的元素的@Fail属性(使用key()函数)。

  2. 使用<xsl:sort .../>指令按所需总和排序。

  3. 使用名为pSortBy的全局参数,其中包含要对其进行排序的和的属性的名称。

  4. 的使用XSLT功能current()

相关问题