XSLT - Adding Looped Variable Values from xml

Topics: General, Troubleshooting, XSLT
Aug 1, 2011 at 7:51 PM

I am trying to add values together from my displayed xml data.  I first created my function calls to call in the first 9 records and then the second 9 records (worked great).  In between each set I want to sum up the field values of @BYards and @Par for both sets of 9.  I then want to display the total of both @BYards and @Par overall.  Is there a way to add these values?

I posted my XSLT below.

 

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:in="http://www.composite.net/ns/transformation/input/1.0" xmlns:lang="http://www.composite.net/ns/localization/1.0" xmlns:f="http://www.composite.net/ns/function/1.0" xmlns="http://www.w3.org/1999/xhtml" exclude-result-prefixes="xsl in lang f">
  <xsl:template match="/">
    <html>
      <head>
				<!-- markup placed here will be shown in the head section of the rendered page --></head>
      <body>
        <table border="0" align="center" cellpadding="0" cellspacing="0" id="score-card">
          <tr class="sc-hdr">
            <td width="25%">
              <p><strong>Hole</strong></p>
            </td>
            <td width="25%">
              <p><strong>Yards</strong></p>
            </td>
            <td width="25%">
              <p class="tableHeadCard"><strong>Par</strong></p>
            </td>
            <td>
              <p><strong>SI</strong></p>
            </td>
          </tr>
          <xsl:for-each select="/in:inputs/in:result[@name='GetFront9Xml']/coursecard">
            <xsl:choose>
              <xsl:when test="position() mod 2 = 1">
                <tr class="sc-reg">
                  <td>
                    <p>
                      <span>
                        <xsl:value-of select="@Hole" />
                      </span>
                    </p>
                  </td>
                  <td>
                    <p>
                      <span>
                        <xsl:value-of select="@BYards" />
                      </span>
                    </p>
                  </td>
                  <td>
                    <p>
                      <span>
                        <xsl:value-of select="@Par" />
                      </span>
                    </p>
                  </td>
                  <td>
                    <p>
                      <span>
                        <xsl:value-of select="@SI" />
                      </span>
                    </p>
                  </td>
                </tr>
              </xsl:when>
              <xsl:otherwise>
                <tr class="sc-alt">
                  <td>
                    <p>
                      <span>
                        <xsl:value-of select="@Hole" />
                      </span>
                    </p>
                  </td>
                  <td>
                    <p>
                      <span>
                        <xsl:value-of select="@BYards" />
                      </span>
                    </p>
                  </td>
                  <td>
                    <p>
                      <span>
                        <xsl:value-of select="@Par" />
                      </span>
                    </p>
                  </td>
                  <td>
                    <p>
                      <span>
                        <xsl:value-of select="@SI" />
                      </span>
                    </p>
                  </td>
                </tr>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:for-each>
          <tr class="sc-inout">
            <td>
              <p>OUT</p>
            </td>
            <td>
              <p><strong>3080</strong></p>
            </td>
            <td>
              <p>35</p>
            </td>
            <td>
              <p>--</p>
            </td>
          </tr>
          <xsl:for-each select="/in:inputs/in:result[@name='GetBack9Xml']/coursecard">
            <xsl:choose>
              <xsl:when test="position() mod 2 = 1">
                <tr class="sc-reg">
                  <td>
                    <p>
                      <span>
                        <xsl:value-of select="@Hole" />
                      </span>
                    </p>
                  </td>
                  <td>
                    <p>
                      <span>
                        <xsl:value-of select="@BYards" />
                      </span>
                    </p>
                  </td>
                  <td>
                    <p>
                      <span>
                        <xsl:value-of select="@Par" />
                      </span>
                    </p>
                  </td>
                  <td>
                    <p>
                      <span>
                        <xsl:value-of select="@SI" />
                      </span>
                    </p>
                  </td>
                </tr>
              </xsl:when>
              <xsl:otherwise>
                <tr class="sc-alt">
                  <td>
                    <p>
                      <span>
                        <xsl:value-of select="@Hole" />
                      </span>
                    </p>
                  </td>
                  <td>
                    <p>
                      <span>
                        <xsl:value-of select="@BYards" />
                      </span>
                    </p>
                  </td>
                  <td>
                    <p>
                      <span>
                        <xsl:value-of select="@Par" />
                      </span>
                    </p>
                  </td>
                  <td>
                    <p>
                      <span>
                        <xsl:value-of select="@SI" />
                      </span>
                    </p>
                  </td>
                </tr>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:for-each>
 
<tr class="sc-inout">
            <td>
              <p>In</p>
            </td>
            <td>
              <p><strong>3258</strong></p>
            </td>
            <td>
              <p>36</p>
            </td>
            <td>
              <p>--</p>
            </td>
          </tr>
 
 <tr class="sc-hdr">
            <td>
              <p><strong>Total</strong></p>
            </td>
            <td>
              <p><strong>6608</strong></p>
            </td>
            <td>
              <p><strong>71</strong></p>
            </td>
            <td>
              <p>--</p>
            </td>
          </tr>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

 

Aug 1, 2011 at 11:01 PM

I think something like this should work:


<xsl:value-of select="number(@BYards)+number(@Par)" />
I'm not sure if the "number()" part is needed.

Aug 2, 2011 at 12:44 AM

Sorry, I misread your question - check http://blog.davidkaspar.com/archives/2003/10/xslt-sum-of-products-from-multiple-nodes.php

Aug 4, 2011 at 4:24 AM

Uggg.. I am having trouble totaling the 2 fields @BYards and @Par.  I tried to follow the link and still not getting it to work.  Any suggestions on where I should set the variables.  I think I am getting lost as to were to place the code into my xslt. 

Thanks

Aug 4, 2011 at 8:25 AM
Edited Aug 4, 2011 at 8:26 AM

Hello,

Marcus provided a very useful link in this question. Below is an example how you can get these total sums:

 

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:in="http://www.composite.net/ns/transformation/input/1.0" 
xmlns:lang="http://www.composite.net/ns/localization/1.0"
xmlns:f="http://www.composite.net/ns/function/1.0"
xmlns="http://www.w3.org/1999/xhtml" 
xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
exclude-result-prefixes="xsl in lang f msxsl">
 
 
  <xsl:variable name="Front9_BYards_Total" select="sum(/in:inputs/in:result[@name='GetFront9Xml']/coursecard/@BYards)" />
  <xsl:variable name="Front9_Par_Total" select="sum(/in:inputs/in:result[@name='GetFront9Xml']/coursecard/@Par)" />
  
  <xsl:variable name="Back9_BYards_Total" select="sum(/in:inputs/in:result[@name='GetBack9Xml']/coursecard/@BYards)" />
  <xsl:variable name="Back9_Par_Total" select="sum(/in:inputs/in:result[@name='GetBack9Xml']/coursecard/@Par)" />
  
  
  <xsl:variable name="tmpGeneral_BYards_Total">
       <item><xsl:value-of select="$Front9_BYards_Total" /></item>
       <item><xsl:value-of select="$Back9_BYards_Total" /></item>
  </xsl:variable>
  
  <xsl:variable name="tmpGeneral_Par_Total">
       <item><xsl:value-of select="$Front9_Par_Total" /></item>
       <item><xsl:value-of select="$Back9_Par_Total" /></item>
  </xsl:variable>
  
 
  <xsl:variable name="General_BYards_Total" select="sum(msxsl:node-set($tmpGeneral_BYards_Total)/*)" />
  <xsl:variable name="General_Par_Total" select="sum(msxsl:node-set($tmpGeneral_Par_Total)/*)" />
  
  <!-- then you can output the value of this variables -->
   <xsl:value-of select="$General_BYards_Total" />

...

Aug 5, 2011 at 5:57 AM

Thank you so much.  That helped me understand the information from the link Marcus provided earlier.   

Again, Thanks for the help!