[xquery-talk] Tree-walking (was: index-of() behavior)

Michael Kay mike at saxonica.com
Thu Mar 19 19:38:18 PST 2009

>I'm using recursive typeswitching to mimic XSLT. 
The XQuery WG decided recently to add higher-order functions to the
language, and I've been wondering how this might be used to make recursive
tree-walking a less painful process.

Clearly it's easy enough to write a higher-order function that walks the
tree and applies a supplied (argument) function to each node in the tree,
which is a big step forward.

Can one go further and apply a different function to each node based on a
pattern matching (or type matching) capability?

Well, you could pass a set of functions and invoke them all, and each one
could do nothing unless the node is one that it is interested in. But that
sounds difficult to optimize.

To get close to emulating apply-templates, I think you need a function M
that maps node-names to functions T, so you pass M to your tree-walking
function W, and W when it reaches a node N calls M to obtain the function T
corresponding to the node-name of N, and then calls T. (M=mode, T=template).
I fear the result would be rather impenetrable to all but Haskell addicts.
But perhaps it could be dressed in some syntactic sugar...

Michael Kay

More information about the talk mailing list