Unlocking the Mysteries of Data View Web Part XSL Tags – Part 3 – <xsl:call-template>

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

Cross-posted from EndUserSharePoint.com

<xsl:call-template>

This is how you call a template which you’ve defined with <xsl-template>.

imageIn the last installment, I showed you what templates SharePoint Designer usually creates when you set up a Data View Web Part (DVWP). Having the templates in place is great, but you need a way to chain them together to make them do anything. That’s where <xsl:call-template> comes in.

Hopefully, even if you don’t have a programming background, this can make sense. Look at the diagram to the right and think of this like a flow chart. Each of the blue squares represents an <xsl:template>, and the arrows represent how <xsl:call-template> allows you to pass control from one template to the next. (The arrows are on the right and left just to make it look better; there’s no distinction intended.)

The order in which you call the templates makes a difference, of course. You need to put on your socks before your shoes; cook dinner before you can eat it, watch TV, and go to bed, etc.

If you look at the sample XSL again, you’ll see where <xsl:call-template> is used:

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

You’ll see that each of the first three templates uses <xsl:call-template> to pass control along. The dvt_1.rowview template doesn’t use <xsl:call-template> because it’s the “end of the line”.

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

<xsl:template name="dvt_1">
  <xsl:call-template name="dvt_1.body">
    …
  </xsl:call-template>
</xsl:template>

<xsl:template name="dvt_1.body">
  <xsl:call-template name="dvt_1.rowview"/>
</xsl:template>

Note that the code section above is just the skeleton to show you how things fit together; it isn’t valid XSL.

Here’s a little bonus trick you can use to see the raw XML which is being returned from your DataSource. If you drop this XSL section in, replacing everything between the <XSL> and </XSL> tags, you’ll be able to see the raw XML as it’s coming back from your DataSource. This is handy when you’re working on displaying the results from a Search more nicely, but also anytime you just want to know exactly what’s in the result set.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ddwrt2="urn:frontpage:internal">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Depending on where you use this trick, you’ll probably need to View Source for the page to see the results. Search for <dsQueryResponse> and you’ll see the rows which are returned from the DataSource.

Next up: <xsl:with-param>. You use this with <xsl:call-template> when you want to pass a value into a template, usually a value that varies.

Series Navigation<< Unlocking the Mysteries of Data View Web Part XSL Tags – Part 2 – <xsl:template>Unlocking the Mysteries of Data View Web Part XSL Tags – Part 4 – <xsl:with-param> >>

Have a thought or opinion?