1,804 articles and 14,496 comments as of Wednesday, January 12th, 2011

EndUserSharePoint has combined resources with NothingButSharePoint.com. You can now find End User (Mark Miller), Developer (Jeremy Thake) and IT Pro SharePoint (Joel Oleson) content all in one place!

This site is a historical archive and is no longer being updated. Please update your favorites, bookmarks and RSS feeds.

NothingButSharePoint.com
Thursday, March 25, 2010

Unlocking the Mysteries of Data View Web Part XSL Tags – Part 18: Miscellaneous – Some Math / Number Functions

Author: Marc D. Anderson
http://mdasblog.wordpress.com

I’m going to go back to the XPath Math / Number functions for this article to cover a few of them. Most of these functions are fairly intuitive, but I’m a math guy. (Yup, I even majored in it in college back during the Renaissance.) However, I find that the “documentation” in the XPath Expression Builder is a bit obtuse for these functions so I’m just going to run through some of them “in my own words”. If I state the obvious or repeat the descriptions in the XPath Expression Builder, please forgive me. Your refund is in the mail.

average()

average() computes the average of a set of numbers. You’ll generally use this function in the body template because you need a nodeset (multiple items) for an average to make sense. This is analogous to the way you’ll typically use sum. So, you go into the XPath Expression Builder, and you select average, and you see:

sum() div count()

What the heck? In fact, there is no average function. Tricked you, didn’t we? (SharePoint Designer and I like to pal around on stuff like this.) You need to insert the column you want the average of in both the sum() and the count() functions, like this:

<xsl:value-of select="sum(/dsQueryResponse/Rows/Row/@Potential_x0020_Value) div count(/dsQueryResponse/Rows/Row/@Potential_x0020_Value)"/>

This computes the average just like we all learned in grade school, but you’ve got to do a little more work. Because I’m putting this into the dvt_1.body function, I need to specify the full XPath to the column. Here’s where this fits into the dvt_1.body template to make it useful:

<xsl:template name="dvt_1.body">
  <xsl:param name="Rows"/>
  <xsl:for-each select="$Rows" >
    <xsl:call-template name="dvt_1.rowview"/>
  </xsl:for-each>
  <tr>
    <td>
    </td>
    <td class="ms-vb" align="right" colspan="2">
      <b><xsl:value-of select="format-number(sum(/dsQueryResponse/Rows/Row/@Potential_x0020_Value) div count(/dsQueryResponse/Rows/Row/@Potential_x0020_Value), &quot;$#,##0.00;-$#,##0.00&quot;)"/></b>
    </td>
  </tr>
</xsl:template>

<xsl:template name="dvt_1.rowview">
  <tr>
    <xsl:if test="position() mod 2 = 1">
      <xsl:attribute name="class">ms-alternating</xsl:attribute>
    </xsl:if>
    <td class="ms-vb">
      <xsl:value-of select="@Title"/>
    </td>
    <td class="ms-vb" align="right">
      <xsl:value-of select="format-number(@Potential_x0020_Value, &quot;$#,##0.00;-$#,##0.00&quot;)"/>
    </td>
  </tr>
</xsl:template>

After I’ve called the dvt_1.rowview template “for-each” item, I am writing out another table row which contains the average. Note that I’ve added the format-number function around the individual values and the average so that they look more presentable.


format-number

Here’s the description of format-number() directly from the XPath Expression Builder:



format-number() simply takes a textual representation of a number and formats it using the format-pattern which you provide.

String bean man again!  When you open the XPath Expression Builder you can never quite read the descriptions of the functions very well. The trick is to manually drag the window larger:


Min and Max

These twins are actually members of the good old ddwrt family. You need to provide them with a nodeset, just like sum and count:

<xsl:value-of select="format-number(ddwrt:Max(/dsQueryResponse/Rows/Row/@Potential_x0020_Value), &quot;$#,##0.00;-$#,##0.00&quot;)"/>

Replacing my average calculation with ddwrt:Max:


Operators

The math operators aren’t functions, but they are worth listing out:

· + – the plus sign, which adds two numbers together

· - – the minus sign, which subtracts one number from another

· * – the asterisk, which is used to multiply two numbers together

· div – div, which divides the first number by the second

That last one threw you, didn’t it? You’re probably used to using the slash character (/) for division. However, in XSL and XPath, the slash means something else entirely. The slash is how you sort of “parse” the nodeset, as you can see in my use of the full XPath for Potential Value above: /dsQueryResponse/Rows/Row/@Potential_x0020_Value

These are a few of the quirky Math / Number functions which are available in XPath. I’ll run through the rest of the Math / Number functions in the next article.

