Creating Libraries

As in most programming environments, rather than coding all logic into a single file, XSL supports a modular programming model.  Such an XSL Library is not a Sitecore XSL rendering, but a place to store named XSL templates which function like static methods in a C# programming environment. 

An XSL Library can be created through the Sitecore UI, but since it is only referenced by other XSL code and will never be bound statically or dynamically to a layout component it is more logical to create it directly on the file system in the same directory as other XSL renderings.

Copy /sitecore/client/templates/basic.xslt to /sitecore/client/templates/library.xslt.  Most of the boilerplate code should be removed, leaving:

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
  xmlns:sc="http://www.sitecore.net/sc"  
  xm:dot="http://www.sitecore.net/dot" 
  exclude-result-prefixes="dot sc">
<xsl:output method="html" indent="no" encoding="UTF-8" />
  <xsl:template name="<MethodName>">
  </xsl:template>
</xsl:stylesheet>

Register any custom namespaces and save the file, then copy it to /xsl/functions.xslt.  Add named methods as <xsl:template> blocks inside under the root element <xsl:stylesheet>. See the sample library source for details.

1.  Notes

Important:  it is not possible to use Sitecore XSL Extension Controls such as <sc:text> in an XSL Library. Use Sitecore XSL Extension Functions instead: 

Sitecore XslControl

Substitution

<sc:date> 

sc:formatdate(string IsoDate)
sc:formatdate(string IsoDate, string Format)

<sc:dot>

dot:Render(.)

<sc:html>

sc:fld('html', .)

<sc:image>

sc:image('fieldName', .)
sc:image('fieldName', ., 'parameters')

<sc:link>

sc:GetLink(., 'parameters')

<sc:sec>

sc:HasRight('right', .)
//where right can be: 'a' - Admin, 'c' - Create, 'd' - Delete, 'n' - Rename, 'r' - Read, 'w' - Write

<sc:text>

sc:fld('fieldName, .)

<sc:enableSecurity>

sc:EnterSecurityState(bool)

<sc:disableSecurity>

sc:ExitSecurityState()

Sitecore XSL Extension Functions provide a superset of the functionality exposed by Sitecore XSL Extension Controls.

To reference the Library from other XSL renderings, add the following element under the existing <xsl:output> element:

<xsl:include href="functions.xslt" />

If an XSL Library will be referenced from most renderings, backup the existing file and add this reference to the XSL boilerplate /sitecore/client/templates/basic.xslt

Methods in the Library can be invoked to write output:

<sc:link><xsl:call-template name="GetNavTitle" /></sc:link>

Or populate an XSL variable:

<xsl:variable name="parenttitle"> 

  <xsl:call-template name="GetTitle">

    <xsl:with-param name="item" select=".." />

  </xsl:call-template>

</xsl:variable>

If the same lines of XSL code are ever needed in multiple contexts, they should almost always be factored out to an XSL Library.

2.  Sample Library Source

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl
="http://www.w3.org/1999/XSL/Transform"
  xmlns:sc
="http://www.sitecore.net/sc"
  xmlns:dot
="http://www.sitecore.net/dot"
  exclude-result-prefixes
="dot sc">
<xsl:output method="html" indent="no" encoding="UTF-8" />
<xsl:template name="GetNavTitle">
  
<xsl:param name="item" select="." />
  
<xsl:choose>
    
<xsl:when test="sc:fld( 'navtitle', $item )">
      
<xsl:value-of select="sc:fld( 'navtitle', $item )" />
    
</xsl:when>
    
<xsl:when test="$item/@template = 'link' and sc:fld( 'link', $item, 'text' )">
      
<xsl:value-of select="sc:fld( 'link', $item, 'text' )" />
    
</xsl:when>
    
<xsl:otherwise>
      
<xsl:value-of select="$item/@name" />
    
</xsl:otherwise>
  
</xsl:choose>
</xsl:template>
<xsl:template name="GetTitle">
  
<xsl:param name="item" select="." />
  
<xsl:choose>
    
<xsl:when test="sc:fld( 'title', $item )">
      
<xsl:value-of select="sc:fld( 'title', $item )" />
    
</xsl:when>
    
<xsl:otherwise>
      
<xsl:value-of select="$item/@name" />
    
</xsl:otherwise>
  
</xsl:choose>
</xsl:template>
</xsl:stylesheet>