<?xml version="1.0" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<!-- Global Variables -->

<xsl:variable name="abbrev">
  <xsl:value-of select="/pai/agency/abbrev"/>
</xsl:variable>

<xsl:variable name="indent">
  <xsl:text>&#160;&#160;&#160;&#160;&#160;</xsl:text>
</xsl:variable>

<xsl:variable name="hasSubagencies">
  <xsl:value-of select="string-length(/pai/agency/subagency)>0"/>
</xsl:variable>

<!-- Main Document -->

<xsl:template match="pai">
  <html>
	  <head>
      <title>
        <xsl:value-of select="agency/abbrev"/>
        <xsl:text> Privacy Act Issuance</xsl:text>
      </title>
    </head>
	  <body>
      <h1>
		<font size="5">
		  <xsl:value-of select="agency/name"/>
		  <a name="top"/>
		  <xsl:if test="agency/abbrev">
		    <xsl:text> (</xsl:text>
		    <xsl:value-of select="agency/abbrev"/>
		    <xsl:text>)</xsl:text>
		  </xsl:if>
		</font>
      </h1>
	    <xsl:apply-templates select="agency[@toc='yes']"/>
	    <xsl:apply-templates select="agency" mode="body"/>
	  </body>
  </html>
</xsl:template>

<!-- Table of Contents -->

<xsl:template match="agency[@toc='yes']">
  <h2>
	<i>
	  <span style="font-weight:normal">
	    <xsl:text>Table of Contents</xsl:text>
	  </span>
	</i>
    <hr/>
  </h2>
  <xsl:apply-templates select="previousPubs[@toc='yes']"/>
  <xsl:apply-templates select="routineUses[@toc='yes']"/>
  <xsl:choose>
    <xsl:when test="$hasSubagencies='true'">
      <xsl:apply-templates select="subagency[@toc='yes']"/>
      <h3>Regulations</h3>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="section[@toc='yes']"/>
      <xsl:apply-templates select="appendix[@toc='yes']"/>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:apply-templates select="regulations[@toc='yes']"/>
  <p><a href="#top">[Return to TOP]</a></p>
</xsl:template>

<xsl:template match="subagency[@toc='yes']">
  <h3>
    <xsl:value-of select="name"/>
      <xsl:if test="abbrev">
	      <xsl:text> (</xsl:text>
	      <xsl:value-of select="abbrev"/>
	      <xsl:text>)</xsl:text>
	      <xsl:if test="note">
          <xsl:text>*</xsl:text>
	      </xsl:if>
      </xsl:if>
  </h3>
  <xsl:apply-templates select="section[@toc='yes']"/>
  <xsl:apply-templates select="appendix[@toc='yes']"/>
  <xsl:if test="note">
    <p>
      <xsl:value-of select="$indent"/>
	    <xsl:text>* </xsl:text>
	    <xsl:value-of select="note"/>
    </p>
  </xsl:if>
</xsl:template>

<xsl:template match="previousPubs[@toc='yes']">
  <p>
    <xsl:value-of select="$indent"/>
    <a>
      <xsl:attribute name="href">
        <xsl:value-of select="concat('&#35;',./@id)"/>
      </xsl:attribute>
      <xsl:value-of select="title"/>
    </a>
  </p>
</xsl:template>

<xsl:template match="routineUses[@toc='yes']">
  <p>
    <xsl:value-of select="$indent"/>
    <a>
      <xsl:attribute name="href">
        <xsl:value-of select="concat('&#35;',./@id)"/>
      </xsl:attribute>
      <xsl:text>Statement of General Routine Uses</xsl:text>
    </a>
  </p>
</xsl:template>

<xsl:template match="section[@toc='yes']">
  <p>
    <xsl:value-of select="$indent"/>
    <a>
      <xsl:attribute name="href">
        <xsl:value-of select="concat('&#35;',./@id)"/>
      </xsl:attribute>
      <xsl:value-of select="../abbrev"/>
      <xsl:text> </xsl:text>
      <xsl:value-of select="systemNumber"/>
      <xsl:text>&#8212;</xsl:text>
      <xsl:if test="subsection[@type='systemName']">
        <xsl:value-of select="subsection[@type='systemName']"/>
      </xsl:if>
      <xsl:if test="@reserved='yes'">
        <xsl:text>[Reserved]</xsl:text>
      </xsl:if>
      <xsl:if test="see">
        <xsl:text>[Deleted, See </xsl:text>
        <xsl:value-of select="see"/>
        <xsl:text>]</xsl:text>
      </xsl:if>
	    <xsl:if test="formerly">
	      <xsl:text> (formerly </xsl:text>
	      <xsl:value-of select="formerly"/>
	      <xsl:text>)</xsl:text>
	    </xsl:if>
    </a>
  </p>
