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

  <xsl:output indent="yes" />

  <xsl:variable name="cell-border">solid #C0C0C0 1pt</xsl:variable>

  <xsl:attribute-set name="cell-style">
    <xsl:attribute name="padding">1mm</xsl:attribute>
    <xsl:attribute name="border-left">
      <xsl:value-of select="$cell-border"/>
    </xsl:attribute>
  </xsl:attribute-set>

  <xsl:template match="/">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

      <fo:layout-master-set>
        <fo:simple-page-master master-name="A4"
           page-width="21cm" page-height="29.7cm"
           margin-top="1cm" margin-bottom="1cm"
           margin-left="1cm" margin-right="1cm">
          <fo:region-body/>
        </fo:simple-page-master>
      </fo:layout-master-set>

      <fo:page-sequence master-reference="A4">
        <fo:flow flow-name="xsl-region-body" 
                 font-family="serif" font-size="9pt">
          <fo:table border="{$cell-border}">
            <fo:table-header text-align="center" 
                             font-family="sans-serif" font-weight="bold"
                             background-color="#E0E0E0">
	      <fo:table-row border-bottom="{$cell-border}">
		<fo:table-cell xsl:use-attribute-sets="cell-style">
		  <fo:block>Country</fo:block>
		</fo:table-cell>
		<fo:table-cell xsl:use-attribute-sets="cell-style">
		  <fo:block>Capital</fo:block>
		</fo:table-cell>
		<fo:table-cell xsl:use-attribute-sets="cell-style">
		  <fo:block>Population<fo:block></fo:block>
                  <fo:inline font-size="smaller">(<fo:inline
                   font-style="italic">million</fo:inline>)</fo:inline>
                  </fo:block>
		</fo:table-cell>
		<fo:table-cell xsl:use-attribute-sets="cell-style">
		  <fo:block>Exports<fo:block></fo:block>
                  <fo:inline font-size="smaller">(<fo:inline
                   font-style="italic">billion $</fo:inline>)</fo:inline>
                  </fo:block>
		</fo:table-cell>
		<fo:table-cell xsl:use-attribute-sets="cell-style">
		  <fo:block>Imports<fo:block></fo:block>
                  <fo:inline font-size="smaller">(<fo:inline
                   font-style="italic">billion $</fo:inline>)</fo:inline>
                  </fo:block>
		</fo:table-cell>
		<fo:table-cell xsl:use-attribute-sets="cell-style">
		  <fo:block>Miscellaneous<fo:block></fo:block>
                  <fo:inline font-size="smaller">(<fo:inline 
                  font-style="italic">flag, location</fo:inline>)</fo:inline>
                  </fo:block>
		</fo:table-cell>
	      </fo:table-row>
            </fo:table-header>
            <fo:table-body>
              <xsl:apply-templates select="//country">
                <xsl:sort data-type="number" order="descending" 
                          select="exports"/>
              </xsl:apply-templates>
            </fo:table-body>
          </fo:table>

          <fo:block font-family="sans-serif" font-size="smaller" 
                    font-style="italic" margin-top="2mm" 
                    keep-with-previous="always">
            <fo:inline>
              <xsl:call-template name="continent-color">
                <xsl:with-param name="continent" select="'Africa'" />
              </xsl:call-template>

              <xsl:text>&#xA0;&#xA0;&#xA0;&#xA0;</xsl:text>
            </fo:inline>
            <fo:inline>Africa</fo:inline>

            <fo:inline>
              <xsl:call-template name="continent-color">
                <xsl:with-param name="continent" select="'America'" />
              </xsl:call-template>

              <xsl:text>&#xA0;&#xA0;&#xA0;&#xA0;</xsl:text>
            </fo:inline>
            <fo:inline>America</fo:inline>

            <fo:inline>
              <xsl:call-template name="continent-color">
                <xsl:with-param name="continent" select="'Asia'" />
              </xsl:call-template>

              <xsl:text>&#xA0;&#xA0;&#xA0;&#xA0;</xsl:text>
            </fo:inline>
            <fo:inline>Asia</fo:inline>

            <fo:inline>
              <xsl:call-template name="continent-color">
                <xsl:with-param name="continent" select="'Australia'" />
              </xsl:call-template>

              <xsl:text>&#xA0;&#xA0;&#xA0;&#xA0;</xsl:text>
            </fo:inline>
            <fo:inline>Australia</fo:inline>

            <fo:inline>
              <xsl:call-template name="continent-color">
                <xsl:with-param name="continent" select="'Europe'" />
              </xsl:call-template>

              <xsl:text>&#xA0;&#xA0;&#xA0;&#xA0;</xsl:text>
            </fo:inline>
            <fo:inline>Europe</fo:inline>
          </fo:block>
        </fo:flow>
      </fo:page-sequence>

    </fo:root>
  </xsl:template>

  <xsl:template match="country">
    <fo:table-row border-bottom="{$cell-border}">
      <xsl:call-template name="continent-color">
        <xsl:with-param name="continent" select="continent" />
      </xsl:call-template>

      <fo:table-cell xsl:use-attribute-sets="cell-style">
        <fo:block><xsl:value-of select="name"/></fo:block>
      </fo:table-cell>
      <fo:table-cell xsl:use-attribute-sets="cell-style">
        <fo:block><xsl:value-of select="capital"/></fo:block>
      </fo:table-cell>
      <fo:table-cell xsl:use-attribute-sets="cell-style"
                     text-align="right">
        <fo:block><xsl:value-of select="population"/></fo:block>
      </fo:table-cell>
      <fo:table-cell xsl:use-attribute-sets="cell-style"
                     text-align="right">
        <fo:block><xsl:value-of select="exports"/></fo:block>
      </fo:table-cell>
      <fo:table-cell xsl:use-attribute-sets="cell-style"
                     text-align="right">
        <fo:block><xsl:value-of select="imports"/></fo:block>
      </fo:table-cell>
      <fo:table-cell xsl:use-attribute-sets="cell-style">
        <fo:table>
          <fo:table-body>
            <fo:table-row>
              <fo:table-cell padding="2mm">
                <fo:block><fo:external-graphic src="url({@flag})"
                content-width="50px"/></fo:block>
              </fo:table-cell>
              <fo:table-cell padding="2mm">
                <fo:block><xsl:value-of select="location"/></fo:block>
              </fo:table-cell>
            </fo:table-row>
          </fo:table-body>
        </fo:table>
      </fo:table-cell>
    </fo:table-row>
  </xsl:template>

  <xsl:template name="continent-color">
    <xsl:param name="continent" select="''" />

    <xsl:attribute name="background-color">
      <xsl:choose>
        <xsl:when test="$continent='Africa'">#FFFFE0</xsl:when>
        <xsl:when test="$continent='Asia'">#FFE0E0</xsl:when>
        <xsl:when test="$continent='America'">#E0E0FF</xsl:when>
        <xsl:when test="$continent='Europe'">#E0FFFF</xsl:when>
        <xsl:when test="$continent='Australia'">#FFE0FF</xsl:when>
        <xsl:otherwise>transparent</xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>
  </xsl:template>

</xsl:stylesheet>
