Unlocking the Mysteries of Data View Web Part XSL Tags – Part 11 – <xsl:value-of>

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

Cross-posted from EndUserSharePoint.com

<xsl:value-of>
Outputs the value to which it evaluates, whether it be the value of a column, a variable, etc.

<xsl:value-of> is sort of the “biggie” XSL tag.  It’s what you use to output values of things that are variable.  Pretty much every other tag we’ve talked about exists to get you to the point where you can use the <xsl:value-of> tag.  After all, if you didn’t want to output values from the items in your DataSource, you probably wouldn’t even bother with a Data View Web Part (DVWP), right?

Looking at the old standard example DVWP code, we see exactly one <xsl:value-of>:

<XSL><xsl:stylesheet xmlns:x="<a href="http://www.w3.org/2001/XMLSchema">http://www.w3.org/2001/XMLSchema</a>" xmlns:d="<a href="http://schemas.microsoft.com/sharepoint/dsp">http://schemas.microsoft.com/sharepoint/dsp</a>" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="<a href="http://schemas.microsoft.com/WebParts/v2/DataView/runtime">http://schemas.microsoft.com/WebParts/v2/DataView/runtime</a>" xmlns:asp="<a href="http://schemas.microsoft.com/ASPNET/20">http://schemas.microsoft.com/ASPNET/20</a>" xmlns:__designer="<a href="http://schemas.microsoft.com/WebParts/v2/DataView/designer">http://schemas.microsoft.com/WebParts/v2/DataView/designer</a>" xmlns:xsl="<a href="http://www.w3.org/1999/XSL/Transform">http://www.w3.org/1999/XSL/Transform</a>" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal">

  <xsl:output method="html" indent="no"/>
  <xsl:decimal-format NaN=""/>
  <xsl:param name="dvt_apos">'</xsl:param>
  <xsl:variable name="dvt_1_automode">0</xsl:variable>

  <xsl:template match="/">
    <xsl:call-template name="dvt_1"/>
  </xsl:template>

  <xsl:template name="dvt_1">
    <xsl:variable name="dvt_StyleName">Table</xsl:variable>
    <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row"/>
    <table border="0" width="100%" cellpadding="2" cellspacing="0">
      <tr valign="top">
        <xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
          <th width="1%" nowrap="nowrap"></th>
        </xsl:if>
        <th nowrap="nowrap">Title</th>
      </tr>
      <xsl:call-template name="dvt_1.body">
        <xsl:with-param name="Rows" select="$Rows"/>
      </xsl:call-template>
    </table>
  </xsl:template>

  <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>
  </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>
      <xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
        <td width="1%" nowrap="nowrap">
          <span ddwrt:amkeyfield="ID" ddwrt:amkeyvalue="ddwrt:EscapeDelims(string(@ID))" ddwrt:ammode="view"></span>
        </td>
      </xsl:if>
      <td>
        <xsl:value-of select="@Title"/>
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet></XSL>

Yup, there it is, all the way at the bottom of the XSL:

<xsl:value-of select="@Title"/>

All it does is display the value of the Title column from items in the list.  However, all of the XSL leading up to it is what has framed it correctly: we’ve gotten the items we want from the DataSource, we’ve rendered a TABLE to contain things, and we’ve added title column headers (THs) to the table.  In the dvt_1.rowview template, we’ve started a new table row (TR) for each item and then (ignoring the two <xsl:if>s at the moment) we output a table detail cell (TD) containing the value of the Title column.  What a lot of work for that little tiny thing, right?

Well, obviously this is a tremendously simple example.  It’s only useful to see what items are in a list and not much else.  But once you have all of this in place, you can start tweaking, either through the Common Data View Tasks dialogs or by diving into the XSL yourself.  (I’m hoping that as we are nearing the end of this series, you won’t think that diving into the XSL is such a scary thing, after all.)

<xsl:value-of> can have only two attributes:

