Posts
- Dependency injection in XSLT...or actually maybe not
8 May 2024
Dependency injection in XSLT…or actually maybe not
Ever written two, three or more stylesheets that are almost the same, but not quite the same, maybe you’ve struggled with #import or #include to separate your core shared code from the bespoke specific code? Maybe you’re an OO programmer steeped in the theology of objects, favouring composition over inheritance and find XSLT innate mechanisms to close to its OO cousins? Or a functional programmer at ease with higher order functions, but dabbling in some XSLT and wondering how to apply you functional skills, well read on, hopefully this blog will help you apply these skills in XSLT but ironically question whether these techniques really are the “state of the art”, many believe and when you return to your Scala/C#/Java/C++ or whatever it is you do most of the time, question whether there is more than one way to skin the cat (ew…I hate that phrase).
- Producing JSON in XSLT, a simple walkthrough.
8 May 2024
Producing JSON in XSLT, a simple walkthrough.
XSLT isnt just for XML, it can, surprisingly transform from and to JSON, but simple tutorials seem thin on the ground, what follows is a walkthough of of how to generate a simple JSON output.
- Functional XSLT/XPath/XQuery - #1 Introduction
3 June 2024
Functional XSLT/XPath/XQuery - #1 Introduction
To the modern programmer these languages appear strange beasts, born when the world was obsessed with XML, it is declarative, almost functional, but somehow missing some of the idioms of later more consciously functional languages. For a functional programmer it misses familiar function and data types that the functional programmer instinctively expect. To be fair their roots predate much of the flowering of functional patterns in language development and whilst the semantics of the languages are quite well suited to this style of development, the foundations are idiosyncratic, and now locked in by their history to be slightly out of step.
- Functional XSLT/XPath/XQuery - #2 Functor pattern
3 June 2024
Functional XSLT/XPath/XQuery - #2 Functor pattern
In the previous blog we introduced some machinery, the id function, and function composition, we talked about function types, and invented an informal notion of parametric types in order to aid our thinking.
We now move onto the functional patterns. These are a bit like OO patterns but have more formal mathematical underpinnings. Knowing what to call the things is problematic in itself, in mathematics these underpinnings are called a category, in functional circles they are often called a typeclass , this is a borrowed term from Haskell that has a particular mechanism for implementing them which may be misleading, other languages have different mechanisms, some languages have no formal mechanism, but have informal mechanisms, or no mechanism at all apart from idiomatic usage, like an OO pattern.
- Functional XSLT/XPath/XQuery - #3 Maybe data type
5 June 2024
Functional XSLT/XPath/XQuery - #3 Maybe data type
We are now in a position to fill in a missing data type.
Lets cover some XPath basics.
Consider the following XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="#all" version="3.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:kooks="http://kookerella.com"> <xsl:output method="xml" indent="yes"/> <xsl:function name="kooks:safeDivision" as="xs:numeric?"> <xsl:param name="numerator" as="xs:numeric"/> <xsl:param name="denominator" as="xs:numeric"/> <xsl:if test="not($denominator eq 0)"> <xsl:sequence select="$denominator div $denominator"/> </xsl:if> </xsl:function> <xsl:template match="/"> <output> <xsl:variable name="calculations" as="xs:numeric*" select="(1 div 1,1 div 2,2 div 3)"/> <xsl:sequence select="$calculations"/> </output> </xsl:template> </xsl:stylesheet>If we execute it against an xml it returns this:
- Functional XSLT/XPath/XQuery - #4 Monoid
9 June 2024
Functional XSLT/XPath/XQuery - #4 Monoid
Usually in these sorts of tutorials Monoid comes first, its the simplest, easiest most familiar sort of thing, we don’t really even need code to describe it, its loosely the theory of having something you can composing two things together and getting another thing of the same type, together with an identity element, so in mathematics the integers form a monoid with
+and the identity element is0or we can use multiplication*and1, the set of booleans, i.e.{ True , False }together with the operatorANDform a monoid, where the identity element isTrue, to be honest, these things are rarely used as monoids in functional code, they could be, there just isn’t much use, the things that are usually used as monoids are collections, so in the XPath world this issequence,array,map(and the recently addedmaybe). - Functional XSLT/XPath/XQuery - #5 Monad pattern
15 June 2024
Functional XSLT/XPath/XQuery - #5 Monad pattern
The monad is probably the most written about functional pattern, it is famous for convoluted and obtuse explanations, but I will steal some of the better explanations and translate them into XSLT/XPath.
(adapted directly from All About Monads - HaskellWiki).
“Suppose that we are writing a program to keep track of sheep cloning experiments. We would certainly want to know the genetic history of all of our sheep, so we would need
motherandfatherfunctions. But since these are cloned sheep, they may not always have both a mother and a father!