</xsl:template>

<xsl:template match="appendix[@toc='yes']">
  <p>
    <xsl:value-of select="$indent"/>
    <a>
      <xsl:attribute name="href">
        <xsl:value-of select="concat('&#35;',./@id)"/>
      </xsl:attribute>
      <xsl:text>Appendix </xsl:text>
      <xsl:if test="@letter">
        <xsl:text> </xsl:text>
        <xsl:value-of select="@letter"/>
      </xsl:if>
      <xsl:if test="title">
        <xsl:text>&#8212;</xsl:text>
        <xsl:value-of select="title"/>
      </xsl:if>
    </a>
  </p>
</xsl:template>

<xsl:template match="regulations[@toc='yes']">
  <p>
    <xsl:value-of select="$indent"/>
    <a>
      <xsl:attribute name="href">
        <xsl:value-of select="concat('&#35;',./@id)"/>
      </xsl:attribute>
      <xsl:choose>
        <xsl:when test="title">
          <xsl:value-of select="title"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>Privacy Act Regulations</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </a>
  </p>
</xsl:template>

<xsl:template match="@id">
  <a>
    <xsl:attribute name="name">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </a>
</xsl:template>

<!-- Body Parts of the Document -->

<xsl:template match="agency" mode="body">
  <xsl:if test="previousPubs">
    <xsl:apply-templates select="previousPubs" mode="body"/>
  </xsl:if>
  <xsl:choose>
    <xsl:when test="$hasSubagencies='true'">
      <xsl:apply-templates select="subagency" mode="body"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="routineUses" mode="body"/>
      <xsl:apply-templates select="section" mode="body"/>
      <xsl:apply-templates select="appendix" mode="body"/>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:apply-templates select="regulations" mode="body"/>
</xsl:template>

<xsl:template match="previousPubs" mode="body">
  <h2>
	<i>
	  <span style="font-weight:normal">
		<xsl:value-of select="title"/>
	  </span>
	</i>
	<xsl:apply-templates select="@id"/>
    <hr/>
  </h2>
  <xsl:choose>
    <xsl:when test="$hasSubagencies='true'">
      <xsl:for-each select="../subagency">
        <xsl:if test="previouslyPublished">
				  <h3>
				    <xsl:value-of select="name"/>
				    <xsl:text> (</xsl:text>
				    <xsl:value-of select="abbrev"/>
				    <xsl:text>)</xsl:text>
				  </h3>
		      <xsl:call-template name="printPubs">
		        <xsl:with-param name="root" select="."/>
		      </xsl:call-template>
		    </xsl:if>
      </xsl:for-each>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="printPubs">
        <xsl:with-param name="root" select=".."/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
  <p><a href="#top">[Return to TOP]</a></p>
</xsl:template>

<xsl:template name="printPubs">
  <xsl:param name="root"/>
  <xsl:for-each select="$root/previouslyPublished[not(date/@year=preceding-sibling::previouslyPublished/date/@year)]">
    <xsl:variable name="pubYear" select="date/@year"/>
    <h4>
      <i><xsl:value-of select="$pubYear"/></i>
    </h4>
    <xsl:for-each select="$root/previouslyPublished[date/@year=$pubYear]">
      <p>
        <xsl:value-of select="$indent"/>
        <a>
          <xsl:attribute name="href">
            <xsl:value-of select="url"/>
          </xsl:attribute>
          <xsl:value-of select="title"/>
          <xsl:text>: </xsl:text>
          <xsl:call-template name="monthName">
            <xsl:with-param name="monthNumber" select="date/@month"/>
          </xsl:call-template>
          <xsl:text> </xsl:text>
          <xsl:value-of select="date/@day"/>
        </a>
      </p>
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>

