Hi I am a complete beginner with xslt and have just started using it as I am building a site with Umbraco. I was wondering if there was any way of creating functions within xslt so I didn't have to repeat the same thing over. I have had a look at a few sites but don't really understand it
My code is:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> ]>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
xmlns:umbraco.library="urn:umbraco.library"
exclude-result-prefixes="msxml umbraco.library">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:param name="currentPage"/>
<xsl:template match="/">
<xsl:variable name="items" select="$currentPage/ancestor-or-self::* [@isDoc and @level = 1]/* [@isDoc and string(umbracoNaviHide) != '1']"/>
<!-- The fun starts here -->
<xsl:if test="count($items) > 0">
<ul id="SubNav" class="level{@level}">
<xsl:for-each select="$items">
<li>
<a href="{umbraco.library:NiceUrl(@id)}">
<xsl:value-of select="@nodeName"/>
</a>
<xsl:if test="count(./child::*[@isDoc and string(umbracoNaviHide) != '1']) > 0">
<ul class="level{@level}">
<xsl:for-each select="./child::*[@isDoc and string(umbracoNaviHide) != '1']">
<li>
<a href="{umbraco.library:NiceUrl(@id)}">
<xsl:value-of select="@nodeName"/>
</a>
<xsl:if test="count(./child::*[@isDoc and string(umbracoNaviHide) != '1']) > 0">
<ul class="level{@level}">
<xsl:for-each select="./child::*[@isDoc and string(umbracoNaviHide) != '1']">
<li>
<a href="{umbraco.library:NiceUrl(@id)}">
<xsl:value-of select="@nodeName"/>
</a>
</li>
</xsl:for-each>
</ul>
</xsl:if>
</li>
</xsl:for-each>
</ul>
</xsl:if>
</li>
</xsl:for-each>
</ul>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
As you can see foreach node I am reusing the same code to list the children so I was wondering if I could pull this out into a function so I wouldn't have to nest the same code for each level of children I need
The idea in XSLT is to work with templates that match certain nodes in your XML structure. You can even apply the same exact template with different processing instructions using "modes".
I do not know your XML structure but I assume that would be the way to go. Searching in stackoverflow provides information too: https://stackoverflow.com/questions/tagged/templates+xslt