Recursion in XSLT

Recursion is something you will quite often encounter when working in the field of integration and working heavily with XML. There are different ways to handle recursion but a simple way is to use templates. Lets get a simple XML as seen below:

<?xml version="1.0" encoding="UTF-8"?>
<organization>
	<employees>
		<employee>
			<title>CEO</title>
			<firstname>Larry</firstname>
			<lastname>Paine</lastname>
			<employees>
				<employee>
					<title>Senior Vice President Sales</title>
					<firstname>John</firstname>
					<lastname>Thorp</lastname>
					<employees>
						<employee>
							<title>Sales Director</title>
							<firstname>Jeffrey</firstname>
							<lastname>Kindle</lastname>
						</employee>
					</employees>
				</employee>
				<employee>
					<title>Senior Vice President Development</title>
					<firstname>John</firstname>
					<lastname>Thorp</lastname>
					<employees>
						<employee>
							<title>Development Director</title>
							<firstname>Kyle</firstname>
							<lastname>Buffet</lastname>
							<employees>
								<employee>
									<title>Teamlead development</title>
									<firstname>Dave</firstname>
									<lastname>Masters</lastname>
									<employees>
										<employee>
											<title>Developer</title>
											<firstname>Ron</firstname>
											<lastname>Torsten</lastname>
										</employee>
										<employee>
											<title>Developer</title>
											<firstname>Lyle</firstname>
											<lastname>Smuck</lastname>
										</employee>
										<employee>
											<title>Developer</title>
											<firstname>Perry</firstname>
											<lastname>Ableton</lastname>
										</employee>
									</employees>
								</employee>
							</employees>
						</employee>
					</employees>
				</employee>
			</employees>
		</employee>
	</employees>
</organization>

Here you see an organization with employees. As you can see, employee is a recurring element as it has a employees element which contains a list of employee’s. In xmlspy this would look like this:

recursion

Now lets say we want to look through the whole organization and see if there is an employee with the title ‘Development Director’. We can do this by making a template with a parameter. We take the first employees element and call the template ‘CheckEmployees’. The template checks if there is a Development Director and then calls the same template recursively to check the employees which are under the current one.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
   <DevelopmentDirector>
        <xsl:call-template name="CheckEmployees">
          <xsl:with-param name="employees"
                          select="/organization/employees"/>
        </xsl:call-template>
   </DevelopmentDirector>
  </xsl:template>

  <xsl:template name="CheckEmployees">
    <xsl:param name="employees"/>
    <xsl:for-each select="$employees/employee">
      <xsl:if test="title = 'Development Director'">
        <xsl:value-of select="lastname" />
      </xsl:if>
      <xsl:call-template name="CheckEmployees">
        <xsl:with-param name="employees" select="employees"/>
      </xsl:call-template>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

As you run the XSL you would get the following outcome:

<DevelopmentDirector>Buffet<DevelopmentDirector>

This is an easy example of recursion in XSLT!

Leave a Reply

Your email address will not be published. Required fields are marked *

*