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

<!-- ######################################################## -->
<!--

COPYRIGHT  (C)  2005 
THE REGENTS OF THE UNIVERSITY OF MICHIGAN
ALL RIGHTS RESERVED
 
PERMISSION IS GRANTED TO USE, COPY, CREATE DERIVATIVE WORKS AND REDISTRIBUTE 
THIS SOFTWARE AND SUCH DERIVATIVE WORKS FOR ANY PURPOSE, SO LONG AS NO FEE IS 
CHARGED, AND SO LONG AS THE COPYRIGHT NOTICE ABOVE, THIS GRANT OF PERMISSION, 
AND THE DISCLAIMER BELOW APPEAR IN ALL COPIES MADE; AND SO LONG AS THE NAME OF 
THE UNIVERSITY OF MICHIGAN IS NOT USED IN ANY ADVERTISING OR PUBLICITY 
PERTAINING TO THE USE OR DISTRIBUTION OF THIS SOFTWARE WITHOUT SPECIFIC, 
WRITTEN PRIOR AUTHORIZATION.
 
THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION FROM THE UNIVERSITY OF 
MICHIGAN AS TO ITS FITNESS FOR ANY PURPOSE, AND WITHOUT WARRANTY BY THE 
UNIVERSITY OF MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT 
LIMITATION THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
PARTICULAR PURPOSE. THE REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE 
LIABLE FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR 
CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING OUT OF OR IN 
CONNECTION WITH THE USE OF THE SOFTWARE, EVEN IF IT HAS BEEN OR IS HEREAFTER 
ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

WARNING: This stylesheet makes certain assumptions in order to enable
linking between elements.

1) The following elements require r:IDs so that they can be linked to:

- s:StudyUnit
- c:Concept
- d:QuestionItem
- l:Variable

2) r:ID elements are unique *within the StudyUnit*. You can have multiple 
"V0001" variables, one in each s:StudyUnit, but you cannot have multiple
within the same s:StudyUnit, nor can you have a d:QuestionItem that also
has a "V0001" r:ID.

3) 



FYI, the design is strongly table-based. I would have preferred to avoid using
tables, but the data in DDI files is so fielded in nature, that it's very hard to read
if you don't make a clear delineation between field label and field content.



-->
<!-- ######################################################## -->


<xsl:param name="filename"/>
<xsl:param name="token" select="'vars'"/>

<!-- ######################################################## -->
<!-- main templates -->
<!-- ######################################################## -->

<xsl:template match="ns1:DDIInstance">
 
<html>
<head>
	<title><xsl:value-of select="s:StudyUnit/r:Citation/r:Title"/></title>
<style type="text/css">

td, th {
font-size: 10pt;
font-family: Arial;
vertical-align: top;
padding: 5px;
}

h3 {
padding-top: 5px;
margin-top: 5px;
}

.odd {
background-color: #ffffff;
}

.even {
background-color: #cccccc;
}

h3, p {
padding-top: 0px;
margin-top: 0px;
}

</style>
</head>

<body bgcolor="#FFFFFF">
  
<h1>DDI Instance -- <xsl:value-of select="$filename"/></h1>

<ul>
<li><a href="?token=desc">Display descriptive information</a></li>
<li><a href="?token=vars">Display variables</a></li>
</ul>

<table border="0" width="100%">

	<xsl:if test="count(s:StudyUnit)&gt;'1'">
		<p>This DDI instance contains the following studies:</p>
		<ul>
			<xsl:for-each select="s:StudyUnit">
				<li><a href="#{r:Identification/r:ID}"><xsl:value-of select="r:Citation/r:Title"/></a></li>
			</xsl:for-each>
		</ul>
	</xsl:if> 

<xsl:if test="$token='desc'">
 	<xsl:apply-templates select="g:Group"/>
</xsl:if>

 	<xsl:apply-templates select="s:StudyUnit"/>

</table>

</body>
</html>
 
</xsl:template>

<xsl:template match="g:Group">
	<xsl:apply-templates select="r:Citation/r:Title" mode="header2"/>
	<xsl:apply-templates select="r:Citation"/>
	<xsl:apply-templates select="g:Abstract/r:Content" mode="row">
		<xsl:with-param name="label" select="'Description'"/>
	</xsl:apply-templates>
