Supporters of End User
Web

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

Item is currently unrated. Press SHIFT+ENTER to rate this item.1 star selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+ESCAPE to leave rating submit mode.2 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.3 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.4 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.5 stars selected. Press SHIFT+ENTER to submit. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.
Categories:MOSS; WSS; 2007; 2010; Unlocking the Mysteries of the Data View Web Part; Site Manager/Power User; SharePoint Designer; Data View Web Part; Templates

<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.

Comments

Ron

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

Hi Marc

Nice Articles i have gone through all posts in this series to solve mysteries of XSL.
Please try to write post on to how call Jquery or JavaScript function in XSL on elements like
Drop down.
I want to call Jquery function on Dropdown onload event but no luck.

Please advise me

Posted 25-Feb-2010 by Ron
dawg3294

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

I'm still new to xsl, and I'm about halfway through reading this. So far, this is the best technical article I've ever read. Explaining things with normal English. Wow, what a concept. I wish more programming books did this.

Posted 09-Jul-2010 by dawg3294
Marc Anderson

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

dawg:

Really glad that you're finding this helpful. Check out my other articles and my eBook, which ties this entire series up into one nice, reformatted and revised PDF.

M.

Posted 10-Jul-2010 by Marc Anderson
Kirkster

How can I set my sort order to a specific sequence of field values?

I have the need to sort based on a status field.  The four choices are Critical, Advisory, Verified and Defined (in order of priority).  How can I group on @Status and ensure that the groups are displayed in the priority order?

Posted 19-Apr-2011 by Kirkster

Notify me of comments to this article

E-mail:
   

Add Comment

Title:

 
Comment:
Email:

   


Name:

 
Url: