Unlocking the Mysteries of Data View Web Part XSL Tags – Part 13 – Miscellaneous: String Functions

This entry is part 13 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 talked about teasing out the various parts of the values in Person or Group column values. To do this, I relied on the substring functions which are available to us in XSL. In this article, I’ll go over a few of those string functions and show you how useful they can be.

What you create a new variable with <xsl:variable> or use <xsl:value-of> to display the contents of a variable or column (among other things), you have many different options available to you in the select attribute. When you first type the equal sign after the select, you’ll see a dropdown menu in SharePoint Designer. The first option in that dropdown is XPath Expression Builder:

clip_image002

When you click on the XPath Expression Builder, you can build up more complex XPath expressions with the Builder to help you through the process. In this article, we’re going to focus on a few of the Text / String functions:

clip_image004

As you click on each of these functions, you’ll see how to use them on the right side, as in the screenshot above showing the concat definition. I won’t repeat those definitions here as there are many other great posts out there that cover them. Instead, I’ll go over a few tricks you’ll need to know to use these functions to do cool stuff. If you’re used to using string functions in other languages, you might get frustrated with this set of functions. I know I was early on, and it took some getting used to them before I was able to crank out useful XPath expressions easily.

substring-before and substring-after

These twins work great together. Let me repeat an example from the previous article on Person or Group columns. Here’s what a typical Person or Group column value might look like:

clip_image006

If you don’t want to display the user’s name as a hyperlink, you can substring out the actual name:

<xsl:variable name="UserName" select="substring-after(substring-before(substring-after(@Author, ‘ID=’), ‘&lt;’), ‘&gt;’)"/>

This was probably clear as mud when I showed it before, but let’s break it apart a little.

First, we grab the text after the ‘ID=’ string:

substring-after(@Author, ‘ID=’)

clip_image008

Then the text before the ‘<’ in the remaining text:

substring-before(substring-after(@Author, ‘ID=’), ‘&lt;’)

clip_image010

And finally then the text after the ‘>’ in the remaining text:

substring-after(substring-before(substring-after(@Author, ‘ID=’), ‘&lt;’), ‘&gt;’)

clip_image012

Hopefully the pictures make it a little more obvious how we’re whittling down the big long string into what we want.

string-length

string-length is especially useful when we want to test for the existence of a value. Keep in mind that all values coming back to us in the DataSource are always coming stored as text in XML. SharePoint Designer parses apart the XML for us so that we don’t have to think about the actual representation. When a value is “empty” (in other languages you might test for a null value or an undefined value, etc.), then it has no value. We can test for the existence of the value by checking the string-length:

<xsl:choose>
  <xsl:when test="string-length(@Title &gt; 0)">
    <xsl:value-of select="@Title"/>
  </xsl:when>
  <xsl:otherwise>
    The Title is empty.
  </xsl:otherwise>
</xsl:choose>

translate

translate lets you replace one character with another in a text value. Here’s one spiffy trick you can use it for: converting a string to all upper case for reliable comparisons between strings.

<xsl:variable name="UTitle" select="translate(@Title, 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>

What this does is replace the instances of each lowercase letter with their uppercase equivalent. If you are doing this just for display, then I would encourage you to use a CSS class with text-transform:uppercase, but this trick is great when you need to work with the strings in your XSL logic.

clip_image014This is an example of nice little template to keep around in case you need it. In larger projects, I’ll usually build up a utilities.xsl file and store it in /Style Library/XSL Style Sheets. In that file, I’ll store reusable utility templates to do things like this uppercase conversion. Then I include the utility.xsl file in DVWPs wherever I need it using the <xsl:import> tag. (No, I haven’t covered that one in this series. Yet.)

Obviously, there are a few more of these string functions available to use in your XPath expressions. I just wanted to show you a few of the little tricks that you can do with them that turn out to be useful over and over again.

In the next article, I’ll go into some more depth on the ddwrt namespace.

Series Navigation<< Unlocking the Mysteries of Data View Web Part XSL Tags – Part 12 – Miscellaneous: Person or Group ColumnsUnlocking the Mysteries of Data View Web Part XSL Tags – Part 14 – Miscellaneous: ddwrt Namespace Functions >>

Similar Posts

9 Comments

  1. <tr><td valign=”top” style = “width:33%” ><table border=”0″ width=”81%” ><tr><td width=”10%” valign=”left”>

    </td></tr></table></td></tr>



    In above code snippet, i want to reassign value of variable dvt_FirstCharacter like:
    $dvt_FirstCharacter = substring(@Title,1,1).

    Is this feasibe? Please help me as I have not been able to found solution for this.

    Above code is not complete code, I can provide you complete code of template if required.

    Thanks and Regards,
    Deepak Chawla

  2. Hi,

    Post is not showing XLT code, so let me explain my query once again, I have declared variable “” and in a particlular condition, i want to reassign value to this variable like $dvt_FirstCharacter = substring(@Title,1,1).

    Please let me know if this is possible.

    Thanks,
    Deepak

  3. Thanks Marc, your posts have been extremely helpful. Using the code in your posts, I have been able build a chart that subtotals by person. I used the code to string together the person’s name without the “presence” hyperlink, and then was able to add a different hyperlink to open up a new window with the list detail for that person. I am only running into one problem. For one person, the last name is O’Reilly, and the apostrophe is represented as ‘ in the long string (example of partial string below). Any ideas how to translate that in the final string, so that the name appears as O’Reilly, John?

    ID=482″>O’Reilly, John

    Thanks again for all the great posts!

    1. Allison:

      I assume that you are doing all of this in a DVWP. How are you displaying the users’ names? Generally, adding disable-output-escaping=”yes” to the xsl:value-of ought to display the escaped characters correctly in the browser.

      M.

      1. Yes, it is in a DVWP. I used the string code you described in this article to parse out the name and display it as text so that I could then add a hyperlink to it. When I did it the other way (disable-output-escapting=”yes”) it did display the name correctly, but it was the name “with presence,” and I want to hyperlink each person’s name to go to a different link, not the SharePoint user information. Is there a way to display the name without the “presence” and add a different hyperlink to it? Thanks so much for your help!

        Allison

        1. I just realized that when I first posted, what I typed was translated to an apostrophe. It is not an apostrophe that is displaying. Instead of the apostrophe, it is an ampersand followed by pound sign, then 39;

        2. Allison:

          Sure. You’ve probably got the resulting string in a variable; let’s call it Username. So, the XSL would be:

          <a href="http://mynextpage"><xsl:value-of select="$Username" disable-output-escapting="yes"/></a>
          

          And the ampersand-pound-39 you’re seeing is the encoded value for an apostrophe. ;+)

          M.

          1. The string logic isn’t displaying the name correctly because of the apostrophe. I just realized that my original post did not display the real problem because what I typed was translated to an apostrophe. When I parse out the name, the apostrophe shows up as ampersand, pound 39;

            If I use the disable-output-escaping method, the name shows up correctly, but I can’t seem to figure out how to get my hyperlink to work instead of linking to the SP user info. Am I missing something? Thanks again!

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.