</xsl:template>

<xsl:template match="r:Citation">
	<xsl:apply-templates select="r:Creator" mode="row"/>
	<xsl:apply-templates select="r:Publisher" mode="row"/>
	<xsl:apply-templates select="r:Contributor" mode="row"/>
	<xsl:apply-templates select="r:PublicationDate"/>
</xsl:template>

<xsl:template match="r:PublicationDate">
	<xsl:variable name="content">
		<xsl:choose>
			<xsl:when test="r:StartDate and r:EndDate"><xsl:value-of select="r:StartDate"/> - <xsl:value-of select="r:EndDate"/></xsl:when>
			<xsl:when test="r:SimpleDate"><xsl:value-of select="r:SimpleDate"/></xsl:when>
		</xsl:choose>
	</xsl:variable>
	<xsl:call-template name="generic-row">
		<xsl:with-param name="label" select="'Pub. Date(s)'"/>
		<xsl:with-param name="content" select="$content"/>
	</xsl:call-template>
</xsl:template>

<xsl:template match="s:StudyUnit">
 	<xsl:variable name="study-token" select="r:Identification/r:ID"/>

	<xsl:apply-templates select="r:Citation/r:Title" mode="header2">
		<xsl:with-param name="name" select="r:Identification/r:ID"/>
	</xsl:apply-templates>

<xsl:if test="$token='desc'">
	<xsl:apply-templates select="r:Citation"/>
	<xsl:apply-templates select="s:Abstract/r:Content" mode="row">
		<xsl:with-param name="label" select="'Description'"/>
	</xsl:apply-templates>	
	<xsl:apply-templates select="r:Universe" mode="row"/>
	<xsl:apply-templates select="r:FundingInformation"/>
	<xsl:apply-templates select="r:Coverage"/>
	<xsl:apply-templates select="s:AnalysisUnit" mode="row">
		<xsl:with-param name="label" select="'Unit of Analysis'"/>
	</xsl:apply-templates>
	<xsl:apply-templates select="d:DataCollection" mode="description"/>
</xsl:if>


	<!-- begin variable section -->
	
<xsl:if test="$token='vars'">
	<xsl:apply-templates select="c:ConceptualComponent/c:ConceptScheme">
		<xsl:with-param name="study-token" select="$study-token"/>
	</xsl:apply-templates>
	  
	<xsl:apply-templates select="d:DataCollection" mode="questions">
		<xsl:with-param name="study-token" select="$study-token"/>
	</xsl:apply-templates>
	
	<xsl:apply-templates select="l:LogicalProduct">
		<xsl:with-param name="study-token" select="$study-token"/>
	</xsl:apply-templates>
</xsl:if>

</xsl:template>

<!-- ######################################################## -->
<!-- bibligographic templates -->
<!-- ######################################################## -->

<xsl:template match="r:FundingInformation">
	<xsl:variable name="content">
		<xsl:apply-templates select="r:Agency"/>
		<xsl:apply-templates select="r:AgencyAbbreviation"/>
		<xsl:apply-templates select="r:GrantNumber"/>
	</xsl:variable>
	<xsl:call-template name="generic-row">
		<xsl:with-param name="label" select="'Funding'"/>
		<xsl:with-param name="content" select="$content"/>
	</xsl:call-template>
</xsl:template>

<xsl:template match="r:Agency"><xsl:value-of select="."/> </xsl:template>

<xsl:template match="r:AgencyAbbreviation">(<xsl:value-of select="."/>) </xsl:template>

<xsl:template match="r:GrantNumber">(<xsl:value-of select="."/>) </xsl:template>

<xsl:template match="r:Coverage">
	<xsl:apply-templates select="r:TopicalCoverage"/>
	<xsl:apply-templates select="r:SpatialCoverage"/>
	<xsl:apply-templates select="r:TemporalCoverage/r:ReferenceDate/r:SimpleDate" mode="row">
		<xsl:with-param name="label" select="'Time Period'"/>
	</xsl:apply-templates>
