XML Connections

Monday, August 13, 2007

Formatting numbers in XQuery 1.0


[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



Labels: , , , ,

4 Comments:

  • Minolo, I took the liberty of linking this item into a page on the XQuery wikibook whihc I and Dan McCreary are working on.

    http://en.wikibooks.org/wiki/XQuery/Formatting_Numbers

    The article needs more work but I have your function running on our server and some tests. We'd be very pleased if you would like to contribute in any way.

    Chris Wallace

    By Blogger chris wallace, At November 19, 2008 6:08 AM  

  • This is a great start! It would be nice to have this built in to a future version of XQuery.

    By Blogger Dr. Data Dictionary, At November 19, 2008 8:03 AM  

  • Chris, I wrote the format-number() function in XQuery out of desperation and frustration; but I have little doubts that what needs to happen is that formatting numbers and dates must become a functionality provided at the XQuery language/engine level. I believe XQuery 1.1 is moving in that direction.

    That said, your tests do highlight two limitations in my simple format-number() implementation and one bug (even if, as Marc would say, "the specs are the specs! They are all bugs!"); I'll try to find some time to play with it and I'll post back with some improvements; I've already added support for xsl:decimal-format in a more recent "internal" version.

    By Blogger Minollo, At November 20, 2008 7:21 AM  

  • I have attached an updated version of the XQuery-based format-number function to my post here.

    By Blogger Minollo, At November 24, 2008 3:06 PM  

Post a Comment



<< Home