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:



	
		
			CEO
			Larry
			Paine
			
				
					Senior Vice President Sales
					John
					Thorp
					
						
							Sales Director
							Jeffrey
							Kindle
						
					
				
				
					Senior Vice President Development
					John
					Thorp
					
						
							Development Director
							Kyle
							Buffet
							
								
									Teamlead development
									Dave
									Masters
									
										
											Developer
											Ron
											Torsten
										
										
											Developer
											Lyle
											Smuck
										
										
											Developer
											Perry
											Ableton
										
									
								
							
						
					
				
			
		
	

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.




   
        
          
        
   
  

  
    
    
      
        
      
      
        
      
    
  


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

Buffet

This is an easy example of recursion in XSLT!

Leave a Reply

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

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.