</xsl:template>

<xsl:template match="r:TopicalCoverage">
	<xsl:apply-templates select="r:Subject[1]"/>
	<xsl:apply-templates select="r:Keyword[1]"/>
</xsl:template>

<xsl:template match="r:Subject">
	<xsl:variable name="content">
		<xsl:for-each select="../r:Subject">
			<xsl:value-of select="."/><xsl:if test="position()!=last()">, </xsl:if>
		</xsl:for-each>
	</xsl:variable>
	<xsl:call-template name="generic-row">
		<xsl:with-param name="label" select="'Subject Term(s)'"/>
		<xsl:with-param name="content" select="$content"/>
	</xsl:call-template>
</xsl:template>

<xsl:template match="r:Keyword">
	<xsl:variable name="content">
		<xsl:for-each select="../r:Keyword">
			<xsl:value-of select="."/><xsl:if test="position()!=last()">, </xsl:if>
		</xsl:for-each>
	</xsl:variable>
	<xsl:call-template name="generic-row">
		<xsl:with-param name="label" select="'Keyword(s)'"/>
		<xsl:with-param name="content" select="$content"/>
	</xsl:call-template>
</xsl:template>

<xsl:template match="r:SpatialCoverage">
	<!-- need mockups of how this should display -->
</xsl:template>

<xsl:template match="d:DataCollection" mode="description">
	<xsl:apply-templates select="d:Methodology"/>
	<xsl:apply-templates select="d:CollectionEvent"/>
	<xsl:apply-templates select="d:ProcessingEvent"/>
</xsl:template>

<xsl:template match="d:Methodology">
	<xsl:apply-templates select="d:SamplingProcedure" mode="row">
		<xsl:with-param name="label" select="'Sampling'"/>
	</xsl:apply-templates>	
</xsl:template>

<xsl:template match="d:CollectionEvent">
	<xsl:apply-templates select="d:DataSource" mode="row">
		<xsl:with-param name="label" select="'Data Source'"/>
	</xsl:apply-templates>	
	<xsl:apply-templates select="d:ModeOfCollection/r:Content" mode="row">
		<xsl:with-param name="label" select="'Mode of Collection'"/>
	</xsl:apply-templates>	
</xsl:template>

<xsl:template match="d:ProcessingEvent">
	<xsl:apply-templates select="d:CleaningOperation" mode="row">
		<xsl:with-param name="label" select="'Extent of Processing'"/>
	</xsl:apply-templates>	
	<xsl:apply-templates select="d:Weighting/r:Content" mode="row">
		<xsl:with-param name="label" select="'Weighting'"/>
	</xsl:apply-templates>	
</xsl:template>

<xsl:template match="d:Agency">
	<p><xsl:text>As performed by </xsl:text><xsl:value-of select="."/>.</p>
</xsl:template>

<!-- ######################################################## -->
<!-- concept templates -->
<!-- ######################################################## -->

<xsl:template match="c:ConceptualComponent">
	<xsl:param name="study-token"/>
	<xsl:apply-templates select="c:ConceptScheme">
		<xsl:with-param name="study-token" select="$study-token"/>
	</xsl:apply-templates>
</xsl:template>

<xsl:template match="c:ConceptScheme">
	<xsl:param name="study-token"/>
	<xsl:variable name="content">
		<p>This study contains the following concepts, which link to specific questions:</p>
		<ul>
			<xsl:apply-templates select="c:Concept">		
				<xsl:with-param name="study-token" select="$study-token"/>
			</xsl:apply-templates>
		</ul>
	</xsl:variable>
	<xsl:call-template name="major-row">
		<xsl:with-param name="label" select="'Concepts'"/>
		<xsl:with-param name="content" select="$content"/>
	</xsl:call-template>
</xsl:template>

<xsl:template match="c:Concept">
	<xsl:param name="study-token"/>
	<xsl:variable name="concept-token"><xsl:value-of select="r:Identification/r:ID"/></xsl:variable>

	<li><xsl:value-of select="c:Description"/>
	(<xsl:apply-templates select="../../../l:LogicalProduct/l:VariableScheme/l:Variable[l:ConceptReference/r:ID=$concept-token]/l:QuestionReference/r:ID" mode="concept-list">
			<xsl:with-param name="study-token" select="$study-token"/>
		</xsl:apply-templates>)</li>
