多级分组,总结为HTML表格

时间:2014-03-28 18:20:57

标签: xslt sum grouping

我查看了关于Munechian分组的所有例子,并且必须非常厚,因为我似乎无法理解这个想法。我正在尝试计算示例输出中每列的总计:

Mike S.
Manager

Region 1 
          ID Last Quarter ID This Quarter ID Last Month ID This Month ID Last Week ID This Week 
 Client 1               $        $745,866             $       745,866     $526,927     $209,624 
 Client 2      $1,070,345      $2,107,518      $178,800    $1,370,890     $221,381      $40,563 
   Totals 

Region 2 
          ID Last Quarter ID This Quarter ID Last Month ID This Month ID Last Week ID This Week 
 Client 1               $        $745,866             $      $745,866     $526,927     $209,624 
 Client 3      $8,551,014      $9,620,100    $3,133,717    $3,237,586     $847,511       $1,471 
   Totals 

我的输入XML是:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="main.xsl"?>
<Person Person_NM="Mike S." Title="Manager">
  <AuditGroup AuditGroup_NM="Region 1">
    <Audit audit_NM="Client 1" LogoPath="\images\CL1.jpg">
      <KPI kpi_NM="IDLastQT" kpi_DESC="ID Last Quarter">
        <KPIValue strValue="0.00">
          <Display ordinal="1" />
        </KPIValue>
      </KPI>
      <KPI kpi_NM="IDThisQT" kpi_DESC="ID This Quarter">
        <KPIValue strValue="745866.05">
          <Display ordinal="2" />
        </KPIValue>
      </KPI>
      <KPI kpi_NM="IDLastMN" kpi_DESC="ID Last Month">
        <KPIValue strValue="0.00">
          <Display ordinal="3" />
        </KPIValue>
      </KPI>
      <KPI kpi_NM="IDThisMN" kpi_DESC="ID This Month">
        <KPIValue strValue="745866.05">
          <Display ordinal="4" />
        </KPIValue>
      </KPI>
      <KPI kpi_NM="IDLastWK" kpi_DESC="ID Last Week">
        <KPIValue strValue="526927.13">
          <Display ordinal="5" />
        </KPIValue>
      </KPI>
      <KPI kpi_NM="IDThisWK" kpi_DESC="ID This Week" URLToOpen="http://www.google.com">
        <KPIValue strValue="209623.54">
          <Display ordinal="6" />
        </KPIValue>
      </KPI>
    </Audit>
    <Audit audit_NM="Client 2" LogoPath="\images\CL2.jpg">
      <KPI kpi_NM="IDLastQT" kpi_DESC="ID Last Quarter">
        <KPIValue strValue="1070345.17">
          <Display ordinal="1" />
        </KPIValue>
      </KPI>
      <KPI kpi_NM="IDThisQT" kpi_DESC="ID This Quarter">
        <KPIValue strValue="2107517.62">
          <Display ordinal="2" />
        </KPIValue>
      </KPI>
      <KPI kpi_NM="IDLastMN" kpi_DESC="ID Last Month">
        <KPIValue strValue="178800.08">
          <Display ordinal="3" />
        </KPIValue>
      </KPI>
      <KPI kpi_NM="IDThisMN" kpi_DESC="ID This Month">
        <KPIValue strValue="1370889.90">
          <Display ordinal="4" />
        </KPIValue>
      </KPI>
      <KPI kpi_NM="IDLastWK" kpi_DESC="ID Last Week">
        <KPIValue strValue="221380.65">
          <Display ordinal="5" />
        </KPIValue>
      </KPI>
      <KPI kpi_NM="IDThisWK" kpi_DESC="ID This Week" URLToOpen="http://www.google.com">
        <KPIValue strValue="40563.25">
          <Display ordinal="6" />
        </KPIValue>
      </KPI>
    </Audit>
  </AuditGroup>
  <AuditGroup AuditGroup_NM="Region 2">
    <Audit audit_NM="Client 1" LogoPath="\images\CL1.jpg">
      <KPI kpi_NM="IDLastQT" kpi_DESC="ID Last Quarter">
        <KPIValue strValue="0.00">
          <Display ordinal="1" />
        </KPIValue>
      </KPI>
      <KPI kpi_NM="IDThisQT" kpi_DESC="ID This Quarter">
        <KPIValue strValue="745866.05">
          <Display ordinal="2" />
        </KPIValue>
      </KPI>
      <KPI kpi_NM="IDLastMN" kpi_DESC="ID Last Month">
        <KPIValue strValue="0.00">
          <Display ordinal="3" />
        </KPIValue>
      </KPI>
      <KPI kpi_NM="IDThisMN" kpi_DESC="ID This Month">
        <KPIValue strValue="745866.05">
          <Display ordinal="4" />
        </KPIValue>
      </KPI>
      <KPI kpi_NM="IDLastWK" kpi_DESC="ID Last Week">
        <KPIValue strValue="526927.13">
          <Display ordinal="5" />
        </KPIValue>
      </KPI>
      <KPI kpi_NM="IDThisWK" kpi_DESC="ID This Week" URLToOpen="http://www.google.com">
        <KPIValue strValue="209623.54">
          <Display ordinal="6" />
        </KPIValue>
      </KPI>
    </Audit>
    <Audit audit_NM="Client 3" LogoPath="\images\CL3.jpg">
      <KPI kpi_NM="IDLastQT" kpi_DESC="ID Last Quarter">
        <KPIValue strValue="8551014.09">
          <Display ordinal="1" />
        </KPIValue>
      </KPI>
      <KPI kpi_NM="IDThisQT" kpi_DESC="ID This Quarter">
        <KPIValue strValue="9620100.47">
          <Display ordinal="2" />
        </KPIValue>
      </KPI>
      <KPI kpi_NM="IDLastMN" kpi_DESC="ID Last Month">
        <KPIValue strValue="3133717.02">
          <Display ordinal="3" />
        </KPIValue>
      </KPI>
      <KPI kpi_NM="IDThisMN" kpi_DESC="ID This Month">
        <KPIValue strValue="3237586.46">
          <Display ordinal="4" />
        </KPIValue>
      </KPI>
      <KPI kpi_NM="IDLastWK" kpi_DESC="ID Last Week">
        <KPIValue strValue="847510.78">
          <Display ordinal="5" />
        </KPIValue>
      </KPI>
      <KPI kpi_NM="IDThisWK" kpi_DESC="ID This Week" URLToOpen="http://www.google.com">
        <KPIValue strValue="1471.12">
          <Display ordinal="6" />
        </KPIValue>
      </KPI>
    </Audit>
  </AuditGroup>
