Recursion

XSLT doesn't really do things like changing values of variables or for next loops. What XSLT does is recursion where a template calls it self until an exit condition is satisfied.

Examples

Splitting up a comma separated variable (CSV)

Here are some addresses where each line of the address is separated with a comma.

Here's an XSLT stylesheet that's got a recursive template called splitAddress.

This recursive template has a parameter called addressToSplit, which is the comma separated value for an address.

What this recursive template does is

  • if the address to split string has any commas then
    • create a line element that has text, which is the substring before the first comma
    • recursive call to this template itself again with a new address to split, which is the substring after the first comma
  • otherwise
    • this is the last comma separated value, which is the postcode here, so create a postcode element that has this text
    • this is the exit condition for this recursive template so stop here
    creates an element that has the substring before the first comma of an address and then calls itself again with the substring after the first comma.

And here's the output XML