</xsl:template>

<xsl:template match="r:ID" mode="concept-list">
	<xsl:param name="study-token"/>
	<a href="#{$study-token}_{.}"><xsl:value-of select="."/></a><xsl:if test="position()!=last()"> </xsl:if>
</xsl:template>

<!-- ######################################################## -->
<!-- questionnaire templates -->
<!-- ######################################################## -->

<xsl:template match="d:DataCollection" mode="questions">
	<xsl:param name="study-token"/>
	<xsl:apply-templates select="d:QuestionScheme">
		<xsl:with-param name="study-token" select="$study-token"/>
	</xsl:apply-templates>
</xsl:template>

<xsl:template match="d:QuestionScheme">
	<xsl:param name="study-token"/>
	<xsl:variable name="content">
	<p>This study contains the following questions, which link to specific variables:</p>
		<ul>
			<xsl:apply-templates select="d:QuestionItem">
				<xsl:with-param name="study-token" select="$study-token"/>
			</xsl:apply-templates>
		</ul>
	</xsl:variable>
	<xsl:call-template name="major-row">
		<xsl:with-param name="label" select="'Questions'"/>
		<xsl:with-param name="content" select="$content"/>
	</xsl:call-template>
</xsl:template>

<xsl:template match="d:QuestionItem">
	<xsl:param name="study-token"/>
	<xsl:variable name="question-token">
		<xsl:choose>
			<xsl:when test="r:Identification/r:ID">
				<xsl:value-of select="r:Identification/r:ID"/>
			</xsl:when>
		</xsl:choose>
	</xsl:variable>
	<li>
		<xsl:call-template name="list-preface">
			<xsl:with-param name="content" select="$question-token"/>
			<xsl:with-param name="study-token" select="$study-token"/>
		</xsl:call-template>
		<xsl:apply-templates select="d:QuestionText"/>
	(<xsl:apply-templates select="../../../l:LogicalProduct/l:VariableScheme/l:Variable[l:QuestionReference/r:ID=$question-token]/r:Identification/r:ID" mode="concept-list">
		<xsl:with-param name="study-token" select="$study-token"/>
	</xsl:apply-templates>)
	</li>
</xsl:template>

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

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

<xsl:template match="d:Text">
	<xsl:value-of select="."/>
</xsl:template>


<!-- ######################################################## -->
<!-- variable templates -->
<!-- ######################################################## -->

<xsl:template match="l:LogicalProduct">
	<xsl:param name="study-token"/>
	<xsl:apply-templates select="l:VariableScheme">
	 	<xsl:with-param name="study-token" select="$study-token"/>
	</xsl:apply-templates>
</xsl:template>

<xsl:template match="l:VariableScheme">
	<xsl:param name="study-token"/>
	<xsl:variable name="content">
	<p>This study contains the following variables:</p>
		<ul>
			<xsl:for-each select="l:Variable">
				<li><a href="#{$study-token}_{r:Identification/r:ID}"><xsl:value-of select="r:Identification/r:ID"/></a>: <xsl:value-of select="r:Label"/></li>
			</xsl:for-each>
		</ul>
	</xsl:variable>
	<xsl:call-template name="major-row">
		<xsl:with-param name="label" select="'Variables'"/>
		<xsl:with-param name="content" select="$content"/>
	</xsl:call-template>
	<xsl:apply-templates select="l:Variable">
	 	<xsl:with-param name="study-token" select="$study-token"/>
	</xsl:apply-templates>
</xsl:template>