</Person>

我当前的XSLT是:

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

  <xsl:output method="html" />
  <xsl:template match="/">
    <xsl:apply-templates select="Person"/>
  </xsl:template>

  <xsl:template match="Person">
    <html>
      <body>
        <h2><xsl:value-of select="@Person_NM"/></h2>
        <h3><xsl:value-of select="@Title"/></h3>

        <xsl:apply-templates select="AuditGroup"/>

      </body>
    </html>
  </xsl:template>

  <xsl:template match="AuditGroup">  
    <table>
      <tr>
    <td><b><xsl:value-of select="@AuditGroup_NM"/></b></td>
      </tr>
      <tr>
        <td></td>
        <td></td>
        <xsl:for-each select="Audit">
          <xsl:if test="position()=1">
            <xsl:for-each select="KPI">
              <td><b><xsl:value-of select="@kpi_DESC"/></b></td>
            </xsl:for-each>
          </xsl:if>
        </xsl:for-each>
      </tr>

      <xsl:apply-templates select="Audit"/>

      <tr>
        <td></td>
        <td align="right"><i><b>Totals</b></i></td>
      </tr>

    </table>
  </xsl:template>

  <xsl:template match="Audit">  
    <tr>
      <td></td>
      <td><xsl:value-of select="@audit_NM"/></td>
      <xsl:for-each select="KPI/KPIValue">
        <td align="right"><xsl:value-of select="format-number(@strValue, '$###,###')"/></td>
      </xsl:for-each>
    </tr>
  </xsl:template>

</xsl:stylesheet>

我如何计算价值?我假设我首先需要通过Audit / @ Audit_ID进行分组,然后通过KPI / @ kpi_NM进行分组,这需要两个密钥但在此之后我不知所措。一个答案将不胜感激,但更容易理解一个易于理解的解释。感谢...

麦克

1 个答案:

答案 0 :(得分:0)

你不需要Muenchian分组 - 只需一把钥匙。但是,关键需要一个&#34;复合材料&#34;值为 use 参数。试试这种方式;

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

<xsl:key name="val" match="KPI" use="concat(../../@AuditGroup_NM, '|', @kpi_NM)" />

<xsl:template match="/Person">
    <html>
        <body>
            <h2><xsl:value-of select="@Person_NM"/></h2>
            <h3><xsl:value-of select="@Title"/></h3>
            <table border="1">
                <xsl:apply-templates select="AuditGroup"/>
            </table>
        </body>
    </html>
</xsl:template>

<xsl:template match="AuditGroup"> 
    <xsl:variable name="col" select="Audit[1]/KPI" />
    <tr>
        <th colspan="{count($col) + 1}"><xsl:value-of select="@AuditGroup_NM"/></th>
    </tr>
    <tr>
        <td></td>
        <xsl:for-each select="$col">
            <th><xsl:value-of select="@kpi_DESC"/></th>
        </xsl:for-each>
    </tr>
    <xsl:apply-templates select="Audit"/>
    <tr>
        <td align="right"><i><b>Totals</b></i></td>
        <xsl:for-each select="$col">
            <td align="right"><xsl:value-of select="format-number(sum(key('val', concat(../../@AuditGroup_NM, '|', @kpi_NM))/KPIValue/@strValue), '$#,###')"/></td>
        </xsl:for-each>
    </tr>
</xsl:template>

<xsl:template match="Audit">  
    <tr>
        <td><xsl:value-of select="@audit_NM"/></td>
        <xsl:for-each select="KPI">
            <td align="right"><xsl:value-of select="format-number(KPIValue/@strValue, '$#,###')"/></td>
        </xsl:for-each>
    </tr>
</xsl:template>

</xsl:stylesheet>