2009-04-17 12 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を持っていますか?

このXSLT変換:

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

コードの使用は非常に不完全です。完全でありながら最低限の作業コードを提供してください。 –

答えて

2

何かのように見えます

<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. 合計の値のうち現在のグループだけに属している要素(key()関数の使用)には、10または@Failという属性があります。

  2. <xsl:sort .../>命令を使用して、必要な合計でソートします。

  3. pSortByという名前のグローバルパラメータを使用します。このパラメータには、ソートする合計の属性名が含まれています。

  4. XSLT機能current()

関連する問題