Formatting numbers in XQuery 1.0

August 13, 2007 Data & AI

[See here for an update about this topic]
I often need to use XQuery to create XHTML or even XSL-FO; which is apparently a fairly unusual usecase for XQuery. It is true that most people dealing with the transformation of XML into HTML or XSL-FO have been doing that with XSLT in the past; and I guess most people working on XQuery specs assumed that this would still be the case even after XQuery became a standard.

Or at least that's the only reason I can find for some obvious things that seem to be missing in XQuery 1.0, one of which is the availability of a format-number() function, which is a quite basic function for creating a string representation of a number.

Why do I need to use XQuery rather than XSLT? Because the XHTML/XSL-FO reports I need to create are aggregating data that is available in part as XML documents, and in part inside a relational database; and at least some XQuery implementations make that task very easy to achieve in a highly scalable way.

Of course in most XQuery implementations like DataDirect XQuery or Saxon you can create Java extension functions to implement a format-number() function; but I wanted to find a way to achieve at least some of the format-number() functionality without having to rely on a Java function; that's why I ended up writing the attached piece of XQuery.

I haven't spent much time cleaning it up, adding documentation or making it part of a nicely structured library module; if anyone is willing to do that (or to make it become part of FunctX), please feel free to. And also, if you have any comments or suggestions about how to improve it, feel free to post them here.

formatnumber-xquery.xq

digg_skin = 'compact';

Minollo