<xsl:template match="l:Variable">
	<xsl:param name="study-token"/>
	<xsl:variable name="variable-token"><xsl:value-of select="r:Identification/r:ID"/></xsl:variable>
	<xsl:variable name="content">
		<xsl:call-template name="list-preface">
			<xsl:with-param name="content" select="r:Identification/r:ID"/>
			<xsl:with-param name="study-token" select="$study-token"/>
		</xsl:call-template>
		<xsl:value-of select="r:Label"/>
	</xsl:variable>
	<xsl:call-template name="header4">
		<xsl:with-param name="content" select="$content"/>
		<xsl:with-param name="name" select="concat($study-token,'_',r:Identification/r:ID)"/>
	</xsl:call-template>

	<xsl:apply-templates select="l:ConceptReference">
		<xsl:with-param name="study-token" select="$study-token"/>
	</xsl:apply-templates>

	<xsl:apply-templates select="l:QuestionReference[1]">
		<xsl:with-param name="study-token" select="$study-token"/>
	</xsl:apply-templates>

<xsl:apply-templates select="r:Universe" mode="row"/>

<xsl:apply-templates select="../../../pd:PhysicalDataProduct/pd:RecordLayout/pd:DataItem[pd:VariableReference/r:ID=$variable-token]/pd:PhysicalLocation/pd:ValueLocation"/>

<xsl:apply-templates select="l:Representation">
	<xsl:with-param name="variable-token" select="$variable-token"/>
</xsl:apply-templates>

</xsl:template>

<xsl:template match="pd:ValueLocation">
	<xsl:param name="study-token"/>
	<xsl:variable name="content"><xsl:value-of select="pd:StartPosition"/> - <xsl:value-of select="pd:StartPosition + pd:Width"/></xsl:variable>
	<xsl:call-template name="generic-row">
		<xsl:with-param name="label" select="'Position'"/>
		<xsl:with-param name="content" select="$content"/>
	</xsl:call-template>
</xsl:template>


<xsl:template match="l:ConceptReference">
	<xsl:param name="study-token"/>
	<xsl:variable name="content">
		<xsl:apply-templates select="r:ID" mode="comma-separated-list">
			<xsl:with-param name="study-token" select="$study-token"/>
		</xsl:apply-templates>
	</xsl:variable>
	<xsl:call-template name="generic-row">
		<xsl:with-param name="label" select="'Concept(s)'"/>
		<xsl:with-param name="content" select="$content"/>
	</xsl:call-template>
</xsl:template>

<xsl:template match="r:ID" mode="comma-separated-list">
	<xsl:param name="study-token"/>
	<xsl:variable name="concept-token"><xsl:value-of select="."/></xsl:variable>
	<xsl:apply-templates select="../../../../../c:ConceptualComponent/c:ConceptScheme/c:Concept[r:Identification/r:ID=$concept-token]/c:Description"/><xsl:if test="position()!=last()">, </xsl:if>
</xsl:template>

<xsl:template match="c:Description">
	<xsl:value-of select="."/>
</xsl:template>

<xsl:template match="l:QuestionReference">
	<xsl:param name="study-token"/>
	<xsl:variable name="question-token"><xsl:value-of select="r:ID"/></xsl:variable>
	<xsl:variable name="content">
		<xsl:apply-templates select="../../../../d:DataCollection/d:QuestionScheme/d:QuestionItem[r:Identification/r:ID=$question-token]/d:QuestionText"/><xsl:if test="count(.)&gt;1"><br/> <br/></xsl:if>
	</xsl:variable>
	<xsl:call-template name="generic-row">
		<xsl:with-param name="label" select="'Question(s)'"/>
		<xsl:with-param name="content" select="$content"/>
	</xsl:call-template>
</xsl:template>

<xsl:template match="l:Representation">
	<xsl:param name="variable-token"/>

	<xsl:apply-templates select="l:DataType"/>

	<xsl:if test="l:ValidDataRange">
		<xsl:variable name="content"><xsl:apply-templates select="l:ValidDataRange"/></xsl:variable>
		<xsl:call-template name="generic-row">
			<xsl:with-param name="label" select="'Valid Data Range'"/>
			<xsl:with-param name="content" select="$content"/>
		</xsl:call-template>
	</xsl:if>
	<!-- the next line begins the generation of answers/frequencies -->
	<xsl:apply-templates select="l:CodeSchemeReference">
		<xsl:with-param name="variable-token" select="$variable-token"/>
	</xsl:apply-templates>

</xsl:template>

