1,804 articles and 14,829 comments as of Wednesday, May 11th, 2011

EndUserSharePoint has combined resources with NothingButSharePoint.com. You can now find End User (Mark Miller), Developer (Jeremy Thake) and IT Pro SharePoint content all in one place!

This site is a historical archive and is no longer being updated. Please update your favorites, bookmarks and RSS feeds.

NothingButSharePoint.com
Thursday, February 4, 2010

Unlocking the Mysteries of Data View Web Part XSL Tags – Part 5: xsl:param

Author: Marc D. Anderson
http://mdasblog.wordpress.com

<xsl:param>

A value you’ve passed into a template with <xsl:with-param>. You need to have an <xsl:param> at the top of the template for each value you expect to be passed into it.

I talked in the last post about passing values into templates using <xsl:with-param>. Whenever you pass these values, you need to tell the “receiving” template to expect them. You do this by using the <xsl:param> tag.

If we look at the example XSL again, we can see that there are <xsl:param> tags used in two places:

  • At the top of the XSL, prior to any templates
  • At the top of the dvt_1.body template
<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>

At the top of the XSL, we see one use of <xsl:param>.  Anything which we declare at this level (outside any individual  templates) is available, or scoped, to the entire XSL section.

<xsl:param name="dvt_apos">'</xsl:param>

SharePoint Designer puts this silly little line of code in DVWPs to set up a parameter to represent the apostrophe character. Because of the highly reserved nature of this character, it can be a bear to work with, and this way you have a parameter containing it to use.

We need an <xsl:param> here for any values we’re declaring in the ParameterBindings section which we want to use in the XSL, too. So, for instance, if we declare the URL Server Variable in the ParameterBindings section: <ParameterBinding Name="URL" Location="ServerVariable(URL)" DefaultValue=""/>, then we need an <xsl:param> at the top of the XSL if we want to use the value: <xsl:param name="URL">.

In the dvt_1.body template, we can see:

<xsl:param name="Rows"/>

When the dvt_1.body template is called, Rows is being passed with <xsl:with-param>:

<xsl:call-template name="dvt_1.body">
  <xsl:with-param name="Rows" select="$Rows"/>
</xsl:call-template>

We need the <xsl:param> tag to “catch” the value that is being thrown in. This “catch” idea is required for anything that we pass into a template. Any parameter which we declare inside a template is available, or scoped, only within that template.

When you want to use a parameter which you’ve “caught”, you precede the name with a dollar sign. (The same is true for <xsl:variable>, but that’s the next installment.) So in the Rows example above, you’d use the parameter as $Rows (you’ll see this parameter a lot in the XSL). In the URL example, it’d be $URL.

Next up: <xsl:variable>

A value you create for use within a template which is only defined within the scope of that template.

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

2 Responses to “Unlocking the Mysteries of Data View Web Part XSL Tags – Part 5: xsl:param”
  1. Matt B. says:

    This is one of my favorite series on EUSP. It just makes sense after reading every part. Thanks for sharing!

Trackbacks

Check out what others are saying about this post...
  1. [...] Unlocking the Mysteries of Data View Web Part XSL Tags – Part 5: xsl:param [...]




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!