<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:svg="http://www.w3.org/2000/svg" version="1.0">
    <xsl:template match="invitation_letter">
        <fo:root>
            <fo:layout-master-set>
                <fo:simple-page-master master-name="order" page-width="210mm" page-height="297mm">
                    <fo:region-body margin="25mm"/>
                    <fo:region-before extent="20mm"/>
                    <fo:region-after extent="0mm"/>
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="order">
                <fo:flow flow-name="xsl-region-body">
                    <fo:block font-size="11pt" font-family="NewtonAm">
                        <fo:table>
                            <fo:table-column column-width="100%"/>
                            <fo:table-body>
                                <fo:table-row height="30mm">
                                    <fo:table-cell>
                                        <fo:block>
                                            <xsl:apply-templates select="hat"/>
                                        </fo:block>
                                    </fo:table-cell>
                                </fo:table-row>
                                <fo:table-row border-top="1mm solid green" border-bottom="2mm solid green" height="{297-30-25-25}mm">
                                    <fo:table-cell>
                                        <fo:block margin-top="5mm">
                                            <xsl:apply-templates select="header"/>
                                            <fo:block space-after="20mm"/>
                                            <xsl:apply-templates select="body"/>
                                            <fo:block space-after="20mm"/>
                                            <xsl:apply-templates select="footer"/>
                                        </fo:block>
                                    </fo:table-cell>
                                </fo:table-row>
                            </fo:table-body>
                        </fo:table>
                    </fo:block>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>
    <!-- ====================================== -->
    <xsl:template match="hat">
        <fo:table>
            <fo:table-column column-width="50%"/>
            <fo:table-column column-width="50%"/>
            <fo:table-body>
                <fo:table-row>
                    <fo:table-cell>
                        <fo:block>
                            <fo:block font-style="italic" text-align="center">
                                <xsl:apply-templates select="comment"/>
                            </fo:block>
                            <fo:external-graphic content-height="20mm" src="url('{logo}')"/>
                        </fo:block>
                    </fo:table-cell>
                    <fo:table-cell text-align="right">
                        <fo:block>
                            <fo:block>
                                <xsl:apply-templates select="country"/>
                            </fo:block>
                            <fo:block>
                                <xsl:apply-templates select="address"/>Phone: <xsl:apply-templates select="phone"/>
                            </fo:block>
                            <fo:block>
                                Tax code <xsl:apply-templates select="tax_number"/>
                            </fo:block>
                            <fo:block>
                                <xsl:apply-templates select="site"/>
                            </fo:block>
                            <fo:block>
                                E-mail: <xsl:apply-templates select="e-mail"/>
                            </fo:block>
                        </fo:block>
                    </fo:table-cell>
                </fo:table-row>
            </fo:table-body>
        </fo:table>
    </xsl:template>
    <!-- ====================================== -->
    <xsl:template match="header">
        <xsl:apply-templates select="date"/>
        <fo:block space-after="10mm"/>
        <xsl:apply-templates select="person"/>
        <fo:block space-after="20mm"/>
    </xsl:template>
    <!-- ====================================== -->
    <xsl:template match="date">
        <fo:block>
            <xsl:apply-templates select="month"/>&#xA0;<xsl:apply-templates
                select="day"/>,&#xA0;<xsl:apply-templates select="year"/>
        </fo:block>
    </xsl:template>
    <!-- ====================================== -->
    <xsl:template match="link">
        <fo:basic-link external-destination="url({../link})">
            <fo:inline color="blue" text-decoration="underline" font-weight="bold">
                <xsl:value-of select="../link"/>
            </fo:inline>
        </fo:basic-link>
    </xsl:template>
    <!-- ====================================== -->
    <xsl:template match="body">
        <xsl:apply-templates select="start"/>
        <fo:block space-after="10mm"/>
        <xsl:apply-templates select="text"/>
        <fo:block space-after="10mm"/>
        <xsl:apply-templates select="end"/>
        <fo:block space-after="20mm"/>
    </xsl:template>
    <!-- ====================================== -->
    <xsl:template match="start">
        <fo:block>
            <xsl:apply-templates select="polite"/>&#xA0;<xsl:apply-templates
                select="abbrev"/>&#xA0;<xsl:apply-templates
            select="//person[@role = 'recipient']/surname"/>: </fo:block>
    </xsl:template>
    <!-- ====================================== -->
    <xsl:template match="text | end | position | company | person/address">
        <fo:block>
            <xsl:apply-templates/>
        </fo:block>
    </xsl:template>
    <!-- ====================================== -->
    <xsl:template match="footer">
        <fo:block>
            <xsl:apply-templates select="person[@role = 'sender']"/>
        </fo:block>
    </xsl:template>
    <!-- ====================================== -->
    <xsl:template match="person">
        <fo:block>
            <fo:block>
                <xsl:apply-templates select="name"/>&#xA0;<xsl:apply-templates select="surname"/>
            </fo:block>
            <xsl:apply-templates select="*[not(local-name() = 'name' or local-name() = 'surname')]"/>
        </fo:block>
    </xsl:template>
    <!-- ====================================== -->
    <xsl:template match="p">
        <fo:block space-after="7mm">
            <xsl:apply-templates/>
        </fo:block>
    </xsl:template>
</xsl:stylesheet>