Attribute Values Description
select XPath expression The select can take the form of any value XPath expression. Most often, you’ll just specify a column name.  Other times you may want to do calculations or complex XPath stuff. You always need to have a select attribute.
disable-output-escaping  [no | yes] This setting determines whether the text in the column is treated like regular text (“no”) or as text which contains markup (“yes”). [Optional]

As with <xsl:variable>, which we covered in an earlier article, the select attribute in the <xsl:value-of> can contain any valid XPath expression: plain text values, values of variables or columns, calculations, or very complex XPath expressions which pull data from multiple DataSources.

Here are some examples, going from truly simple to more complex:

<xsl:value-of select="’A’"/>
<xsl:value-of select="@Title"/>
<xsl:value-of select="substring-after($Title, ‘-‘)"/>
<xsl:value-of select="concat(100 * (@Sales - $Cost) / @Sales, ‘%’)"/>
<xsl:value-of select="@Author" disable-output-escaping=”yes” />

These examples will render:

  • The letter “A”
  • The contents of the Title column
  • The part of the Title column which follows a dash (“-“)
  • The gross margin, displayed as a percentage
  • The name of the person who was the author or the item (Created By).  By setting the disable-output-escaping=”yes”, we’re treating the value as markup.

Next up: Some of the miscellaneous things you will likely see in your DVWP’s XSL.  Much of what I’ll cover has come from questions I’ve gotten directly during this series, as well as from threads I’ve answered in the MSDN Forums, right here at End User SharePoint’s Stump the Panel, and elsewhere.  If you have questions about anything that I haven’t covered so far, send ‘em along and I’ll try to cover them in the miscellaneous article.

Series Navigation<< Unlocking the Mysteries of Data View Web Part XSL Tags – Part 10 – <xsl:choose>Unlocking the Mysteries of Data View Web Part XSL Tags – Part 12 – Miscellaneous: Person or Group Columns >>

Similar Posts

