<?xml version='1.0' encoding='UTF-8'?>

<!-- output one HTML for each language -->

<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:udhr="http://www.unhchr.ch/udhr"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes='udhr'
  version="2.0">


<!-- if "#no", a single HTML document is sent to stdout;
     otherwise, this parameter is a directory, and one HTML per language
     is created there-->
<xsl:param name='split-into'/>

<!-- whether only the demo versions ("yes")
     or all the versions ("no") are processed -->
<xsl:param name='subset'/>

<!-- whether the full text is used ("yes")
     or only the first article ("no") is output -->
<xsl:param name='full'/>



<xsl:output 
  method="xml"
  omit-xml-declaration='yes'
  doctype-public='-//W3C//DTD XHTML 1.0 Strict//EN'
  doctype-system='http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'
  indent="yes"
  encoding="UTF-8"/>


<xsl:template match='/'>
  <xsl:choose>
    <xsl:when test='$split-into="#no"'>
      <xsl:apply-templates select='udhrs' mode='single'/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select='udhrs' mode='multiple'/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!--________________________________________________________________ multiple -->

<xsl:template match='udhrs' mode='multiple'>
    <html>
      <head>
        <title>UDHR Index</title>
      </head>
      <body>
        <h3 style="text-align: center;">Universal Declaration of Human Rights - Index</h3>
        <ul>
	  <xsl:apply-templates select='udhr[@stage!=2]' mode='#current'>
	    <xsl:sort select="@n"/>
	  </xsl:apply-templates>
        </ul>
      </body>
    </html>
</xsl:template>

<xsl:template match='udhr' mode='multiple'>
  <xsl:variable name='code'>
    <xsl:choose>
     <xsl:when test='@v'><xsl:value-of select='@l'/>_<xsl:value-of select='@v'/></xsl:when>
      <xsl:otherwise><xsl:value-of select='@l'/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name='name'>
    <xsl:choose>
     <xsl:when test='@nv'><xsl:value-of select='@n'/> (<xsl:value-of select='@nv'/>)</xsl:when>
      <xsl:otherwise><xsl:value-of select='@n'/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name='f'>udhr_<xsl:value-of select='$code'/>.xml</xsl:variable>

  <!--xsl:message select='$code'/-->
  
  <xsl:result-document href='{$split-into}/udhr_{$code}.html'>
    <xsl:apply-templates select='document($f,/)' mode='#current'>
      <xsl:with-param name='name' select='$name'/>
    </xsl:apply-templates>
  </xsl:result-document>
</xsl:template>

<xsl:template match='udhr:udhr' mode='multiple'>
  <xsl:param name='name'/>
  <html>
    <xsl:call-template name='dir-rtl'/>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
      <title>UDHR - <xsl:value-of select='$name'/></title>
    </head>    

    <body>
      <h3 style='text-align:center'>
	<xsl:call-template name='dir-ltr'/>
        <xsl:text>Universal Declaration of Human Rights - </xsl:text>
        <xsl:value-of select='$name'/>
      </h3>

      <p>
	<xsl:call-template name='dir-ltr'/>
	&#x00A9; 1996 &#x2013; 2009 The Office of the High Commissioner for Human Rights</p>

      <p>
	<xsl:call-template name='dir-ltr'/>
	This HTML version prepared by the <i>UDHR in Unicode</i>
	project, <a
	href='http://www.unicode.org/udhr'>http://www.unicode.org/udhr</a>.</p>

      <hr/>

      <xsl:apply-templates/>

      <hr/>
    </body>
  </html>
</xsl:template>

<!--__________________________________________________________________ single -->

<xsl:template match='udhrs' mode='single'>
  <xsl:variable name='title1'>
    <xsl:choose>
      <xsl:when test='$full="yes"'>
	<xsl:text>Full text</xsl:text>
      </xsl:when>
      <xsl:otherwise>
	<xsl:text>First article</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name='title2'>
    <xsl:choose>
      <xsl:when test='$subset="yes"'>
	<xsl:text>selected languages</xsl:text>
      </xsl:when>
      <xsl:otherwise>
	<xsl:text>all languages</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
      <title>UDHR - <xsl:value-of select='$title1'/>, <xsl:value-of
      select='$title2'/></title>
    </head>

    <body>
      <h3 style='text-align:center'>Universal Declaration of Human Rights -
      <xsl:value-of select='$title1'/>, <xsl:value-of
      select='$title2'/></h3>

      <p>&#x00A9; 1996 &#x2013; 2009 The Office of the High Commissioner for
      Human Rights</p>

      <p>This HTML version prepared by the <i>UDHR in Unicode</i>
      project, <a
      href='http://www.unicode.org/udhr'>http://www.unicode.org/udhr</a>.</p>

      <hr/>

      <xsl:choose>
	<xsl:when test='$subset="yes"'>
	  <xsl:apply-templates select='udhr[@stage!=2][@demo="y"]' mode='#current'>
	    <xsl:sort lang='en' select='@n'/>
	    <xsl:sort lang='en' select='@nv'/>
	  </xsl:apply-templates>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:apply-templates select='udhr[@stage!=2]' mode='#current'>
	    <xsl:sort lang='en' select='@n'/>
	    <xsl:sort lang='en' select='@nv'/>
	  </xsl:apply-templates>
	</xsl:otherwise>
      </xsl:choose>

      <xsl:if test='$full!="yes"'>
	<hr/>
      </xsl:if>
    </body>
  </html>