<xsl:template name="monthName">
  <xsl:param name="monthNumber"/>
  <xsl:choose>
    <xsl:when test="$monthNumber='1'"><xsl:text>January</xsl:text></xsl:when>
    <xsl:when test="$monthNumber='2'"><xsl:text>February</xsl:text></xsl:when>
    <xsl:when test="$monthNumber='3'"><xsl:text>March</xsl:text></xsl:when>
    <xsl:when test="$monthNumber='4'"><xsl:text>April</xsl:text></xsl:when>
    <xsl:when test="$monthNumber='5'"><xsl:text>May</xsl:text></xsl:when>
    <xsl:when test="$monthNumber='6'"><xsl:text>June</xsl:text></xsl:when>
    <xsl:when test="$monthNumber='7'"><xsl:text>July</xsl:text></xsl:when>
    <xsl:when test="$monthNumber='8'"><xsl:text>August</xsl:text></xsl:when>
    <xsl:when test="$monthNumber='9'"><xsl:text>September</xsl:text></xsl:when>
    <xsl:when test="$monthNumber='10'"><xsl:text>October</xsl:text></xsl:when>
    <xsl:when test="$monthNumber='11'"><xsl:text>November</xsl:text></xsl:when>
    <xsl:when test="$monthNumber='12'"><xsl:text>December</xsl:text></xsl:when>
  </xsl:choose>
</xsl:template>

<xsl:template name="ordinal">
  <xsl:param name="number"/>
  <xsl:value-of select="$number"></xsl:value-of>
  <xsl:choose>
    <xsl:when test="(string-length($number) > 1) and (substring($number,string-length($number)-1,1) = '1')">
      <xsl:text>th</xsl:text>
    </xsl:when>
    <xsl:when test="substring($number,string-length($number)) = '1'">
      <xsl:text>st</xsl:text>
    </xsl:when>
    <xsl:when test="substring($number,string-length($number)) = '2'">
      <xsl:text>nd</xsl:text>
    </xsl:when>
    <xsl:when test="substring($number,string-length($number)) = '3'">
      <xsl:text>rd</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>th</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="subagency" mode="body">
  <h2>
	<i>
	  <span style="font-weight:normal">
		<xsl:value-of select="name"/>
		<xsl:if test="abbrev">
		  <xsl:text> (</xsl:text>
		  <xsl:value-of select="abbrev"/>
		  <xsl:text>)</xsl:text>
		</xsl:if>
	  </span>
	</i>
    <hr/>
  </h2>
  <xsl:apply-templates select="section" mode="body"/>
  <xsl:apply-templates select="appendix" mode="body"/>
</xsl:template>

<xsl:template match="section" mode="body">
  <h3>
    <xsl:if test="($hasSubagencies='true') and not($abbrev=../abbrev)">
      <xsl:value-of select="$abbrev"/>
      <xsl:text>/</xsl:text>
    </xsl:if>
    <xsl:value-of select="../abbrev"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="systemNumber"/>
    <xsl:if test="@reserved='yes'">
      <xsl:text> [Reserved]</xsl:text>
    </xsl:if>
    <xsl:if test="see">
      <xsl:text> [Deleted, See </xsl:text>
      <xsl:value-of select="see"/>
      <xsl:text>]</xsl:text>
    </xsl:if>
    <!-- 
    <xsl:if test="formerly">
      <xsl:text> (formerly </xsl:text>
      <xsl:value-of select="formerly"/>
      <xsl:text>)</xsl:text>
    </xsl:if>
     -->
    <xsl:apply-templates select="@id"/>
    <hr/>
  </h3>
  <xsl:apply-templates select="subsection"/>
  <xsl:apply-templates select="xhtmlContent/*"/>     
  <p><a href="#top">[Return to TOP]</a></p>
</xsl:template>

<xsl:template match="appendix" mode="body">
  <h3>
    <xsl:text>Appendix</xsl:text>
    <xsl:if test="@letter">
      <xsl:text> </xsl:text>
      <xsl:value-of select="@letter"/>
    </xsl:if>
    <xsl:if test="title">
      <xsl:text>&#8212;</xsl:text>
      <xsl:value-of select="title"/>
    </xsl:if>
    <xsl:apply-templates select="@id"/>
    <hr/>
  </h3>
  <xsl:apply-templates select="xhtmlContent/*"/>     
  <xsl:apply-templates select="genericSubsection"/>     
  <p><a href="#top">[Return to TOP]</a></p>
</xsl:template>

<xsl:template name="regulationsNode">
  <xsl:param name="node"/>
  <h4>
    <xsl:if test="not(name()='regulationsOther')">
		  <xsl:choose>
		    <xsl:when test="name()='regulationsTitle'"><xsl:text>Title</xsl:text></xsl:when>
		    <xsl:when test="name()='regulationsSubtitle'"><xsl:text>Subtitle</xsl:text></xsl:when>
		    <xsl:when test="name()='regulationsChapter'"><xsl:text>Chapter</xsl:text></xsl:when>
		    <xsl:when test="name()='regulationsPart'"><xsl:text>Part</xsl:text></xsl:when>
		    <xsl:when test="name()='regulationsSubpart'"><xsl:text>Subpart</xsl:text></xsl:when>
		    <xsl:when test="name()='regulationsAppendix'"><xsl:text>Appendix</xsl:text></xsl:when>
		  </xsl:choose>
		  <xsl:text> </xsl:text>
		  <xsl:value-of select="@number"/>
	  </xsl:if>
	  <xsl:if test="heading">
	    <xsl:text> &#8212; </xsl:text>
	    <xsl:value-of select="heading"/>
	  </xsl:if>
	  <hr/>
  </h4>
  <xsl:apply-templates select="xhtmlContent/*"/>       
  <xsl:for-each select="regulationsSubtitle">
    <xsl:call-template name="regulationsNode">
      <xsl:with-param name="node" select="."/>
    </xsl:call-template>
  </xsl:for-each>
  <xsl:for-each select="regulationsChapter">
    <xsl:call-template name="regulationsNode">
      <xsl:with-param name="node" select="."/>
    </xsl:call-template>
  </xsl:for-each>
  <xsl:for-each select="regulationsPart">
    <xsl:call-template name="regulationsNode">
      <xsl:with-param name="node" select="."/>
    </xsl:call-template>
  </xsl:for-each>
  <xsl:for-each select="regulationsSubpart">
    <xsl:call-template name="regulationsNode">
      <xsl:with-param name="node" select="."/>
    </xsl:call-template>
  </xsl:for-each>
  <xsl:for-each select="regulationsAppendix">
    <xsl:call-template name="regulationsNode">
      <xsl:with-param name="node" select="."/>
    </xsl:call-template>
  </xsl:for-each>
  <xsl:for-each select="regulationsOther">
    <xsl:call-template name="regulationsNode">
      <xsl:with-param name="node" select="."/>
    </xsl:call-template>
  </xsl:for-each>
</xsl:template>

<xsl:template match="regulations" mode="body">
  <h3>
	  <xsl:choose>
	    <xsl:when test="title">
	      <xsl:value-of select="title"/>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:text>Privacy Act Regulations</xsl:text>
	    </xsl:otherwise>
	  </xsl:choose>
    <xsl:apply-templates select="@id"/>
	  <hr/>
  </h3>
  <xsl:for-each select="regulationsTitle">
		<xsl:call-template name="regulationsNode">
		  <xsl:with-param name="node" select="regulationsTitle"/>
		</xsl:call-template>
  </xsl:for-each>
  <p><a href="#top">[Return to TOP]</a></p>
</xsl:template>

