Question

I looked through all the examples on Munechian grouping and must be pretty thick because I can't seem to grasp the idea. I'm trying to calculate the totals for each column in the example output:

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 

my input XML is:

<?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>

and my current XSLT is:

<?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>

How would I go about calculating the values? I assume I first need to group by Audit/@Audit_ID and then by KPI/@kpi_NM which would require two keys but after that I'm at a loss. An answer would be appreciated but an easy to understand explanation would be even more appreciated. Thanks...

Mike

Was it helpful?

Solution

You don't need Muenchian grouping here - just a key. However, the key needs a "composite" value as the use parameter. try it this way;

<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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top