If you’re familiar with XSL, you’re probably thinking: “There ARE no arrays in XSL!” Yes, it’s a travesty. But last week at work, while programming in XSL, I desperately needed to loop through an array. Through Googling, I was able to arrive at some alternative functionality that accomplished what I needed.
I was making a template that would take a string of text and return a div in which every other word was a different color. The website I was making a template for had implemented this style by manually wrapping every other word in tags. (Behind-the-scenes CSS took care of the rest.)
I would split the string into an array of words, loop through the array, and wrap every other word in tags. Then I would put the array back together as a string and return it. Easy!
XSL offered limited possibilities in comparison. Instead of arrays, it had something called sequences which consisted of existing nodes (rather than copies of the node, importantly). But discovering the tokenize function was a game-changer: I could input a string and a separator (such as a space), and the function would return a sequence of every word in the string (since I had split the string by spaces).
Best of all, the sequence was not a copy of the nodes–I could modify the very nodes themselves with a for-each loop on the tokenized sequence!
Behold the final code: