replaced XSLT stylesheet by Perl code, it is easier to read for complex transformations