</xsl:template>

<xsl:template match='udhr' mode='single'>
  <xsl:variable name='code'>
    <xsl:choose>
     <xsl:when test='@v'><xsl:value-of select='@l'/>_<xsl:value-of select='@v'/></xsl:when>
      <xsl:otherwise><xsl:value-of select='@l'/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name='name'>
    <xsl:choose>
     <xsl:when test='@nv'><xsl:value-of select='@n'/> (<xsl:value-of select='@nv'/>)</xsl:when>
      <xsl:otherwise><xsl:value-of select='@n'/></xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name='f'>udhr_<xsl:value-of select='$code'/>.xml</xsl:variable>

  <!--xsl:message select='$code'/-->
  
  <xsl:apply-templates select='document($f,/)/udhr:udhr' mode='#current'>
    <xsl:with-param name='name' select='$name'/>
  </xsl:apply-templates>
</xsl:template>


<xsl:template match='udhr:udhr' mode='single'>
  <xsl:param name='name'/>

  <xsl:choose>
    <xsl:when test='$full="yes"'>
      <h2><xsl:value-of select='$name'/></h2>

      <div>
	<xsl:call-template name='dir-rtl'/>
	<xsl:apply-templates/>
      </div>

      <hr/>
    </xsl:when>

    <xsl:otherwise>
      <p><xsl:value-of select='$name'/></p>

      <blockquote>
	<p>
	  <xsl:call-template name='dir-rtl'/>
	  <xsl:apply-templates select='udhr:article[@number="1"]/udhr:para/text()'/>
	</p>
      </blockquote>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!--__________________________________________________________________ common -->

<xsl:template match='udhr:udhr/udhr:title'>
  <h3><xsl:apply-templates/></h3>
</xsl:template>

<xsl:template match='udhr:preamble/udhr:title'>
  <h4><xsl:apply-templates/></h4>
</xsl:template>

<xsl:template match='udhr:article/udhr:title'>
  <h4><xsl:apply-templates/></h4>
</xsl:template>


<xsl:template match='udhr:orderedlist'>
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match='udhr:listitem'>
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match='udhr:listitem/udhr:para[position()=1]'>
  <p>
    <xsl:choose>
      <xsl:when test='ancestor::udhr:listitem[@tag]'>
	<xsl:value-of select='ancestor::udhr:listitem/@tag'/>
      </xsl:when>
      <xsl:otherwise>
	<xsl:number level='single' count='udhr:listitem' format='1.'/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:text> </xsl:text>
    <xsl:apply-templates/>
  </p>
</xsl:template>

<xsl:template match='udhr:listitem/udhr:para[position()!=1]'>
  <p>
    <xsl:apply-templates/>
  </p>
</xsl:template>

<xsl:template match='udhr:para'>
  <p><xsl:apply-templates/></p>
</xsl:template>

<xsl:template name='dir-rtl'>
  <xsl:if test='//@lang="heb" or //@lang="arb" or //@lang="pnb" or //@lang="skr" or //@lang="ydd" or //@lang="pes" or //@lang="urd" or //@lang="pbu" or //@lang="mly_arab" or //@lang="uig_arab" or //@lang="aii" or //@lang="div"'>
    <xsl:attribute name='dir'>rtl</xsl:attribute>
  </xsl:if>
</xsl:template>

<xsl:template name='dir-ltr'>
  <xsl:if test='//@lang="heb" or //@lang="arb" or //@lang="pnb" or //@lang="skr" or //@lang="ydd" or //@lang="pes" or //@lang="urd" or //@lang="pbu" or //@lang="mly_arab" or //@lang="uig_arab" or //@lang="aii" or //@lang="div"'>
    <xsl:attribute name='dir'>ltr</xsl:attribute>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>

