Recursive XSL Templates

Recursion in XSL

XSL is a declarative programming language. Variables, once declared, cannot be reassigned. It is often difficult for programmers coming from a procedural languages background to do advanced tasks. By using XSL, you can solve complex problems that at first glance often seem to be difficult or impossible. In this brief, I will demonstrate the recursive nature of templates. In a typical case of a product catalog display, the requirement is to display the product details in each cell, and number of columns should be selectable by the user. A sample XML file is listed below:

Assume that you get the above from a business component. Each element row corresponds to a product whose attributes are comprised of product-specific data. Each product, along with its details, will be rendered in a single cell. The number of columns should be definable at runtime. The following is the brief XSL that does the rendering:

As a C or C++ programmer organizes and reuses the code by using functions or object methods, in XSL you can organize code by using templates. The code above is the root template that will be invoked by the XSLT processor. You declare a parameter by using xsl:param, whose name is numCols. The user can pass this parameter; if the user does not supply any value in this parameter, by default it will have the value of 3. This variable will specify the number of columns to be rendered.

You call the renderColumns template, to which you pass three parameters. In listrows, you select all product elements. startindex signifies the starting index, and numofCols will control the number of rows to be rendered.

In XSL template renderColumns, you select elements whose position will be greater than or equal to the starting index and whose position is less than or equal to the sum of startindex and numofcols. After rendering this subset of elements, you recursively call renderColumns by selecting a subset of elements whose position is greater than the sum of startindex and numofCols, which are already rendered. To exit this recursive loop, you have a test condition at the start; it checks for the count of elements in the listrows variable. Because you are selecting only those elements that are yet to be rendered while calling recursively, sa et of nodes by each call will be reduced by the number of elements rendered. To rendering rows, in this call template you using the following template:

Conclusion

Even though variables are constant throughout the lifetime of a variable in XSL, you can achieve things that are at first glance looks not possible due to the declarative nature of XSL. Upon a close look and thinking in a declarative manner, you can solve the problem.

As a C or C++ programmer organizes and reuses the code using by functions or object methods, in XSL you can organize code by using templates. The code above is the root template that will be invoked by the XSLT processor. You declare a parameter by using the xsl:param, whose name is numCols. The user can pass this parameter. If the user does not supply any value in this parameter, by default it will have the value of 3. This variable will specify the number of columns to be rendered.

Top White Papers and Webcasts

When individual departments procure cloud service for their own use, they usually don't consider the hazardous organization-wide implications. Read this paper to learn best practices for setting up an internal, IT-based cloud brokerage function that service the entire organization. Find out how this approach enables you to retain top-down visibility and control of network security and manage the impact of cloud traffic on your WAN.

U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there is simply not enough quality talent to go around. Tiempo Development is a nearshore software development company. Our headquarters are in AZ, but we are a pioneer and leader in outsourcing to Mexico, based on our three software development centers there. We have a proven process and we are experts at providing our customers with powerful solutions. We transform ideas into reality.