XML/XSL Recursion

In the past 2 weeks, I dove head first into the world of XML with XSL, and now I’ve found myself floating to the top. The fundamental reason for using these two together is to allow the retrieval of data from a data source, output it as XML, then apply the look and feel to it using XSLT and CSS. By keeping the data separate from the design, we are able to adjust the look and feel without bothering with the backend programming or data. When your company requires a Change Management process to publish code, this will be your best friend.

One issue I came across that others had also come across was the confusion of what XSL calls variables. Many programmers such as myself find ourselves upset when we cannot reassign values to these variables, but XSL intended for these variables to behave in the same way that variables do in a math equation. They don’t reassign. They get substituted as the problem is executed. While this seems easy enough to grasp, I still wasn’t happy with the fact that no website I visited offered the solution to what I needed accomplish. All they did was argue why XSL variables made sense or not.

In a loop, I was wanting to trigger a flag (variable) that would tell the program to do something different after that flag was raised. This is easy enough to do in most programming languages, but XSL is special in that way. Though not published anywhere easily found, the way around this is using recursion and passing your values through parameters rather than variables. In other words, you need call the template with a initial value for your flag, then call the template again from within the template with a new set of parameters, which reflect the change you wanted to make (to your flag). I have a sample XSLT here. You see near the end of the days template, I call the template again with new parameter values. I also removed the for-each because with the recursion it is not required for my project. Just make sure you program your recursion to end at a certain point, otherwise all of you programmer friends will laugh at you as your XSL gets caught in an infinite loop. I’ve done that enough that this time, I avoided it. I hope this helps others looking for the answer to how to reassign values when XSL variables won’t take them.

Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Comment: