<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
  xmlns:x="http://www.w3.org/1999/xhtml"
  xmlns:c="http://www.acooke.org/countries"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" version="1.0" encoding="iso-8859-1"
   omit-xml-declaration="no" indent="yes"/>

 <xsl:template match="c:file">
  <c:country>
   <c:name>
    <xsl:value-of select="translate(following-sibling::c:name, '&#xa;', ' ')"/>
   </c:name>
   <c:code>
    <xsl:value-of select="substring-before(., '.')"/>
   </c:code>
   <xsl:apply-templates select="document(.)/child::node()"/>
  </c:country>
 </xsl:template>
 <xsl:template match="/">
  <c:arms>
   <xsl:apply-templates/>
  </c:arms>
 </xsl:template>

 <xsl:template match="x:td[preceding-sibling::x:td/x:div[text()='Geographic coordinates:']]">
  <c:coords>
   <c:lat>
    <xsl:variable name="lat-str" select="substring-before(., ',')"/>
    <xsl:variable name="lat-deg" select="number(substring-before($lat-str, ' '))"/>
    <xsl:variable name="lat-min" select="number(substring-before(substring-after($lat-str, ' '), ' '))"/>
    <xsl:variable name="lat-raw" select="$lat-deg + ($lat-min div 60.0)"/>
    <xsl:choose>
     <xsl:when test="contains($lat-str, 'N')">
      <xsl:value-of select="$lat-raw"/>
     </xsl:when>
     <xsl:otherwise>
      <xsl:value-of select="-1 * $lat-raw"/>
     </xsl:otherwise>
    </xsl:choose>
   </c:lat>
   <c:long>
    <xsl:variable name="lon-str" select="translate(substring-after(., ', '), '&#xa;', ' ')"/>
    <xsl:variable name="lon-deg" select="number(substring-before($lon-str, ' '))"/>
    <xsl:variable name="lon-min" select="number(substring-before(substring-after($lon-str, ' '), ' '))"/>
    <xsl:variable name="lon-raw" select="$lon-deg + ($lon-min div 60.0)"/>
    <xsl:choose>
     <xsl:when test="contains($lon-str, 'E')">
      <xsl:value-of select="$lon-raw"/>
     </xsl:when>
     <xsl:otherwise>
      <xsl:value-of select="-1 * $lon-raw"/>
     </xsl:otherwise>
    </xsl:choose>
   </c:long>
  </c:coords>
 </xsl:template>

 <xsl:template match="x:td[x:div[text()='Military expenditures - dollar figure:']]">
  <c:dollar>
   <xsl:variable name="dol-str" select="following-sibling::x:td"/>
   <xsl:variable name="dol-num">
    <xsl:choose>
     <xsl:when test="contains($dol-str, 'million')">
      <xsl:value-of select="1000000 * number(translate(substring-before(substring-after($dol-str, '$'), ' '), ',', ''))"/>
     </xsl:when>
     <xsl:when test="contains($dol-str, 'billion')">
      <xsl:value-of select="1000000000 * number(translate(substring-before(substring-after($dol-str, '$'), ' '), ',', ''))"/>
     </xsl:when>
     <xsl:otherwise>
      <xsl:value-of select="$dol-str"/>
     </xsl:otherwise>
    </xsl:choose>
   </xsl:variable>
   <xsl:value-of select="format-number($dol-num, '##################')"/>
  </c:dollar>
 </xsl:template>

 <xsl:template match="x:td[x:div[text()='Military expenditures - percent of GDP:']]">
  <xsl:variable name="gdp-str" select="substring-before(following-sibling::x:td, '%')"/>
  <xsl:variable name="gdp">
   <xsl:choose>
    <xsl:when test="contains($gdp-str, '-')">
     <xsl:value-of select="substring-before($gdp-str, '-')"/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:value-of select="$gdp-str"/>
    </xsl:otherwise>
   </xsl:choose>
  </xsl:variable>
  <c:gdp>
   <xsl:value-of select="$gdp"/>
  </c:gdp>
 </xsl:template>

 <xsl:template match="x:td[x:div[text()='Population:']]">
  <c:population>
   <xsl:variable name="pop-str" select="translate(following-sibling::x:td, '&#xa;,', ' ')"/>
   <xsl:variable name="pop-num">
    <xsl:choose>
     <xsl:when test="contains($pop-str, ' ')">
      <xsl:value-of select="substring-before($pop-str, ' ')"/>
     </xsl:when>
     <xsl:otherwise>
      <xsl:value-of select="$pop-str"/>
     </xsl:otherwise>
    </xsl:choose>
   </xsl:variable>
   <xsl:value-of select="format-number($pop-num, '##################')"/>
  </c:population>
 </xsl:template>

 <xsl:template match="*">
  <xsl:apply-templates select="child::*"/>
 </xsl:template>

 <xsl:template match="@*"/>
 <xsl:template match="text()"/>

</xsl:stylesheet>