<xsl:template match="l:DataType">
	<xsl:call-template name="generic-row">
		<xsl:with-param name="label" select="'Data Type'"/>
		<xsl:with-param name="content" select="."/>
	</xsl:call-template>
</xsl:template>

<xsl:template match="l:ValidDataRange">
	<xsl:value-of select="l:MinimumValue"/>-<xsl:value-of select="l:MaximumValue"/><xsl:if test="position()!=last()">, </xsl:if>
</xsl:template>

<xsl:template match="l:CodeSchemeReference">
	<xsl:param name="variable-token"/>
	<xsl:variable name="content"> 
		<table border="0" cellpadding="5" cellspacing="0">
			<tr class="even">
				<th>Code</th>
				<th align="left">Label</th>
				<th>Freq.</th>
				<th>%</th>
			</tr>
			<xsl:apply-templates select="l:CodeSchemeRef">
				<xsl:with-param name="variable-token" select="$variable-token"/>
			</xsl:apply-templates>	
		</table>
	</xsl:variable>
	<xsl:call-template name="generic-row">
		<xsl:with-param name="label" select="'Responses'"/>
		<xsl:with-param name="content" select="$content"/>
	</xsl:call-template>
</xsl:template>

<xsl:template match="l:CodeSchemeRef">
	<xsl:param name="variable-token"/>
	<xsl:variable name="codescheme-token"><xsl:value-of select="r:ID"/></xsl:variable>	
	
	<xsl:apply-templates select="../../../../../l:CodeScheme[r:Identification/r:ID=$codescheme-token]">
		<xsl:with-param name="variable-token" select="$variable-token"/>
	</xsl:apply-templates>
</xsl:template>

<xsl:template match="l:CodeScheme">
	<xsl:param name="variable-token"/>
	<xsl:variable name="categoryscheme-token"><xsl:value-of select="l:CategorySchemeReference/r:ID"/></xsl:variable>
	<xsl:apply-templates select="l:Code">
		<xsl:with-param name="categoryscheme-token" select="$categoryscheme-token"/>
		<xsl:with-param name="variable-token" select="$variable-token"/>
	</xsl:apply-templates>
</xsl:template>

<xsl:template match="l:Code">
	<xsl:param name="variable-token"/>
	<xsl:param name="categoryscheme-token"/>
	<xsl:variable name="code-token"><xsl:value-of select="r:ID"/></xsl:variable>
	<xsl:variable name="codevalue-token"><xsl:value-of select="l:Value"/></xsl:variable>
	<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="freq-total"><xsl:value-of select="sum(../../../pi:PhysicalInstance/pi:Statistics/pi:VariableStatistic[pi:VariableReference/r:ID=$variable-token]/pi:CategoryStatistics/pi:CategoryStatistic[@type='Frequency']/pi:Value)"/></xsl:variable>
	<xsl:variable name="freq-ind"><xsl:value-of select="../../../pi:PhysicalInstance/pi:Statistics/pi:VariableStatistic[pi:VariableReference/r:ID=$variable-token]/pi:CategoryStatistics[pi:CategoryValue=$codevalue-token]/pi:CategoryStatistic/pi:Value"/></xsl:variable>
	<xsl:variable name="validorinvalid">
		<xsl:choose>
			<xsl:when test="../../l:CategoryScheme[r:Identification/r:ID=$categoryscheme-token]/l:Category[r:Identification/r:ID=$code-token]/@missing='true'">invalid</xsl:when>
			<xsl:otherwise>valid</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	
	<tr class="{$rowtype}">
	<!-- code: present at this part of the XML -->
	<td><xsl:apply-templates select="l:Value"/></td>
	<!-- category label closeby in another section -->
	<td>
		<xsl:apply-templates select="../../l:CategoryScheme[r:Identification/r:ID=$categoryscheme-token]/l:Category[r:Identification/r:ID=$code-token]"/>
	</td>
	<!-- freqs are off in the PhysicalInstance part of the file -->
	<td align="right">
		<xsl:value-of select="$freq-ind"/>	
	</td>
	<!-- % are calculated on the fly -->
	<td align="right">
		<xsl:if test="$validorinvalid='valid'">
			<xsl:value-of select="format-number($freq-ind div $freq-total,'#00.00%')"/>
		</xsl:if>
	</td>
	</tr>
	
	<xsl:choose>
		<xsl:when test="position()=last() and $rowtype='even'">
		<tr class="odd">
			<td/>
			<th align="right">Total</th>
			<td align="right"><xsl:value-of select="$freq-total"/></td>
			<td align="right">100.00%</td>
		</tr>
		</xsl:when>
		<xsl:when test="position()=last() and $rowtype='odd'">
		<tr class="even">
			<td/>
			<th align="right">Total</th>
			<td align="right"><xsl:value-of select="$freq-total"/></td>
			<td align="right">100.00%</td>
		</tr>
		</xsl:when>
	</xsl:choose>
