<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:d="ddi:datacollection:3_0" xmlns:m3="ddi:physicaldataproduct/ncube/inline:3_0" xmlns:ns7="http://purl.org/dc/elements/1.1/" xmlns:r="ddi:reusable:3_0" xmlns:c="ddi:conceptualcomponent:3_0" xmlns:p="ddi:physicaldataproduct:3_0" xmlns:cm="ddi:comparative:3_0" xmlns:g="ddi:group:3_0" xmlns:l="ddi:logicalproduct:3_0" xmlns:pr="ddi:profile:3_0" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:ds="ddi:dataset:3_0" xmlns:m1="ddi:physicaldataproduct/ncube/normal:3_0" xmlns:ns1="ddi:instance:3_0" xmlns:a="ddi:archive:3_0" xmlns:dc="ddi:dcelements:3_0" xmlns:m2="ddi:physicaldataproduct/ncube/tabular:3_0" xmlns:pi="ddi:physicalinstance:3_0" xmlns:s="ddi:studyunit:3_0" version="1.0">

<xsl:param name="filename"/>
<xsl:param name="language" select="'en'"/>

<xsl:template match="/ns1:DDIInstance">

<results>
<page-title>Display of ICPSR Metdata Record</page-title>
<content> 
 
<xsl:apply-templates select="s:StudyUnit"/>

</content>
</results>


</xsl:template>

<xsl:template match="s:StudyUnit">

	
	
	<p class="small"><a href="?language=en">english</a> | <a href="?language=es">spanish</a> (for questions/categories)</p>

	<xsl:call-template name="tabnav"/>

	<table cellpadding="5" cellspacing="0" class="box-table">
	
	<tr>
	<td colspan="2"><h2><xsl:value-of select="r:Citation/r:Title"/></h2></td>
	</tr>
	
	
		<xsl:apply-templates select="c:ConceptualComponent"/>
	
		<xsl:apply-templates select="d:DataCollection/d:QuestionScheme"/>
	
		<xsl:apply-templates select="l:LogicalProduct/l:VariableScheme"/>

	</table>

</xsl:template>



<xsl:template name="tabnav">
	<div class="tabnav" id="tabnav">
		<ul>   
			<li id="n01"><a href="/cocoon/DDI3/templatedisplay/{$filename}.xml">Description</a></li>
			<li id="current"><a href="/cocoon/DDI3/vardisplay/{$filename}.xml">Variables</a></li>
		</ul>
	</div>
</xsl:template>



<!-- this part defines the list of concepts atop the page -->

<xsl:template match="c:ConceptualComponent">
	<xsl:apply-templates select="c:ConceptScheme"/>
</xsl:template>

<xsl:template match="c:ConceptScheme">

	<tr valign="top">
	<td>
		<h3>Concepts</h3>
	</td>
	<td>
	
	<p>This study contains the following concepts, which link to specific questions and variables:</p>
	
	<xsl:if test="c:ConceptGroup">
	
		<h4>Concept Groups</h4>
		
		<ul>
			<xsl:apply-templates select="c:ConceptGroup"/>
		</ul>
		
		<h4>Discrete Concepts</h4>
	
	</xsl:if>
	
	<ul>
		<xsl:apply-templates select="c:Concept" mode="discrete"/>
	</ul>
	
	</td>
	</tr>

</xsl:template>

<xsl:template match="c:ConceptGroup">
	<li>
		<xsl:apply-templates select="r:Label" mode="li"/><xsl:apply-templates select="r:Description" mode="li"/>  
		<xsl:if test="c:ConceptReference">
			<ul>
				<xsl:apply-templates select="c:ConceptReference"/>
			</ul>
		</xsl:if>
	</li>
</xsl:template>

<xsl:template match="r:Description" mode="li">
	<xsl:apply-templates/>
</xsl:template>

<xsl:template match="r:Label" mode="li">
	<xsl:apply-templates/><xsl:if test="../r:Description">: </xsl:if>
</xsl:template>

<xsl:template match="c:ConceptReference">
	<li>
		<xsl:apply-templates select="r:ID" mode="li-followup"/>
			<xsl:if test="c:ConceptReference">
				<ul>
					<xsl:apply-templates select="c:ConceptReference"/>
				</ul>
			</xsl:if>
		</li>
</xsl:template>

<xsl:template match="r:ID" mode="li-followup">
<xsl:variable name="token" select="."/>
	<xsl:apply-templates select="//c:ConceptScheme/c:Concept[@id=$token]"/>
</xsl:template>

<xsl:template match="c:Concept">
	<xsl:variable name="token" select="@id"/>
	<xsl:apply-templates select="r:Description"/> (<xsl:apply-templates select="//l:Variable[l:ConceptReference/r:ID = $token]/r:Name" mode="link"/> 
	<xsl:apply-templates select="//d:QuestionItem[d:ConceptReference/r:ID = $token]" mode="link"/>)
</xsl:template>

