<?xml version='1.0' encoding='ISO-8859-1' ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:rx="http://www.renderx.com/XSL/Extensions" version="1.0">
    <!-- ********* params ******************* -->
    <xsl:param name="space-between-options" select="'&#xA0;&#xA0;&#xA0;'"/>
    <!-- ********* variables***************** -->
    <xsl:variable name="field-name-font-size" select="'8pt'"/>
    <xsl:variable name="option-font-size" select="'8pt'"/>
    <xsl:variable name="small-font-size" select="'7pt'"/>
    <!-- ************************************ -->
    <!-- ********* templates **************** -->

    <!-- ************************************ -->
    <!-- ########### HEAD ################### -->
    <xsl:template match="visa">
        <fo:root>
            <fo:layout-master-set>
                <fo:simple-page-master master-name="MainPage">
                    <fo:region-body margin="10mm"/>
                    <fo:region-before extent="0mm"/>
                    <fo:region-after extent="10mm"/>

                </fo:simple-page-master>
                <fo:page-sequence-master master-name="Repeat-MainPages">
                    <fo:repeatable-page-master-reference master-reference="MainPage"/>
                </fo:page-sequence-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="Repeat-MainPages">
                <!-- page numbers -->
                <fo:static-content flow-name="xsl-region-before">
                    <fo:block text-align="center" font-size="10pt" padding-top="3mm">

                        This document has no legal effect and cannot be basis for a visa application in any accredited Embassy or Consulate
                    </fo:block>
                </fo:static-content>
                <fo:static-content flow-name="xsl-region-after">
                    <fo:block text-align="center" font-size="10pt">&#x2014; <fo:page-number/> &#x2014;</fo:block>
                </fo:static-content>
                <fo:flow flow-name="xsl-region-body">
                    <fo:block font-size="12pt" background-image="url('bg.jpg')">

                        <xsl:apply-templates/>
                    </fo:block>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>
    <!-- ================================== -->
    <xsl:template match="head">
        <fo:table>

            <fo:table-column column-width="30mm"/>
            <fo:table-column column-width="115mm"/>
            <fo:table-column column-width="30mm"/>
            <fo:table-body>
                <fo:table-row height="70mm">
                    <fo:table-cell padding-top="30mm">
                        <xsl:apply-templates select="image-placeholders/photo-placeholder"/>
                    </fo:table-cell>
                    <fo:table-cell text-align="center" padding="5mm">

                        <fo:block>
                            <xsl:apply-templates select="logos"/>
                        </fo:block>
                        <xsl:apply-templates select="title"/>
                    </fo:table-cell>
                    <fo:table-cell padding-top="10mm">
                        <xsl:apply-templates select="image-placeholders/stamp-placeholder"/>
                        <fo:block>
                            <xsl:apply-templates select="certificate"/>

                        </fo:block>
                    </fo:table-cell>
                </fo:table-row>
            </fo:table-body>
        </fo:table>
    </xsl:template>
    <!-- ================================== -->
    <xsl:template match="photo-placeholder">
        <fo:block border="1pt solid black" padding-top="10mm" padding-bottom="25mm" text-align="center">

            <xsl:value-of select="@alt"/>
        </fo:block>
    </xsl:template>
    <!-- ================================== -->
    <xsl:template match="logos">
        <xsl:for-each select="img">
            <fo:external-graphic src="url('{@src}')" content-height="20mm"/>
        </xsl:for-each>
    </xsl:template>

    <!-- ================================== -->
    <xsl:template match="title">
        <fo:block font-size="24pt">
            <xsl:apply-templates select="text"/>
        </fo:block>
        <fo:block font-size="14pt">
            <xsl:apply-templates select="comments"/>
        </fo:block>
    </xsl:template>

    <!-- ================================== -->
    <xsl:template match="certificate">
        <fo:block font-size="{$field-name-font-size}" border="1pt solid black" padding-top="5mm"
            padding-bottom="5mm" text-align="center" padding-left="3mm" padding-right="3mm">
            <xsl:value-of select="../image-placeholders/stamp-placeholder/@alt"/>
        </fo:block>
        <fo:block space-before="20mm">
            <fo:block>
                <fo:external-graphic src="url('{img/@src}')" content-height="10mm"/>
            </fo:block>

            <fo:block>
                <xsl:value-of select="concat('No ', number)"/>
            </fo:block>
        </fo:block>
    </xsl:template>
    <!-- ########### BODY ################### -->
    <xsl:template match="body">
        <fo:block>
            <fo:table>

                <fo:table-column column-width="80%"/>
                <fo:table-column column-width="20%"/>
                <fo:table-body>
                    <fo:table-row>
                        <fo:table-cell>
                            <xsl:apply-templates select="left"/>
                        </fo:table-cell>
                        <fo:table-cell border="1pt solid black" padding="2pt" border-after-width.conditionality="retain" border-before-width.conditionality="retain">
                            <xsl:apply-templates select="right"/>

                        </fo:table-cell>
                    </fo:table-row>
                </fo:table-body>
            </fo:table>
            <fo:block space-before="2pt" font-size="{$field-name-font-size}" font-weight="bold"> *
                    <xsl:apply-templates select="//footnote"/>
            </fo:block>
        </fo:block>
    </xsl:template>

    <!-- ================================== -->
    <xsl:template match="left">
        <fo:block>
            <xsl:apply-templates/>
        </fo:block>
    </xsl:template>
    <!-- ================================== -->
    <xsl:template match="right">
        <fo:block>

            <xsl:apply-templates/>
        </fo:block>
    </xsl:template>
    <!-- ================================== -->
    <xsl:template match="notes/title">
        <fo:block space-after="2mm" font-size="9pt" text-align="center">
            <xsl:apply-templates/>
        </fo:block>
    </xsl:template>

    <!-- ================================== -->
    <xsl:template match="note">
        <fo:table>
            <fo:table-column column-width="100%"/>
            <fo:table-body>
                <fo:table-row height="{@height}mm">
                    <fo:table-cell>
                        <fo:block>
                            <xsl:apply-templates/>

                        </fo:block>
                    </fo:table-cell>
                </fo:table-row>
            </fo:table-body>
        </fo:table>
    </xsl:template>
    <!-- ================================== -->
    <xsl:template match="choice">
        <fo:block space-after="1pt" font-size="{$field-name-font-size}">

            <xsl:apply-templates/>
        </fo:block>
    </xsl:template>
    <!-- ================================== -->
    <xsl:template match="table">
        <fo:block font-size="{$field-name-font-size}">
            <fo:table>
                <fo:table-column column-width="3mm"/>
                <xsl:for-each select="columns/*">

                    <fo:table-column column-width="{@width}"/>
                </xsl:for-each>
                <fo:table-body>
                    <fo:table-row>
                        <fo:table-cell><fo:block/></fo:table-cell>
                        <xsl:for-each select="columns/*">
                            <fo:table-cell>
                                <fo:block>
                                    <xsl:value-of select="@header"/>

                                </fo:block>
                            </fo:table-cell>
                        </xsl:for-each>
                    </fo:table-row>
                    <xsl:call-template name="put-rows">
                        <xsl:with-param name="columns" select="count(columns/*)"/>
                        <xsl:with-param name="qty" select="@rows"/>
                    </xsl:call-template>
                </fo:table-body>

            </fo:table>
        </fo:block>
    </xsl:template>
    <!-- ================================== -->
    <xsl:template match="note/name">
        <fo:block space-after="3pt" font-size="9pt" font-weight="bold">
            <xsl:apply-templates/>
        </fo:block>
    </xsl:template>

    <!-- ================================== -->
    <xsl:template match="label">
        <fo:block space-after="1pt" font-size="{$field-name-font-size}">
            <xsl:apply-templates/>
        </fo:block>
    </xsl:template>
    <!-- ================================== -->
    <xsl:template match="parts">
        <fo:block>

            <xsl:apply-templates/>
        </fo:block>
    </xsl:template>
    <!-- ================================== -->
    <xsl:template match="part">
        <fo:block>
            <xsl:if test="following-sibling::part">
                <xsl:attribute name="space-after">3mm</xsl:attribute>

            </xsl:if>
            <xsl:apply-templates/>
        </fo:block>
    </xsl:template>
    <!-- ================================== -->
    <xsl:template match="line">
        <fo:table>
            <xsl:for-each select="field | subfield">
                <fo:table-column column-width="{@width}"/>

            </xsl:for-each>
            <fo:table-body>
                <fo:table-row height="{@height}">
                    <xsl:for-each select="field | subfield">
                        <fo:table-cell border="1pt solid black" padding="2pt">
                            <xsl:apply-templates select="."/>
                        </fo:table-cell>
                    </xsl:for-each>
                </fo:table-row>

            </fo:table-body>
        </fo:table>
        <xsl:apply-templates select="subfields/*"/>
    </xsl:template>
    <!-- ================================== -->
    <xsl:template match="field | subfield">
        <fo:block>
            <xsl:variable name="asterisks">
                <xsl:call-template name="put-asterisks">

                    <xsl:with-param name="length" select="@footnote-id"/>
                </xsl:call-template>
            </xsl:variable>
            <xsl:variable name="prefix">
                <xsl:if test="self::field">
                    <xsl:value-of select="concat($asterisks, @id, '.&#xA0;')"/>
                </xsl:if>
            </xsl:variable>
            <fo:block font-weight="bold" font-size="{$field-name-font-size}">

                <xsl:value-of select="concat($prefix, string(name))"/>
                <xsl:apply-templates select="field-text"/>
            </fo:block>
            <fo:block space-before="1mm">
                <xsl:apply-templates select="options"/>
                <xsl:apply-templates select="table"/>
            </fo:block>
        </fo:block>
    </xsl:template>

    <!-- ================================== -->
    <xsl:template match="options">
        <fo:block font-size="{$option-font-size}">
            <xsl:call-template name="put-options">
                <xsl:with-param name="node-set" select="option"/>
            </xsl:call-template>
        </fo:block>
    </xsl:template>
    <!-- ================================== -->

    <xsl:template match="u">
        <fo:inline text-decoration="underline">
            <xsl:apply-templates/>
        </fo:inline>
    </xsl:template>
    <!-- ================================== -->
    <xsl:template match="field-text">
        <fo:inline font-size="{$small-font-size}">
            <xsl:apply-templates/>

        </fo:inline>
    </xsl:template>
    <!-- ************************************************ -->
    <!-- ************** auxiliary templates ************* -->
    <!-- ************************************************ -->
    <xsl:template name="put-asterisks">
        <xsl:param name="length"/>
        <xsl:choose>
            <xsl:when test="not($length)"/>

            <xsl:otherwise>* <xsl:call-template name="put-asterisks">
                    <xsl:with-param name="length" select="$length - 1"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <!-- ================================== -->
    <xsl:template name="put-options">

        <xsl:param name="node-set"/>
        <xsl:choose>
            <xsl:when test="not($node-set)"/>
            <xsl:otherwise>
                <xsl:value-of select="concat($node-set[1], $space-between-options)"/>
                <xsl:call-template name="put-options">
                    <xsl:with-param name="node-set" select="$node-set[position() &gt; 1]"/>
                </xsl:call-template>
            </xsl:otherwise>

        </xsl:choose>
    </xsl:template>
    <!-- ================================== -->
    <xsl:template name="put-rows">
        <xsl:param name="qty"/>
        <xsl:param name="columns"/>
        <xsl:param name="idx" select="1"/>
        <xsl:choose>
            <xsl:when test="$idx &gt; $qty"/>

            <xsl:otherwise>
                <fo:table-row>
                    <fo:table-cell>
                        <fo:block>
                            <xsl:value-of select="$idx"/>
                        </fo:block>
                    </fo:table-cell>
                    <xsl:call-template name="put-cells">
                        <xsl:with-param name="qty" select="$columns"/>

                    </xsl:call-template>
                </fo:table-row>
                <xsl:call-template name="put-rows">
                    <xsl:with-param name="qty" select="$qty"/>
                    <xsl:with-param name="columns" select="$columns"/>
                    <xsl:with-param name="idx" select="$idx + 1"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>

    </xsl:template>
    <!-- ================================== -->
    <xsl:template name="put-cells">
        <xsl:param name="qty"/>
        <xsl:param name="idx" select="1"/>
        <xsl:choose>
            <xsl:when test="$idx &gt; $qty"/>
            <xsl:otherwise>
                <fo:table-cell><fo:block/></fo:table-cell>

                <xsl:call-template name="put-cells">
                    <xsl:with-param name="qty" select="$qty"/>
                    <xsl:with-param name="idx" select="$idx + 1"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
