I just finished rather tedious bit of work turning my XML to HTML XSLT routine to XML to RTF one. Why? Well I think I made bit of a miscalculation on my part when I thought that HTML files would be good enough for printing invoices. Although they're ok when there's like one invoice to print but printing many is not going to work so easily, for one pagination is rather difficult as are font-sizes and every other crap. So I have this piece of software that outputs invoices as XML and I really didn't want to start tinkering the code, which by the way is done on C#, so the support for XSLT 2.0 was pretty much ruled out. Hate C# but for usability's sake I handed my pinky for the devil.
I wanted to create on A4 per tenant containing years worth of payments, naturally If I'd been able to use XSLT 2.0 this would have been done like 4 hours ago, seeming that XML produced was like:
<invoices>
<invoice>
</invoice>
<tenant>
<amountToPay>
<etc..>
</invoice>
So I'd just used the group-by function to to group same tenant's invoices together and formed the one A4 invoice that way, but no I just had to knife it out, the code I mean. So I refactored the XML to be produced like:
<invoice>
<invoice>
<tenant>
<totalAmountToPay>
<monthlyPayments>
<amount>
<dueDate>
</monthlyPayments>
<monthlyPayments>
<amount>
<dueDate>
</monthlyPayments>
<monthlyPayments>
<amount>
<dueDate>
</monthlyPayments>
</invoice>
</invoices>
So problem solved, Although the XML to RTF conversion proved to be somewhat difficult, I had to look out for how RTF documents are built and then create a XSLT stylesheet to accommodate that. so it ended up looking like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/1999/XSL/Transform lasku_11-3-2008.xsd">
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/laskut">
<xsl:text>{\rtf1\UTF-8</xsl:text>
<xsl:text>{\fonttbl{\f0\fnil Times New Roman;}{\f1\fnil Arial;}{\f2\fnil Symbol;}{\f3\fnil Wingdings;}}</xsl:text>
<xsl:for-each select="lasku" xml:space="default">
<xsl:text>{\pard \b Vastikelasku \par}</xsl:text>
<xsl:text>{\pard AS. OY Koukkukallio \line Tiliyhteys: 207438-1654 \par} \line </xsl:text>
<xsl:text>{\pard \b Maksaja: \par}</xsl:text>
<xsl:text>{\pard </xsl:text><xsl:value-of select="nimi1"/><xsl:text> \par}</xsl:text>
<xsl:text>{\pard </xsl:text><xsl:value-of select="nimi2"/><xsl:text> \par}</xsl:text>
<xsl:text>{\pard </xsl:text><xsl:value-of select="osoite"/><xsl:text> \par}</xsl:text>
<xsl:text>{\pard </xsl:text><xsl:value-of select="postinro"/><xsl:text> \par}</xsl:text>
<xsl:text>{\pard </xsl:text><xsl:value-of select="kaupunki"/><xsl:text> \par} \line</xsl:text>
<xsl:text>{\pard \b Erittely: \par}</xsl:text>
<xsl:if test="vastike != '0,00'">
<xsl:text>{\pard \qj Vastikemaksu: </xsl:text><xsl:value-of select="vastike"/><xsl:text> EUR \par}</xsl:text>
</xsl:if>
<xsl:if test="rahoitusvastike != '0,00'">
<xsl:text>{\pard \qj Rahoitusvastike: </xsl:text><xsl:value-of select="rahoitusvastike"/><xsl:text> EUR \par}</xsl:text>
</xsl:if>
<xsl:if test="saunavastike != '0,00'">
<xsl:text>{\pard \qj Saunavastike: </xsl:text><xsl:value-of select="saunavastike"/><xsl:text> EUR \par}</xsl:text>
</xsl:if>
<xsl:if test="autotallivastike != '0,00'">
<xsl:text>{\pard \qj Autotallivastike: </xsl:text><xsl:value-of select="autotallivastike"/><xsl:text> EUR \par}</xsl:text>
</xsl:if>
<xsl:if test="vesimaksu != '0,00'">
<xsl:text>{\pard \qj Vesimaksu: </xsl:text><xsl:value-of select="vesimaksu"/><xsl:text> EUR \par}</xsl:text>
</xsl:if>
<xsl:if test="saunamaksu != '0,00'">
<xsl:text>{\pard \qj Saunamaksu: </xsl:text><xsl:value-of select="saunamaksu"/><xsl:text> EUR \par}</xsl:text>
</xsl:if>
<xsl:if test="autopaikka != '0,00'">
<xsl:text>{\pard \qj Autopaikkamaksu: </xsl:text><xsl:value-of select="autopaikka"/><xsl:text> EUR \par}</xsl:text>
</xsl:if>
<xsl:text> \line</xsl:text>
<xsl:text>{\pard \b Yhteensä: \tab </xsl:text><xsl:value-of select="maksettava"/><xsl:text> EUR /KK \par}\line</xsl:text>
<xsl:text>{\pard \i Alla seuraavan laskutuskauden maksettavat vastikemaksut, laita rasti ruutuun maksun merkiksi niin voit seurata maksujasi.\par} \line</xsl:text>
<xsl:for-each select="erapaivat" xml:space="default">
<xsl:text disable-output-escaping="yes">{\pard Eräpäivä: </xsl:text><xsl:value-of select="erapaiva"/><xsl:text> Viitenumero: </xsl:text><xsl:value-of select="viite"/><xsl:text> Määrä: </xsl:text><xsl:value-of select="maksettava"/><xsl:text> EUR</xsl:text><xsl:text> Maksettu [ ] \par}</xsl:text>
</xsl:for-each>
<xsl:text> \page </xsl:text>
</xsl:for-each>
<xsl:text>}</xsl:text>
</xsl:template>
</xsl:stylesheet>
Not so beautiful or efficient but it get's the thing done and that's all this is about.
Author: Anteuz
Filed under:
Updated by:Anteuz / 09-04-2008)