<xsl:template match="d:QuestionItem" mode="link">
	<xsl:if test="position()=1"> </xsl:if><a href="#{@id}"><xsl:value-of select="@id"/></a><xsl:if test="position()!=last()"> </xsl:if>
</xsl:template>

<xsl:template match="c:Concept" mode="discrete">
	<xsl:variable name="token" select="@id"/>
	<li><xsl:apply-templates select="r:Description"/> (<xsl:apply-templates select="//l:Variable[l:ConceptReference/r:ID = $token]/r:Name" mode="link"/> 
	<xsl:apply-templates select="//d:QuestionItem[d:ConceptReference/r:ID = $token]" mode="link"/>)</li>
</xsl:template>

<!-- end of concepts -->

<!-- begin list of questions atop page -->

<xsl:template match="d:QuestionScheme">
	<tr valign="top">
		<td>
			<h3>Questions</h3>
		</td>
		<td>
			<p>This study contains the following questions, which link to specific variables:</p>
			<ul>
				<xsl:apply-templates select="d:QuestionItem" mode="li"/>
			</ul>
		</td>
	</tr>
</xsl:template>

<xsl:template match="d:QuestionItem" mode="li">
	<li>
		<xsl:apply-templates select="d:QuestionText[@xml:lang=$language]" mode="li"/>
	</li>
</xsl:template>

<xsl:template match="d:QuestionText" mode="li">
	<xsl:apply-templates select="d:LiteralText/d:Text" mode="li"/>
</xsl:template>

<xsl:template match="d:Text" mode="li">
	<xsl:variable name="token" select="../../../@id"/>
	<xsl:value-of select="$token"/>: <xsl:apply-templates/> (<xsl:apply-templates select="//l:Variable[l:QuestionReference/r:ID = $token]/r:Name" mode="link"/>)
</xsl:template>

<xsl:template match="r:Name" mode="link">
	<a href="#{../@id}"><xsl:apply-templates/></a><xsl:if test="position()!=last()"> </xsl:if>
</xsl:template>

<!-- end of question list -->

<!-- begin list of variables and detail on individual variables -->

<xsl:template match="l:VariableScheme">
	<tr valign="top">
		<td>
			<h3>Variables</h3>
		</td>
		<td>
			<p>This study contains the following variables:</p>
			<ul>
				<xsl:apply-templates select="l:Variable" mode="li"/>
			</ul>
		</td>
	</tr>
	
	<xsl:apply-templates select="l:Variable"/>
	
</xsl:template>


<xsl:template match="l:Variable" mode="li">
	<li>
		<xsl:apply-templates select="r:Name" mode="link"/><xsl:if test="r:Name!=r:Label">: <xsl:apply-templates select="r:Label" mode="li"/></xsl:if>
	</li>
</xsl:template>

<xsl:template match="r:Name">
	<xsl:apply-templates/>
</xsl:template>

<xsl:template match="r:Label" mode="li">
	<a name="{../@id}" id="{../@id}"/><xsl:apply-templates/>
</xsl:template>

<xsl:template match="l:Variable">

	<tr valign="top">
		<td colspan="2">
			<p><strong>
			<xsl:apply-templates select="r:Label" mode="li"/>
			<xsl:if test="r:Name and r:Label!=r:Name">: <xsl:apply-templates select="r:Name"/>
			</xsl:if>
			</strong></p>
		</td>
	</tr>


<xsl:if test="l:ConceptReference">
	<tr valign="top">
		<td><em>Concept(s):</em>
		</td>
		<td>
			<xsl:apply-templates select="l:ConceptReference"/>

		</td>
	</tr>
</xsl:if>

<xsl:if test="l:QuestionReference">
	<tr valign="top">
		<td><em>Question(s):</em>
		</td>
		<td>
			<xsl:apply-templates select="l:QuestionReference" mode="full"/>

		</td>
	</tr>
</xsl:if>

<xsl:apply-templates select="l:Representation/l:CodeRepresentation/r:CodeSchemeReference"/>
<xsl:apply-templates select="l:Representation/l:NumericRepresentation"/>

</xsl:template>

<xsl:template match="l:NumericRepresentation">
	<tr valign="top">
		<td><em>Range:</em>
		</td>
		<td>
			<xsl:apply-templates select="r:NumberRange"/>

		</td>
	</tr>
</xsl:template>

<xsl:template match="r:NumberRange"><xsl:value-of select="r:Low"/> - <xsl:value-of select="r:High"/></xsl:template>

<xsl:template match="l:ConceptReference">
<xsl:variable name="token" select="r:ID"/>
<p><xsl:apply-templates select="//c:Concept[@id=$token]/r:Description"/></p>
</xsl:template>

<xsl:template match="l:QuestionReference" mode="full">
<xsl:variable name="token" select="r:ID"/>
<p><xsl:apply-templates select="//d:QuestionItem[@id=$token]/d:QuestionText[@xml:lang=$language]" mode="full"/></p>
</xsl:template>

<xsl:template match="d:QuestionText" mode="full">
	<xsl:apply-templates select="d:LiteralText/d:Text" mode="full"/>
