12 days of XSLT
“12 days of Christmas” using XSLT
Deborah Pickett on the Fediverse threw a Christmas coding challenge: to code the famous Twelve days of Christmas rhyme in any (programming) language.
She even answers it herself with XSLT, a solution that I love because it's fast, compact, and it gives visibility to the much-maligned XSLT I've already abundantly discussed here.
So obviously I've asked myself: would I do it differently if I were to use the same language? And the answer is (obviously, as otherwise I wouldn't be writing this) yes.
Two things mainly I would do differently: I would separate the list of items from the transform, and I wouldn't include the cardinal and ordinals in the list of items, but let the transform compute it automatically.
This has some clear downsides compared to @futzle's solution: it requires multiple files (compared to the single one of her solution), and the XSLT itself will be considerably more complex, since at the very least it'll have to include the logic about writing the numbers as letters, both in ordinal and cardinal form.
On the other hand, we gain something: the possibility to customize the list without touching the transformation code (separation of concerns), and potentially even support for multiple (human) languages, which I haven't implemented yet.
You can find my version of the transform here, and you can see it in action here. This is presently a rather quick & dirty implementation, and will produce the incorrect indefinite article for words starting with a vowel sound (or to be more specific, needing an instead of a), but it shows it can be done, and that the transform can be applied to different lists of presents, such as this one taken from RFC 1882 without the commentary (which, on the one hand, highlights another shortcoming of the current transform, but on the other, helps minimize the sexism of the original text since it is unspecified why the nine lady executives are a problem for the tech support person —and that might just as well be because said tech support person happens to be a sexist asshat)
The problem is, now I feel nerd sniped, since there's a number of feature I want to add support for in the XSLT.
The aforementioned multiple language support, first and foremost. A way to specify the cardinal indicator to use (which may be an vs a in English, or gendered forms in languages like Italian or French) would also be useful to allow the correct form to be used. And ideally, even, a way to indicate which part of the “present” needs to be made plural (and how), which would allow reordering the list and still get a grammatically correct output. (Evolving per-present commentary would also be possible, although at the very least I'd want to play around with the idea using a better example than the aformentioned RFC.)
But for the time, this will suffice.