Author: Marc D. Anderson
http://mdasblog.wordpress.com

Marc D. Anderson is a Co-Founder and the President of Sympraxis Consulting LLC, based in Newton, MA.  He has over 25 years of experience as a technology consultant and line manager across a wide spectrum of industries and organizational sizes.  Marc has done extensive consulting on knowledge management and collaboration and what makes them actually work in practice.  Marc is a very frequent “answerer” on the MSDN SharePoint – Design and Customization forum.

Entries in this series:
  1. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 1: Overview
  2. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 2: xsl:template
  3. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 3: xsl:call-template
  4. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 4: xsl:with-param
  5. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 5: xsl:param
  6. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 6: xsl:variable
  7. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 7: xsl:for-each
  8. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 8: xsl:sort
  9. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 9: xsl:if
  10. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 10: xsl:choose
  11. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 11: xsl:value-of
  12. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 12: Miscellaneous - Person or Group Columns
  13. Unlocking the Mysteries of Data View Web Part XSL Tags - Part 13: Miscellaneous - String Functions
  14. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 14: Miscellaneous – ddwrt Namespace Functions
  15. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 15: Miscellaneous – Field / Node Functions
  16. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 16: Miscellaneous – xsl:attribute
  17. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 17: Miscellaneous – xsl:comment and xsl:text
  18. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 18: Miscellaneous – Some Math / Number Functions
  19. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 19: Miscellaneous – More Math / Number Functions
  20. Unlocking the Mysteries of Data View Web Part XSL Tags – Part 20: xsl:import
  21. EUSP eBook Store: First SharePoint Title is Now Available
 

Please Join the Discussion

6 Responses to “Unlocking the Mysteries of Data View Web Part XSL Tags – Part 18: Miscellaneous – Some Math / Number Functions”
  1. Steve D says:

    Birlliant! I needed to do some Max/Min on the 26th and here is a fine posting on the subject from the 25th. Good timing. As always I find your blog helpful.

    I have 2 problems that I am hoping to overcome.
    1. With Max/Min it does not seem to like currency value with cents in it. If I make the cents .00 it works fine. If in the grouping I have cents then the max/min puts up the number $2,147,483,647.00. Since I am working on a project for AR and outstanding customer balances this is a very big, scary, and incorrect number :)
    2. Max/min does not seem to work with a calculated column either. I have contract_amount col and amount_due col and then on each row I have a calculated amount_paid col for display purposes. The amount_paid col shows grouped max and min as the same number – $2,147,483,647.00.

    I assume this strange number is a max for the datatype???

    Any ideas on these? It would not be a big deal for me to round the cents and drop the calculated column to make this work because this project is for tracking purposes and not hard accounting but I would like to see it work, if nothing more than I have lost an afternoon on it and don;t want to be beaten by Sharepoint.

    Thanks,
    Steve

    • Steve:

      The only way I can force the behavior you are describing is if I pass only the column name into ddwrt:Max like this:

      <xsl:value-of select="ddwrt:Max(@Potential_x0020_Value)"/>
      

      Instead, you need to pass in a nodeset, like this:

      <xsl:value-of select="ddwrt:Max(/dsQueryResponse/Rows/Row/@Potential_x0020_Value)"/>
      

      FYI: My @Potential_x0020_Value column is set up as Currency ($, ¥, €) with two decimal places.

      M.

      • Steve D says:

        Hi Marc,

        Thanks for the response. I never could get it to work but that is OK.

        I did mine like this.

        ddwrt:Min(/dsQueryResponse/Rows/Row[@Title=$fieldvalue]/@AmountDue)

        With the [@Title=$fieldvalue] in there to find only the min in the group.

        It works fine if I drop the decimal places fromt he column definition. I may just have something strange so I am not giong to sweat it. The application doesn’t need accuracy to the cents.

        As for not being able to find the Max of the AmountPaid because it is a calculated column – I can get it to work by subtracting the Due from Invoiced amounts and recalcualting into a variable in the DVWP. It didn’t like it if I tried to use a summation function right from the calcualted column, I basdically needed to re-calcualted in the markup.

        In this case it is OK because I am calcualting out a %paid value anyways and using it to make a JQury .progressbar for each group header as a visual indication.

  2. Gaurav Verma says:

    Hi Marc,

    Can we make a sitemap in xslt using parent – child relationship ?

Trackbacks

Check out what others are saying about this post...
  1. [...] the last article, I covered some of the XPath Math / Number functions; in this one, I’ll cover the rest. The first [...]




Notify me of comments to this article:


Speak and you will be heard.

We check comments hourly.
If you want a pic to show with your comment, go get a gravatar!