Functional programming in XSLT (revised)
Dear colleagues Following is the second revision of the proposal for the features designed to enable the functional programming in XSLT. Acknowledgements ---------------- This revision is based on the recent discussion which took place on the XSLT list. Many thanks to Michael Kay, David Rosenborg and Jeni Tennison for critics and suggestions. Design principles ----------------- * respecting the spirit and processing model of the current XSLT specification * following the design patterns common for the traditional functional programming * keeping the number of extension features as small as possible 0. Namespace prefix ------------------- The namespace prefix "fxsl:" (Functional XSLt) will be used in this note for extension elements and functions related to the functional programming. 1. User-defined functions ------------------------- The syntax for the user-defined function is <fxsl:function name="qname"> <!-- parameters --> <!-- local variables and functions --> <fxsl:return ...> </fxsl:function> The <fxsl:function> instruction replaces both <xsl:define> and <xsl:lambda> instructions from the previous proposal. The syntax of <fxsl:function> is modelled after the syntax of the "define" function found in many dialects of LISP. Parameter specification is represented by the sequence of zero or more <xsl:param> elements. The usual XSLT rules are applied to these elements. Local variables are represented by the usual XSLT <xsl:variable> elements. Local functions are defined using <fxsl:function>. Local variables and functions are optional and can be mixed in any order. The single <fxsl:return> element must be the last child of <fxsl:function>. This element describes the value returned by this function. (Note, that this differs from both saxon:function and exsl:function which allow multiple return/result elements). The syntax and semantics of <fxsl:return> are the same as those of <xsl:return> from the previous proposal. There are two forms; the first is based on XPath expressions; the second is based on XSLT templates: <fxsl:return select="expression"/> <fxsl:return> <!-- template --> </fxsl:return> The first form can return value of any type; the second form always returns the result tree fragment (RTF). The <fxsl:function> declarations are allowed at any place, where <xsl:variable> declarations are allowed. In particular, function declarations can be top-level or local to a template. Furthermore, function declarations may be embedded into another function declarations. The scope rules for function declarations are the same as for variable declarations. Within the function declaration, all variables and functions which are in the lexical scope can be referenced. In particular, local variables and functions defined in the containing template or function can be referenced from the locally defined function. (This differs from the previous proposal, which allowed referencing top-level variables and variables declared in this function only). Extension functions declared with <fxsl:function> are invoked using the common XPath syntax. Arguments values are bound to the function parameters declared with <xsl:param> based on their position. Missing arguments are replaced by the appropriate default values; extra arguments cause an error. 2. Lambda objects ----------------- The extension data type - lambda object - is introduced in XPath. A lambda object represents a pair which consists of a function definition and a list of variable bindings. Lambda objects can be obtained using the XPath extension function "fxsl:lambda()". This function has a single argument which is a name of the extension function defined with <fxsl:function> and returns a lambda object which contains definition of that function and a list of bindings for all variables in scope of that definition. Lambda objects are invoked using the XPath extension function "fxsl:call()". This function has one or more arguments. The first argument is the lambda object to be invoked, other arguments provide the function parameters. Example: (implements no useful functionality, but demonstrates rules specified above). <fxsl:function name="incr:new"> <xsl:param name="x" select="0"/> <fxsl:function name="incr:local"> <xsl:param name="y" select="0"/> <fxsl:return select="$x + $y"/> </fxsl:function> <fxsl:return select="fxsl:lambda('incr:local')"/> </fxsl:function> This function returns a lambda object which, when invoked, increments its argument by the given value. The following sequence: <xsl:variable name="incr" select="incr:new(100)"/> <xsl:variable name="x" select="fxsl:call($incr, 200)"/> creates a lambda object, which adds 100 to its argument; this object is then applied to the value of 200 (the variable "x" will be assigned the value of 300 on completion of this sequence). 3. Conditional operator in XPath -------------------------------- The conditional operator is added to XPath. The proposed syntax is if TEST then CONSEQUENT else ATERNATE where "if", "then" and "else" are "reserved words", and "TEST", "CONSEQUENT" and "ALTERNATE" are XPath expressions. Example: The following extension function accepts two arguments: a node-set and a lambda object; it applies the lambda object to all nodes from this node-set and returns a new node-set containing only those nodes, for which the value returned by the lambda object is true. <fxsl:function name="ext:filter"> <xsl:param name="set"/> <xsl:param name="test"/> <fxsl:return select= "if (count($set) = 0) then $set else if fxsl:call($test, $set) then ($set ¦ ext:filter($set[position() > 1], $test)) else ext:filter($set[position() > 1])"/> </fxsl:function> 4. Total cost ------------- Following is the total cost of this proposal (in terms of added features): 1. Extension XSLT instructions: 1 2. Other XSLT extension elements: 1 3. Extension XPath functions: 2 4. Extension XPath data types: 1 5. Extensions in XPath syntax: 1 ----------------------------------- Total of added features: 6 Alexey Gokhberg Unicorn Enterprises SA XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
PURCHASE STYLUS STUDIO ONLINE TODAY!
Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!
Download The World's Best XML IDE!
Accelerate XML development with our award-winning XML IDE - Download a free trial today!
Subscribe in XML format