<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:output method="xml" indent="yes" />

<xsl:param name="base">http://www.bikinitest.ch</xsl:param>

<xsl:template match="/">
<!-- XSL-FO -->
<fo:root>
    <!-- Déclaration de la mise en page -->
    <fo:layout-master-set>
        <fo:simple-page-master master-name="ma-page" margin="2cm">
            <fo:region-body margin="1cm 0 1cm 0" />
            <fo:region-before extent="1cm" />
            <fo:region-after precedence="true" extent="1cm" />
        </fo:simple-page-master>
    </fo:layout-master-set>

    <!-- Corps du document -->
    <fo:page-sequence
        master-reference="ma-page"
        font-family="sans-serif"
        font-size="11.5pt"
        line-height="1.5em">
        <fo:static-content flow-name="xsl-region-before">
            <!-- table-layout="auto" isn't yet supported by FOP -->
            <fo:table table-layout="fixed" border-bottom="1px solid #ccc"
                font-size=".9em" color="#666">
                <fo:table-column column-width="5cm" />
                <fo:table-column column-width="6.31cm" />
                <fo:table-column column-width="5cm" />
                <fo:table-body>
                    <fo:table-row>
                        <fo:table-cell>
                            <fo:block>
                                <!--Récupération du title du site -->
                                <xsl:value-of select="//xhtml:div[@id =
                                    'entete']/xhtml:h1" /></fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block text-align="center"
                                font-weight="bold">
                                <!--Récupération du title de l'agenda -->
                                <xsl:value-of select="//xhtml:div[@id =
                                    'contenu']/xhtml:h1" /></fo:block>
                        </fo:table-cell>
                        <fo:table-cell>
                            <fo:block text-align="right">
                                <xsl:value-of select="$base"/>
                            </fo:block>
                        </fo:table-cell>
                    </fo:table-row>
                </fo:table-body>
            </fo:table>
        </fo:static-content>

    <!-- XSLT -->
    <fo:flow flow-name="xsl-region-body">
        <!-- le titre -->
        <xsl:apply-templates select="//xhtml:div[@id = 'contenu']/*" />
        <!-- les entrées -->
        <!--<xsl:apply-templates select="xhtml:div" />-->
    </fo:flow>

    <!-- XSL-FO -->
    </fo:page-sequence>
</fo:root>
</xsl:template>

<xsl:template match="xhtml:h1">
    <fo:block font-size="25pt">
        <xsl:value-of select="." />
    </fo:block>
</xsl:template>

<xsl:template match="xhtml:div[@class = 'prog_entry']">
    <fo:table table-layout="fixed" space-after=".5em" margin-left="2em">
        <fo:table-column column-width="120pt" />
        <fo:table-column column-width="340pt" />
        <fo:table-body>
            <fo:table-row keep-together="always">
                <fo:table-cell>
                    <xsl:apply-templates select="xhtml:dl/xhtml:dt" />
                </fo:table-cell>
                <fo:table-cell padding-top="0.5em">
                    <xsl:apply-templates select="xhtml:dl/xhtml:dd" />
                </fo:table-cell>
            </fo:table-row>
        </fo:table-body>
    </fo:table>
</xsl:template>

<xsl:template match="xhtml:dt">
    <fo:block>
        <fo:basic-link external-destination="{xhtml:a/@href}">
            <xsl:apply-templates select="xhtml:a/xhtml:img" />
        </fo:basic-link>
    </fo:block>
</xsl:template>

<xsl:template match="xhtml:img">
    <fo:external-graphic src="{concat($base, @src)}" width="{@width}pt" />
</xsl:template>

<xsl:template match="xhtml:dd[@class = 'infos']">
    <fo:block>
        <xsl:apply-templates />
    </fo:block>
</xsl:template>

<xsl:template match="xhtml:span[@class = 'date']">
    <fo:inline font-size="20pt" color="#666">
        <xsl:value-of select="." /><xsl:text> </xsl:text>
    </fo:inline>
</xsl:template>

<xsl:template match="xhtml:dd[@class = 'type']">
    <fo:block font-size="9pt" font-weight="bold">
        <xsl:value-of select="." />
    </fo:block>
</xsl:template>

<xsl:template match="xhtml:dd[@class = 'title']">
    <fo:block font-size="16pt" font-weight="bold">
        <xsl:apply-templates />
    </fo:block>
</xsl:template>

<xsl:template match="xhtml:dd[@class = 'groups']">
    <fo:block font-size="11pt" font-weight="bold">
        <xsl:apply-templates />
    </fo:block>
</xsl:template>

<xsl:template match="xhtml:dd|xhtml:div" />

<xsl:template match="xhtml:a">
    <fo:basic-link color="#333" external-destination="{@href}">
        <xsl:apply-templates />
    </fo:basic-link>
    <fo:footnote>
        <fo:inline font-size="60%" vertical-align="super">
            <xsl:value-of select="count(preceding::xhtml:a[starts-with(@href,
                'http://')]) + 1" />
        </fo:inline>
        <fo:footnote-body>
        <fo:block font-size="8pt" color="#000" font-weight="normal">
            <xsl:value-of select="count(preceding::xhtml:a[starts-with(@href,
                'http://')]) + 1" />
            <xsl:text>) </xsl:text>
            <xsl:value-of select="@href" />
        </fo:block>
    </fo:footnote-body>
    </fo:footnote>
</xsl:template>

</xsl:stylesheet>