</xsl:template>

<xsl:template match="l:Category">
	<xsl:value-of select="r:Label"/><xsl:if test="@missing='true'"> (M)</xsl:if>
</xsl:template>

<!-- ######################################################## -->
<!-- display templates -->
<!-- ######################################################## -->

<xsl:template match="@* | node()">
	<xsl:copy>
		<xsl:apply-templates select="@* | node()"/>
	</xsl:copy>
</xsl:template>

<xsl:template match="text()">
	<xsl:value-of select="normalize-space()"/>
</xsl:template>

<xsl:template match="node()" mode="header2">
	<xsl:param name="name"/>
	<tr>
		<td colspan="2">
			<xsl:choose>
				<xsl:when test="$name=''">
					<h2><xsl:apply-templates/></h2>
				</xsl:when>
				<xsl:otherwise>
					<h2><a name="{$name}"><xsl:apply-templates/></a></h2>
				</xsl:otherwise>
			</xsl:choose>
		</td>
	</tr>
</xsl:template>

<xsl:template match="node()" mode="row">
	<xsl:param name="content"/>
	<xsl:param name="label"/>
	<tr>
		<td nowrap="nowrap">
			<xsl:choose>
				<xsl:when test="$label=''">
					<em><xsl:value-of select="local-name()"/><xsl:text>:</xsl:text></em>
				</xsl:when>
				<xsl:otherwise>
					<em><xsl:value-of select="$label"/><xsl:text>:</xsl:text></em>
				</xsl:otherwise>
			</xsl:choose>
		</td>
		<td>
			<xsl:choose>
				<xsl:when test="$content=''">
					<xsl:apply-templates/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$content"/>
				</xsl:otherwise>
			</xsl:choose>
		</td>
	</tr>
</xsl:template>

<xsl:template name="list-preface">
	<xsl:param name="study-token"/>
	<xsl:param name="content"/>
		<a name="{$study-token}_{$content}" id="{$study-token}_{$content}"><xsl:value-of select="$content"/></a>: 
</xsl:template>

<xsl:template name="header4">
	<xsl:param name="content"/>
	<xsl:param name="name"/>
	<tr>
		<td colspan="2"><h4><a name="{$name}"><xsl:copy-of select="$content"/></a></h4></td>
	</tr>
</xsl:template>

<xsl:template name="generic-row">
	<xsl:param name="label"/>
	<xsl:param name="content"/>
	<tr>
		<td nowrap="nowrap"><em><xsl:value-of select="$label"/>:</em></td>
		<td>
			<xsl:copy-of select="$content"/>	
		</td>
	</tr>
</xsl:template>

<xsl:template name="major-row">
	<xsl:param name="label"/>
	<xsl:param name="content"/>
	<tr>
		<td nowrap="nowrap"><h3><xsl:value-of select="$label"/></h3></td>
		<td>
			<xsl:copy-of select="$content"/>
		</td>
	</tr>
</xsl:template>


<!-- ######################################################## -->
<!-- xhtml templates -->
<!-- ######################################################## -->

<!--
why isn't this working?
<xsl:template match="xhtml:*">
	<xsl:element name="local-name()"><xsl:apply-templates /></xsl:element>
</xsl:template>
-->

<xsl:template match="xhtml:p">
	<p><xsl:apply-templates/></p>
</xsl:template>

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

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

</xsl:stylesheet>