Umbraco/Various useful XSLT methods in Umbraco

From Wikibooks, open books for an open world
Jump to navigation Jump to search

The basics of using XSLT in Umbraco[edit]

Creating a variable:

<xsl:variable name="myVarName" select="[select-expression/value]"/>

Variables a after this referenced using $myVarName (prefix with $)

Outputting a value:

<xsl:value-of select="$myVarName" />


Converting to string:


Converting to number:


If test:

<xsl:if test="$value=3">

Choose test:

	<xsl:when test="[expression]">
	<xsl:when test="[expression]">

Loop all nodes from root and down of a certain doc-type (this example includes sorting on a field in this doc-type):

<xsl:for-each select="$currentPage/ancestor-or-self::root//node [@nodeTypeAlias='myDocType']">
   <xsl:sort select="data [@alias = 'myAttributeAlias']" order="descending"/>

Get a node name from the first parent node of a certain doc-type:

<xsl:variable name="myVarName" select="ancestor-or-self::node [@nodeTypeAlias = 'myDocType']/@nodeName"/>

(Since the Umbraco 4.5 Schema, @nodeTypeAlias is no longer used) - for versions 4.5 and above, use:

<xsl:variable name="myVarName" select="$currentPage/ancestor-or-self::doc-type-alias-goes-here/@nodeName"></xsl:variable>

Details of the change from using @nodeTypeAlias are here:

Get an attribute value from the first parent node that has a certain doc-type:

<xsl:variable name="myVarName" select="ancestor-or-self::node [@nodeTypeAlias = 'myDocType']/data [@alias = 'myAttributeAlias']"/>

Limit a for-each loop to run only five times (if position <= 5) (REMEMBER in XSLT the '<' sign must be written as '&lt;' !) :

<xsl:for-each select="$currentPage/descendant::node">
<xsl:if test="position() &lt;= 5">

Loops first five descendents of current node.

Get attribute value from a current node in a loop:

<xsl:value-of select="data [@alias = 'myAttributeAlias']"/>

Creating a template with two parameters:

<xsl:template name="myTemplate">
	<xsl:param name="startnode"/>
	<xsl:param name="endnode"/>
	<!-- do something-->

Calling a template:

<xsl:call-template name="myTemplate">
	 <xsl:with-param name="startnode" select="23"/>
	 <xsl:with-param name="endnode" select="49"/>

Get a parameter from a macro

<xsl:variable name="myParam" select="/macro/parameterAlias"/>