My favorite XSL tips / tricks

I’m compiling a list of my favorite XSL tip and tricks that always help me. These items are not going to be in any particular order

1) Linking Javascript or CSS

<xsl:text disable-output-escaping="yes">
<![CDATA[ 
 <link rel="stylesheet" href="/Style Library/myCss.css"/>
 <script type="text/javascript" src="/Style Library/js/myScript.js"></script>
]]>
</xsl:text>

2) Stripping HTML 

the first section is the how to call the template below it

<!-- calling template -->
<xsl:call-template name="remove-html">

 <xsl:with-param name="text" select="@MyText">
</xsl:call-template>
<!-- template -->
<xsl:template name="remove-html">
    <xsl:param name="text"/>
    <xsl:choose>
        <xsl:when test="contains($text, '&lt;')">
            <xsl:value-of select="normalize-space(substring-before($text, '&lt;'))"/>
            <xsl:text> </xsl:text>
            <xsl:call-template name="remove-html">
                <xsl:with-param name="text" select="normalize-space(substring-after($text, '&gt;'))"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="normalize-space($text)"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

3) Trim String

<xsl:choose>
  <xsl:when test="string-length(@MyString) > 50">
    <xsl:value-of select="substring(@MyString, 1, 49)" />...
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="@MyString" />
  </xsl:otherwise>
</xsl:choose>

4) Show all XML output

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<xmp><xsl:copy-of select="*"/></xmp>
</xsl:template>
</xsl:stylesheet>

5) SharePoint Templates

A nice set of pre-built .xsl templates for use which contains some helpful things.

SharePoint XSL Templates

 

Advertisements