1,650 articles and 12,218 comments as of Friday, July 30th, 2010

Tuesday, March 30, 2010

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

Author: Marc D. Anderson
http://mdasblog.wordpress.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.

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

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

7 Responses to “Unlocking the Mysteries of Data View Web Part XSL Tags – Part 19: Miscellaneous – More Math / Number Functions”
  1. Brian Bedard says:

    If possible and with permissions, you could take apart some “famous” DVWP code and explain what it does.

    • Such as… ? Can you point Marc to some examples you’d like to deconstruct?

    • Yes, this definitely sounds like an intruiguing idea. Specifics would be great!

      M.

      • paisleygo says:

        How about these for some ideas:

        1. Go through the dispForm.aspx – when you have added a custom list form in place of the ootb control. (turn on all the bells and whistles – adit delete add new)

        2. I would also like to know what that mysterious stuff is outside of the tag – you know the part where all the wiring up happens.

  2. This is a good idea, paisleygo. I’ll add it to my list of article ideas.

    M.

  3. ADBurnett says:

    Dear Marc,

    Excellent -series. I have one that might stump you.

    I have a Bill of Materials list (with needed parts qty) for a circuit board and I have a list of all of our electronic components (with qty on hand) ALL I WANT TO DO IS SIMPLY “Do we have enough parts to make board X”

    Inventory QTY – BOM QTY = ? or equals or less… whatever… just basic math

    I have a second page “BOM-Compare-Test”

    It takes the Bill of Materials list and Inventory list and I created a joined dataview:

    Everything seems to work right:

    Part Name from BOM (@Title) reads out correctly!!! “WidgetX645″
    Qty from BOM (@PCBQuanitty) reads out correctly!!! “18″
    Qty from Inventory (@ITQuantity) reads out correctly!!! “12″

    THEN I try and do a math function:

    @PCBQuantity (18)+ @ITQuantity (12)

    It works for the first row (30), THEN THE SECOND Line MATH IS WRONG!!!!

    It NEVER lets go of the 18, every line after that does 18 plus whatever number is in the Inventory line.

    I take out the math, back to the @xxxxxxxx, and it displays correctly, IF I try and create an expression with the full xpath for one, both, or none I get “NaN”, zip, or crash

    My email is [email protected]

    PLEASE HELP!!!! I can send you the code…. PLEASE!!!!!!!!!!!!!!

    • Adam:

      I’d suggest that you post the code in Stump the Panel here. There are lots of folks (including me) who may be able to help. It sounds like you’re very close, but that perhaps you’re just manipulating the nodesets incorrectly as you are computing the sum.

      M.


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!