<xsl:template match="subsection">
  <xsl:choose>
    <xsl:when test="@type='systemNameandNumber'">                  <!-- Mandatory -->
      <h4><i>SYSTEM NAME AND NUMBER:</i></h4>
    </xsl:when>
    <xsl:when test="@type='securityClassification'">
      <h4><i>SECURITY CLASSIFICATION:</i></h4>
    </xsl:when>
    <xsl:when test="@type='systemLocation'">
      <h4><i>SYSTEM LOCATION:</i></h4>
    </xsl:when>
    <xsl:when test="@type='categoriesOfIndividuals'">
      <h4><i>CATEGORIES OF INDIVIDUALS COVERED BY THE SYSTEM:</i></h4>
    </xsl:when>
    <xsl:when test="@type='categoriesOfRecords'">
      <h4><i>CATEGORIES OF RECORDS IN THE SYSTEM:</i></h4>
    </xsl:when>
    <xsl:when test="@type='authorityForMaintenance'">
      <h4><i>AUTHORITY FOR MAINTENANCE OF THE SYSTEM:</i></h4>
    </xsl:when>
    <xsl:when test="@type='purpose'">
      <h4><i>PURPOSE(S) OF THE SYSTEM:</i></h4>
    </xsl:when>
    <xsl:when test="@type='routineUsesOfRecords'">
      <h4><i>ROUTINE USES OF RECORDS MAINTAINED IN THE SYSTEM, INCLUDING CATEGORIES OF USERS AND THE PURPOSES OF SUCH USES:</i></h4>
    </xsl:when>
    <xsl:when test="@type='disclosureToConsumer'">
      <h4><i>DISCLOSURE TO CONSUMER REPORTING AGENCIES:</i></h4>
    </xsl:when>
    <xsl:when test="@type='policiesAndPractices'">
      <h4><i>POLICIES AND PRACTICE FOR STORING, RETRIEVING, ACCESSING, RETAINING, AND DISPOSING OF RECORDS IN THE SYSTEM:</i></h4>
    </xsl:when>
    <xsl:when test="@type='storage'">
      <h4><i>POLICIES AND PRACTICES FOR STORAGE OF RECORDS:</i></h4>
    </xsl:when>
    <xsl:when test="@type='retrievability'">
      <h4><i>POLICIES AND PRACTICES FOR RETRIEVAL OF RECORDS:</i></h4>
    </xsl:when>
    <xsl:when test="@type='safeguards'">
      <h4><i>ADMINISTRATIVE, TECHNICAL, AND PHYSICAL SAFEGUARDS:</i></h4>
    </xsl:when>
    <xsl:when test="@type='retentionAndDisposal'">
      <h4><i>POLICIES AND PRACTICES FOR RETENTION AND DISPOSAL OF RECORDS:</i></h4>
    </xsl:when>
    <xsl:when test="@type='systemManager'">
      <h4><i>SYSTEM MANAGER(S):</i></h4>
    </xsl:when>
    <xsl:when test="@type='notificationProcedure'">
      <h4><i>NOTIFICATION PROCEDURES:</i></h4>
    </xsl:when>
    <xsl:when test="@type='recordAccessProcedures'">
      <h4><i>RECORD ACCESS PROCEDURES:</i></h4>
    </xsl:when>
    <xsl:when test="@type='contestingRecordProcedures'">
      <h4><i>CONTESTING RECORD PROCEDURES:</i></h4>
    </xsl:when>
    <xsl:when test="@type='recordSourceCategories'">
      <h4><i>RECORD SOURCE CATEGORIES:</i></h4>
    </xsl:when>
    <xsl:when test="@type='exemptionsClaimed'">
      <h4><i>EXEMPTIONS PROMULGATED FOR THE SYSTEM:</i></h4>
    </xsl:when>
    <xsl:when test="@type='systemsExempted'">
      <h4><i>SYSTEMS EXEMPTED FROM CERTAIN PROVISIONS OF THE ACT:</i></h4>
    </xsl:when>
    <xsl:when test="@type='history'">
      <h4><i>HISTORY:</i></h4>
    </xsl:when>
  </xsl:choose>
  <xsl:choose>
    <xsl:when test="xhtmlContent">
      <xsl:apply-templates select="xhtmlContent/*"/>     
    </xsl:when>
    <xsl:otherwise>
      <p><xsl:value-of select="."/></p>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="routineUses" mode="body">
  <h2>
	<i>
	  <span style="font-weight:normal">
		<xsl:text>Statement of General Routine Uses</xsl:text>
	  </span>
	</i>
    <xsl:apply-templates select="@id"/>
  <hr/>
  </h2>
  <xsl:apply-templates select="xhtmlContent/*"/>
  <p><a href="#top">[Return to TOP]</a></p>
</xsl:template>

<xsl:template match="genericSubsection">
  <h4>
    <i>
      <xsl:value-of select="title"/>
    </i>
  </h4>
  <xsl:choose>
    <xsl:when test="xhtmlContent">
      <xsl:apply-templates select="xhtmlContent/*"/>     
    </xsl:when>
    <xsl:otherwise>
      <p><xsl:value-of select="."/></p>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="xhtmlContent/*">
  <xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>
