This would be possible in XSLT, if only browsers would implement the latest spec rather than abandoning it all together.