</xsl:template>

<xsl:template match="d:Text" mode="full">
	<xsl:variable name="token" select="../../../@id"/>
	<xsl:value-of select="$token"/>: <xsl:apply-templates/>
</xsl:template>

<xsl:template match="r:CodeSchemeReference">
<xsl:variable name="token" select="r:ID"/>
<xsl:variable name="var-token" select="../../../@id"/>
<xsl:variable name="total-responses">
	<xsl:value-of select="sum(//pi:Statistics/pi:VariableStatistics[pi:VariableReference/r:ID=$var-token]/pi:CategoryStatistics/pi:CategoryStatistic[pi:CategoryStatisticType='frequency']/pi:Value)"/>
</xsl:variable>



	<tr valign="top">
		<td>
			<xsl:if test="position()='1'">
				<em>Response(s):</em>
			</xsl:if>
		</td>
		<td>
			<xsl:apply-templates select="//l:CodeScheme[@id=$token]">
				<xsl:with-param name="var-token" select="$var-token"/>
				<xsl:with-param name="total-responses" select="$total-responses"/>
				<xsl:with-param name="position" select="position()"/>
				<xsl:with-param name="count" select="count(../r:CodeSchemeReference)"/>
			</xsl:apply-templates>
		</td>
	</tr>
</xsl:template>

<xsl:template match="l:CodeScheme">
	<xsl:param name="var-token"/>
	<xsl:param name="total-responses"/>
	<xsl:param name="position"/>
	<xsl:param name="count"/>
	<table border="0" cellpadding="5" cellspacing="0" width="450px">
		<xsl:if test="$position='1'">
			<tr class="even">
			<th>Code</th>
			<th align="left">Label</th>
			<th>Freq.</th>
			<th>%</th>
			</tr>
		</xsl:if>
		<xsl:apply-templates select="l:Code">
			<xsl:with-param name="var-token" select="$var-token"/>
			<xsl:with-param name="total-responses" select="$total-responses"/>
		</xsl:apply-templates>
		<xsl:if test="$position=$count">
			<xsl:call-template name="total">
				<xsl:with-param name="total-responses" select="$total-responses"/>
			</xsl:call-template>
		</xsl:if>
	</table>
</xsl:template>

<xsl:template name="total">
	<xsl:param name="total-responses"/>
	<tr>
		<th align="right" colspan="2">Total</th>
		<td align="center"><xsl:value-of select="$total-responses"/></td>
		<td align="center">100.00%</td>
	</tr>
</xsl:template>

<xsl:template match="l:Code">
	<xsl:param name="var-token"/>
	<xsl:param name="total-responses"/>
	<xsl:variable name="rowtype"><xsl:choose><xsl:when test="(position() div 2) != round((position() div 2))">odd</xsl:when><xsl:otherwise>even</xsl:otherwise></xsl:choose></xsl:variable>
	<xsl:variable name="code-token" select="l:Value"/>
	<tr class="{$rowtype}">
		<xsl:apply-templates select="l:Value"/>
		<xsl:apply-templates select="l:CategoryReference"/>
		<xsl:apply-templates select="//pi:Statistics/pi:VariableStatistics[pi:VariableReference/r:ID=$var-token]/pi:CategoryStatistics[pi:CategoryValue=$code-token]/pi:CategoryStatistic[pi:CategoryStatisticType='frequency']">
			<xsl:with-param name="total-responses" select="$total-responses"/>
		</xsl:apply-templates>
	</tr>
</xsl:template>

<xsl:template match="l:Value">
	<td style="width:50px;text-align:center;">
		<xsl:apply-templates/>
	</td>
</xsl:template>

<xsl:template match="l:CategoryReference">
<xsl:variable name="token" select="r:ID"/>
	<td style="width:275px;"><!--<xsl:value-of select="$token" />-->
		<xsl:apply-templates select="//l:Category[@id=$token]"/>
	</td>
</xsl:template>

<xsl:template match="l:Category">
	<xsl:choose>
		<xsl:when test="not(r:Label/@xml:lang)"><xsl:apply-templates select="r:Label[1]"/></xsl:when>
		<xsl:when test="$language='es'"><xsl:apply-templates select="r:Label[@xml:lang='es']"/></xsl:when>
		<xsl:when test="$language='en'"><xsl:apply-templates select="r:Label[@xml:lang='en']"/></xsl:when>
		<xsl:otherwise><xsl:apply-templates select="r:Label[1]"/></xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="r:Label">
	<xsl:apply-templates/>
</xsl:template>

<xsl:template match="pi:CategoryStatistic">
	<xsl:param name="total-responses"/>
	<td style="width:50px;text-align:center;">
		<xsl:apply-templates select="pi:Value"/>
	</td>
	<td style="width:75px;text-align:center;">
		<xsl:value-of select="format-number(pi:Value div $total-responses,'#00.00%')"/>
	</td>
</xsl:template>

<xsl:template match="pi:Value">
	<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>