Unlocking the Mysteries of Data View Web Part XSL Tags – Part 19 – Miscellaneous – More Math / Number Functions

This entry is part 19 of 21 in the series Unlocking the Mysteries of Data View Web Part XSL Tags

Cross-posted from EndUserSharePoint.com

In the last article, I covered some of the XPath Math / Number functions; in this one, I’ll cover the rest. The first set included the ones that I considered somewhat tricky, and the ones in this article ought to be more straightforward.

clip_image002One thing that I forgot to mention in the previous article about the Math / Number functions: SharePoint Designer tries to be “smart” about which functions it shows you in the XPath Expression Builder. It may not be as smart as it wants to be, however. For instance, when you are in the context of the default dvt_1.body template, you’ll see the functions which make sense to use with a nodeset. If you are in the context of the default dvt_1.rowview template, you probably won’t. This *may* make sense for your situation, or it may not. The moral of the story is that even the XPath Expression Builder can only take you so far. At some point, you may end up writing your own XPath expressions right into the code. (That’s the fun part, anyway!)

round(), ceiling(), and floor()

These three functions let you do things with numbers and their decimal places. They are pretty basic functions, and here’s the skinny on each.

The most familiar will probably be round(). This function “rounds” the value to the nearest integer.

round(1.24) = 1
round(1.5) = 2
round(1.87) = 2

Sometimes you need to be more proscriptive than what round() can do for you, and that’s when you use ceiling() or floor(). For instance, you may want to find which item in a nodeset contains a value at a certain percentile. In that case, you may want to use the ceiling() function:

<xsl:variable name="PercentilePos" select="ceiling($Percentile div 100 * count($Rows))"/>

Here’s a little table which shows what each function will return with some sample values.

value round ceiling floor
1.24 1 2 1
1.49 1 2 1
1.5 2 2 1
1.51 2 2 1
1.87 2 2 1

number()

number() is truly simple. Given a text representation of a numeric value, number() will convert it to a number that XSL understands. This is much like “casting” into different types in other languages. You’ll rarely need to use it (almost all numbers are represented and managed as text in XSL), but the syntax is:

number(1.42) = 1.42

random

random is yet another of the ddwrt kids. (What would we do without that namespace??? And why isn’t it well-documented???) You supply random a lower and an upper bound, and it returns a random number between them for you:

ddwrt:Random(3, 5) returns one of the values in the set [3, 4, 5]

The function can only take integer values, so if you want a value between 0 and 1, for example, you could do this:

<xsl:value-of select="ddwrt:Random(1, 100) div 100"/>

This will return random values with two decimals of precision.

Well, there you go; that’s the rest of the Math / Number functions. I’m not sure what I’ll cover in the next article, but I’m not out of ideas yet. Any suggestions?

Series Navigation<< Unlocking the Mysteries of Data View Web Part XSL Tags – Part 18 – Miscellaneous – Some Math / Number FunctionsUnlocking the Mysteries of Data View Web Part XSL Tags – Part 20 – <xsl:import> >>

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.