33 Comments

  1. Hi Marc
    Just happened to stumble across your blog. To give you my background I work for microsoft as developer.
    I was just reading through this article and there is a point which is worth mentioning which is not covered in this post which is regarding “disable-output-escaping”. This should be used sparingly and with care or else it is a security bug waiting to happen. Essentially unless you are absolutely sure what markup will be rendered by using that attribute it should not be used. It shouldnt be used to render arbitrary markup as it can lead to XSS attack.

    1. Puneet:

      You’re the first person I’ve ever heard mention this as a security issue. Given that SharePoint maintains the content in any list column, I don’t consider disable-output-escaping a risk at all. I’d be surprised if this was a Microsoft viewpoint, too. If you have documentation that backs up your point, I’d be very interested to see it.

      M.

  2. Marc,
    I havent looked at the documentation but this is an actual problem even if the data is coming from list. It totally depends what is allowed to be stored in that field. Assuming it is user html input which can have script tags it cannot be trusted.
    Typical philoshophy is to store whatever the user gives(after validating the correctness ofcourse) and the rendering code takes care of how to render it safely by calling the correct encoding methods.
    When you render that input verbatim with the disable-output-escaping attribute set to yes you are exposing an attack vector as essentially you are telling your XSL not to encode.
    Having said that there are times when there is a need to use this attribute but in such situations you have to manually ensure that what is being rendered cannot be used for XSS attack. Typical ways to ensure that is parsing the html to ensure only a small set of allowed tags is allowed. An example of this approach is used in the XSL for FederatedResultsWebpart (InternetSearchResults)in search where MakeSafe and GetPlainTextFromHtml runtime methods are used to parse the html and remove the tags which are potentially dangerous or remove all html tags respectively.
    -P

  3. Actually I have a newbie question. Within my dataview web part, I want to turn off the column title menu functionality. This is the menu that appears when you click the arrow that appears to the right of a column title when mousing over that column title. Can this option be turned off and or hidden? If so would it be possible to leave active the ability to sort by the column?

    Thanks in advance your any input.

    Regards,
    dlw

    1. David:

      If you dive into the XSL, you can change anything you want. (That isn’t to say that it’ll necessarily be straightforward.) The other option is to unbind the event(s) you don’t want with script (JavaScript or jQuery). If you still want the sorting to work, then you’ll need to only unbind the other stuff.

      Another option (which I’ve done in the past) is to just make the header simple text and add your own sorting capability. Check out my post Sorting Displays in Data View Web Parts.

      M.

      1. Marc,

        Thank you for your response.

        I took another approach with the help of our SharePoint Guru. What we did is find the FilterDisable=”” attribute within the outer div tag of the column title, in SPD 2010, and set it to ‘TRUE’. For some reason it has to be in all caps. This worked and also allowed the sorting capability to remain in place.

        dlw

  4. I should have made that clear. Sorry about that. Can you recommend any books and or articles for total newbies like myself, that will help me with DVWP and XSLT?

  5. Sounds good. Is the material in your book pertinent to SP 2010 and is it understandable by non-porgramming dummies like myself?

    1. It’s based on the series if articles we’re commenting on, so you can read them here before you buy. The XSL in 2010 isn’t significantly different.

      M.

  6. Hi Marc,

    Per my earlier solution to of setting the FilterDisable setting to TRUE, I’m finding it very hard to get this setting to ‘stick’; it keeps reverting back to “” setting. Any ideas?

    1. Honestly, I’m not sure, as I haven’t used that setting in SP2010 yet myself. What actions are you taking which cause it to revert?

      M.

  7. Hi Mark

    Question how can i past a input type hidden as a parameter in a dvwp? the input type hidden is inside the dvwp xsl tag? Please help

    Thanks
    Alec

  8. Hi Marc,
    Thanks for the series. It has helped me to understand a lot more the code in SPD 2007 and the myriad of possibilities for presentation of data. I am trying to have a SharePoint FormField show/hide based upon the values of a column in a DVWP. If the DVWP column Dept does not contain ‘AB’ then the FormField should show; otherwise hide. I am unable to get it to work using the conditional formatting tool in SPD 2007. It hides the formfield. I’ve tried the msdn forums and they not been able to crack it either. I think that your series is the way to go.

    I have a question: what doe s the [1] mean? Example below.

    not(contains(/dsQueryResponse [ 1 ] /Allocation [ 1 ] /Rows [ 1 ] /Row [ 1 ] /@Dept, ‘ABCD’))

      1. the not(contains(/dsQueryResponse [ 1 ] /Allocation [ 1 ] /Rows [ 1 ] /Row [ 1 ] /@Dept, ‘ABCD’)) comes from an aggregate datasource, which may be the issue, but I’m unsure. I do not know how to test to see if that is an issue.

      2. Are there pitfalls one should look for when working with aggregate datasources and the DVWP?

        1. No, AggregateDataSources are great; I use them all the time. It’s the XSL that SharePoint Designer generates that’s messy.

          M.

  9. I was trying to figure out the path and saw the [1] after each level. When I used the following the formfield hides even although there are values which meet the criteria. I think I am missing some syntax, which is why I am happy to have come across your series.

    not(contains(/dsQueryResponse [ 1 ] /Allocation [ 1 ] /Rows [ 1 ] /Row [ 1 ] /@Dept, ‘UT’))

    Maybe there is something else on the page which prevents it from working.

  10. How would you reference values in an entire table, instead of only a row or column? If I give my table a title, may I reference it that way?

      1. I have SP formfield that is not in xsl. It needs to either be visible or not based on the values contained in a column in a DVWP. The DVWP is in xsl, albeit in SP output.

      2. So, I tried the following, and I think it almost works. This strange test appears “click to display text if no matching items are found.” I want to show the sharepoint formfield when the @TCode contains ‘GI’. The Formfield is a people picker. I only want to allow users to choose a person if the statement is true, otherwise it should hide the people picker. What do you think?

      3. Hi,
        I have a very weird thing happening now. I’ve inserted a joined subview onto the form. I need to reference it in a conditional statement. I have the XSL appearing. No matter what now, when I click on the OK button, I get an error page that a web part is open. Have you seen this happen before?

Leave a Reply to Marc Cancel 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.