<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns="http://www.w3.org/1999/xhtml"
   version="1.0"
   >

  <xsl:output
     method="xml" encoding="utf-8" indent="yes"
     doctype-public="-//W3C//DTD XHTML 1.1//EN"
     doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
     />

  <xsl:template match="abstract">
    <div class="abstract">
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template match="links">
    <div class="links">
      <xsl:for-each select="/frame/index/link">
	<xsl:variable name="href" select="concat(@href,'.html')"/>
	<xsl:for-each select="document(concat(@href,'.xml'))/frame/index">
	  <div class="item">
	    <div class="head">
	      <xsl:element name="a">
		<xsl:attribute name="href"><xsl:value-of select="$href"/></xsl:attribute>
		<xsl:value-of select="title"/>
	      </xsl:element>
	    </div>
	    <div class="text">
	      <xsl:apply-templates select="abstract"/>
	    </div>
	  </div>
	</xsl:for-each>
      </xsl:for-each>
    </div>
  </xsl:template>

  <xsl:template match="link">
    <div class="index">
      <xsl:variable name="href" select="concat(@href,'.html')"/>
      <xsl:for-each select="document(concat(@href,'.xml'))/frame/index">
	<xsl:element name="a">
	  <xsl:attribute name="href"><xsl:value-of select="$href"/></xsl:attribute>
	  <xsl:apply-templates select="title"/>
	</xsl:element>
	<xsl:apply-templates select="link"/>
      </xsl:for-each>
    </div>
  </xsl:template>

  <xsl:template match="smiley">
    <span class="smiley"><xsl:value-of select="."/></span>
  </xsl:template>

  <xsl:template match="smiley-huge">
    <span class="smiley-huge"><xsl:value-of select="."/></span>
  </xsl:template>

  <xsl:template match="key">
    <xsl:for-each select="document('keys.xml')/keys/key[@name=current()/@name]">
      <xsl:variable name="inside">
	<em class="key">
	  <xsl:choose>
	    <xsl:when test="acronym">
	      <xsl:element name="acronym">
		<xsl:attribute name="title"><xsl:value-of select="acronym"/></xsl:attribute>
		<xsl:value-of select="@name"/>
	      </xsl:element>
	    </xsl:when>
	    <xsl:when test="abbr">
	      <xsl:element name="abbr">
		<xsl:attribute name="title"><xsl:value-of select="abbr"/></xsl:attribute>
		<xsl:value-of select="@name"/>
	      </xsl:element>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="@name"/>
	    </xsl:otherwise>
	  </xsl:choose>
	</em>
      </xsl:variable>
      <xsl:choose>
	<xsl:when test="a">
	  <xsl:element name="a">
	    <xsl:attribute name="href"><xsl:value-of select="a/@href"/></xsl:attribute>
	    <xsl:copy-of select="$inside"/>
	  </xsl:element>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:copy-of select="$inside"/>
	</xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="a">
    <xsl:element name="a">
      <xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute>
      <xsl:attribute name="class"><xsl:value-of select="@class"/></xsl:attribute>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="file">
    <xsl:element name="a">
      <xsl:attribute name="href"><xsl:value-of select="document('conf.xml')/conf/file-prefix"/><xsl:value-of select="@path"/></xsl:attribute>
      <xsl:attribute name="class">file</xsl:attribute>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="dir">
    <xsl:element name="a">
      <xsl:attribute name="href"><xsl:value-of select="document('conf.xml')/conf/dir-prefix"/><xsl:value-of select="@path"/></xsl:attribute>
      <xsl:attribute name="class">dir</xsl:attribute>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="code">
    <xsl:element name="code">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="pre">
    <xsl:element name="pre">
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="em">
    <xsl:element name="em">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="url">
    <span class="url">
      <xsl:apply-templates/>
    </span>
  </xsl:template>

  <xsl:template match="ul">
    <xsl:element name="ul">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="li">
    <xsl:element name="li">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="picture">
    <div class="picture">
      <xsl:element name="img">
	<xsl:attribute name="src"><xsl:value-of select="@src"/></xsl:attribute>
	<xsl:attribute name="alt"><xsl:value-of select="@alt"/></xsl:attribute>
      </xsl:element>
    </div>
  </xsl:template>

  <xsl:template match="img">
    <xsl:element name="img">
      <xsl:attribute name="src"><xsl:value-of select="@src"/></xsl:attribute>
      <xsl:attribute name="alt"><xsl:value-of select="@alt"/></xsl:attribute>
    </xsl:element>
  </xsl:template>

  <xsl:template match="text">
    <div class="text">
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template match="head">
    <div class="head">
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template match="item">
    <div class="item">
      <xsl:apply-templates select="head|text"/>
    </div>
  </xsl:template>

  <xsl:template match="title">
    <span class="title"><xsl:apply-templates/></span>
  </xsl:template>

  <xsl:template match="section">
    <div class="section">
      <xsl:apply-templates select="section|title|item"/>
    </div>
  </xsl:template>

  <xsl:template match="email">
    <xsl:element name="a">
      <xsl:attribute name="href">mailto:<xsl:value-of select="@alias"/> &lt;<xsl:value-of select="@user"/>@<xsl:value-of select="@domain"/>&gt;</xsl:attribute>
      <xsl:value-of select="@user"/>@<xsl:value-of select="@domain"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="contact">
    <div class="foot">
      <xsl:apply-templates select="email"/>
    </div>
  </xsl:template>

  <xsl:template match="frame">
    <div class="frame">
      <xsl:apply-templates select="contact"/>
      <div class="title"><xsl:value-of select="index/title"/></div>
      <xsl:apply-templates select="section|links|item"/>
    </div>
  </xsl:template>

  <xsl:template match="menu">
    <div class="menu">
      <xsl:apply-templates select="section|item|link"/>
    </div>
  </xsl:template>

  <xsl:template match="person">
    <xsl:for-each select="document('persons.xml')/persons/person[@id=current()/@id]">
      <span class="person">
	<xsl:choose>
	  <xsl:when test="homepage">
	    <xsl:element name="a">
	      <xsl:attribute name="title"><xsl:value-of select="name"/></xsl:attribute>
	      <xsl:attribute name="href"><xsl:value-of select="homepage"/></xsl:attribute>
	      <xsl:attribute name="class">homepage</xsl:attribute>
	      <xsl:choose>
		<xsl:when test="screenname">
		  <xsl:value-of select="screenname"/>
		</xsl:when>
		<xsl:when test="name">
		  <xsl:value-of select="name"/>
		</xsl:when>
		<xsl:otherwise>?</xsl:otherwise>
	      </xsl:choose>
	    </xsl:element>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:choose>
	      <xsl:when test="screenname">
		<xsl:value-of select="screenname"/>
	      </xsl:when>
	      <xsl:when test="name">
		<xsl:value-of select="name"/>
	      </xsl:when>
	      <xsl:otherwise>?</xsl:otherwise>
	    </xsl:choose>
	  </xsl:otherwise>
	</xsl:choose>
      </span>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="/">
    <html xml:lang="fr">
      <head>
	<title>Stéphane Gimenez</title>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
	<meta name="author" content="gim"/>
	<link rel="stylesheet" type="text/css" href="style/uranus.css" title="uranus" media="screen"/>
	<link rel="alternate stylesheet" type="text/css" href="style/earth.css" title="earth" media="screen"/>
	<link rel="alternate stylesheet" type="text/css" href="style/mars.css" title="mars" media="screen"/>
	<link rel="alternate stylesheet" type="text/css" href="style/mercury.css" title="mercury" media="screen"/>
	<link rel="alternate stylesheet" type="text/css" href="style/neptune.css" title="neptune" media="screen"/>
	<xsl:element name="meta">
	  <xsl:attribute name="name">keywords</xsl:attribute>
	  <xsl:attribute name="content">
	    <xsl:for-each select="descendant::keyword"><xsl:value-of select="@content"/>, </xsl:for-each>
	  </xsl:attribute>
	</xsl:element>
      </head>
      <xsl:element name="body">
	<xsl:attribute name="id"><xsl:value-of select="document('id.xml')"/></xsl:attribute>
	<xsl:for-each select="document('menu.xml')">
	  <xsl:apply-templates/>
	</xsl:for-each>
	<xsl:apply-templates/>
      </xsl:element>
    </html>
  </xsl:template>

</xsl:stylesheet>

