Umbraco/Various useful XSLT methods in Umbraco
The basics of using XSLT in Umbraco
[edit | edit source]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" />
Expressions:
Converting to string:
string($myVarName)
Converting to number:
number($myVarName)
If test:
<xsl:if test="$value=3"> </xsl:if>
Choose test:
<xsl:choose> <xsl:when test="[expression]"> </xsl:when> <xsl:when test="[expression]"> </xsl:when> <xsl:otherwise> </xsl:otherwise> </xsl:choose>
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"/> </xsl:for-each>
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: http://our.umbraco.org/wiki/reference/xslt/45-xml-schema/no-more-@nodetypealias
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 '<' !) :
<xsl:for-each select="$currentPage/descendant::node"> <xsl:if test="position() <= 5"> </xsl:if> </xsl:for-each>
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--> </xsl:template>
Calling a template:
<xsl:call-template name="myTemplate"> <xsl:with-param name="startnode" select="23"/> <xsl:with-param name="endnode" select="49"/> </xsl:call-template>
Get a parameter from a macro
<xsl:variable name="myParam" select="/macro/parameterAlias"/>