<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
   Name:  MIF2AFF.xsl
   Author:  Matthew J. McCready
   Date:  20100628
   Description:  This is the XSLT stylesheet that transforms a MIF (Metadata
                 Import Format document to the AFF (American Fact Finder)
                 format. 

   Modification History:
      Initials   Date      Change Req. ID    Description
      MMc      20100628    N/A               Initial Version 
      MMc      20200916    N/A               Modified the File to account for the fact that the document@lang attribute must be either "en" or "es"
                                              Eliminated the file extension for the document@id attribue.
                                              Removed the program-ref@context attribute.
                                              Inserted underscores for the survey-ref@id attribute.
                                              Inserted underscores for the dataset-ref@id attribute.
                                              Inserted underscores for the category-list/category-ref@id attribute
                                              Changed the value of the size-list/size@type attribute from "bytes" to "BYTES" 
-->
<xsl:stylesheet version="2.0" xmlns="http://factfinder.census.gov/2009/metadata" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:schemaLocation="http://factfinder.census.gov/2009/metadata">
	<xsl:output method="xml" indent="yes" encoding="ISO-8859-1"/>


	<xsl:template name="MIF" match="/">

		<xsl:element name="document">
			<xsl:attribute name="id">
				<xsl:copy>
                                     <xsl:variable name="FileId" select="//MIF/Metadata_File_Identifier"/>
                                          <xsl:value-of select="substring-before($FileId,'.')"/>
				</xsl:copy>
			</xsl:attribute>

			<xsl:attribute name="metada-model-version">0.11</xsl:attribute>
                       <!--   <xsl:attribute name="xsi:schemaLocation">http://factfinder.census.gov/2009/metadata</xsl:attribute> -->

                          
			<xsl:attribute name="lang">
				<xsl:copy>
                                          <xsl:variable name="Lang" select="//MIF/Metadata_Language"/>
                                   <xsl:value-of select="substring($Lang,0,3)"/>
				</xsl:copy>
			</xsl:attribute>

			<xsl:namespace name="xsi" select="'http://www.w3.org/2001/XMLSchema-instance'"/>

			<!-- program-ref -->

			<xsl:element name="program-ref">
				<xsl:attribute name="id">
				</xsl:attribute>
			</xsl:element>

			<!-- file-format-ref -->

			<xsl:if test="//MIF/File_Decompression_Technique">
				<xsl:element name="file-format-ref">
					<xsl:attribute name="id">PDF</xsl:attribute>
					<xsl:attribute name="context">global</xsl:attribute>

					<xsl:attribute name="archiver-type">None</xsl:attribute>

					<xsl:attribute name="compression-type">
						<xsl:for-each select="//MIF/File_Decompression_Technique">
							<xsl:copy>
								<xsl:apply-templates select="."/>
							</xsl:copy>
						</xsl:for-each>
					</xsl:attribute>
				</xsl:element>
			</xsl:if>

			<!-- media-format-ref -->

			<xsl:element name="media-format-ref">

				<xsl:attribute name="id">DOWNLOAD</xsl:attribute>
				<xsl:attribute name="context">global</xsl:attribute>
			</xsl:element>

			<!-- document-type-ref -->

			<xsl:element name="document-type-ref">
				<xsl:attribute name="id">MAP</xsl:attribute>
				<xsl:attribute name="context">global</xsl:attribute>
			</xsl:element>

			<!-- table-type-ref -->

			<xsl:element name="table-type-ref">
				<xsl:attribute name="id">
				</xsl:attribute>
				<xsl:attribute name="context">global</xsl:attribute>
				<xsl:attribute name="is-archive">false</xsl:attribute>
			</xsl:element>

			<!-- survey-ref -->

			<xsl:if test="//MIF/Survey_Name">
				<xsl:element name="survey-ref">
					<xsl:attribute name="id"> 

						 <xsl:copy>
                                                    <xsl:variable name ="SurveyRefId" select="//MIF/Survey_Name"/>
							<!-- <xsl:apply-templates select="//MIF/Survey_Name"/> -->
                                                      <xsl:variable name="PreSpaceSurveyRefId"  select="substring-before($SurveyRefId, ' ')"/>
                                                      <xsl:variable name="PostSpaceSurveyRefId"  select="substring-after($SurveyRefId, ' ')"/>
                                                      <xsl:value-of select="concat($PreSpaceSurveyRefId,'_',$PostSpaceSurveyRefId)"/>
						</xsl:copy>
				</xsl:attribute>
				</xsl:element>
			</xsl:if>

			<!-- data-origination-year -->

			<xsl:element name="data-origination-year">


					<xsl:copy>

						<xsl:variable name="pubDate" select="//MIF/Publication_Date [@section='citation']"/>

						<xsl:value-of select="substring($pubDate,1,4)"/>
					</xsl:copy>
			</xsl:element>

			<!-- dataset-list -->

			<xsl:element name="dataset-list">
				<xsl:element name="dataset-ref">
					<xsl:attribute name="id">
							<xsl:variable name="datasetRefId" select="normalize-space(//MIF/Survey_Instance_Name)"/>
                                                        <xsl:choose>
                                                         <xsl:when test="contains($datasetRefId,' ')">
                                                               <xsl:variable  name="datasetRefIdPre" select="substring-before($datasetRefId, ' ')"/>
                                                                <xsl:variable  name="datasetRefIdPost" select="substring-after($datasetRefId, ' ')"/> 
                                                                  
                                                                  <!-- Block 2 -->
                                                                  <xsl:choose>
                                                                       <xsl:when test="contains($datasetRefIdPost,' ')">
                                                                      <xsl:variable  name="datasetRefIdPostB" select="substring-before($datasetRefIdPost, ' ')"/>
                                                                       <xsl:variable  name="datasetRefIdPostC" select="substring-after($datasetRefIdPost, ' ')"/>
                                                                         <xsl:value-of select="concat($datasetRefIdPre,'_',$datasetRefIdPostB,'_',$datasetRefIdPostC)"/>
</xsl:when>
</xsl:choose> 
<!-- Block 2 End --> 
 </xsl:when>
                                                            <xsl:otherwise>
                                                               <xsl:value-of select="$datasetRefId"/>
                                                      </xsl:otherwise>
                                                     </xsl:choose>
					</xsl:attribute>
				</xsl:element>
			</xsl:element>

			<!-- code-list -->

			<xsl:element name="code-list">
				<xsl:element name="code">

					<xsl:attribute name="type">BOC</xsl:attribute>

					<xsl:copy>
						<xsl:apply-templates select="//MIF/Metadata_File_Identifier"/>
					</xsl:copy>
				</xsl:element>
			</xsl:element>

			<!-- display-label -->
			<xsl:element name="display-label">
					<xsl:copy>
						<xsl:apply-templates select="//MIF/Title [@section='citation']"/>
					</xsl:copy>
			</xsl:element>

			<!-- display-description-text -->
			<xsl:element name="display-description-text">
					<xsl:copy>
						<xsl:apply-templates select="//MIF/Abstract"/>
					</xsl:copy>
			</xsl:element>

			<!-- display-order-instructions-text -->

			<xsl:if test="//MIF/Ordering_Instructions">
				<xsl:element name="display-order-instructions-text">
						<xsl:copy>
							<xsl:apply-templates select="//MIF/Ordering_Instructions"/>
						</xsl:copy>
				</xsl:element>
			</xsl:if>


			<!-- location -->

			<xsl:if test="//MIF/Network_Resource_Name">
				<xsl:element name="location">
					<xsl:attribute name="is-external">false</xsl:attribute>

					<xsl:element name="url">
							<xsl:copy>
								<xsl:apply-templates select="//MIF/Network_Resource_Name"/>
							</xsl:copy>
					</xsl:element>

					<xsl:if test="//MIF/Metadata_File_Identifier">

						<xsl:element name="filename">
								<xsl:copy>
									<xsl:apply-templates select="//MIF/Metadata_File_Identifier"/>
								</xsl:copy>
						</xsl:element>
					</xsl:if>
				</xsl:element>
			</xsl:if>


			<!-- Release Date -->
			<xsl:if test="//MIF/Metadata_Date">
				<xsl:element name="release-date">

						<xsl:copy>
							<xsl:apply-templates select="//MIF/Metadata_Date"/>
						</xsl:copy>
				</xsl:element>
			</xsl:if>

			<!-- category-list -->
			<xsl:if test="//MIF/Series_Name  [@section='citation']">
				<xsl:element name="category-list">
					<xsl:element name="category-ref">

						<xsl:attribute name="id">
                                                   <xsl:variable name="CatRefId" select ="//MIF/Series_Name [@section='citation']"/>
                                                    <xsl:choose>

                                                     <xsl:when test="contains($CatRefId,' ') ">
                                                    <xsl:variable  name="CatRefIdPre" select="substring-before($CatRefId, ' ')"/>
                                                     <xsl:variable  name="CatRefIdPost" select="substring-after($CatRefId, ' ')"/>
                                                     
                                                     <xsl:choose>
                                                        <xsl:when test="contains($CatRefIdPost,' ') ">
                                                            <xsl:variable  name="CatRefIdPostA" select="substring-before($CatRefIdPost, ' ')"/>  
                                                          <xsl:variable  name="CatRefIdPostB" select="substring-after($CatRefIdPost, ' ')"/>
                                                             <xsl:choose>
                                                                <xsl:when test="contains($CatRefIdPostB,' ') ">
                                                                   <xsl:variable  name="CatRefIdPostC" select="substring-before($CatRefIdPostB, ' ')"/>
                                                                    <xsl:variable  name="CatRefIdPostD" select="substring-after($CatRefIdPostB, ' ')"/>
                                                                     <xsl:value-of select="concat($CatRefIdPre,'_',$CatRefIdPostA,'_',$CatRefIdPostC,'_',$CatRefIdPostD)"/> 
                                                                      </xsl:when>
                                                                        <xsl:otherwise>
                                                              <xsl:value-of select="concat($CatRefIdPre,'_',$CatRefIdPostA,'_',$CatRefIdPostB)"/>
                                                                </xsl:otherwise>
                                                               </xsl:choose>
                                                           </xsl:when>
                                                           <xsl:otherwise>
<xsl:copy>
 <xsl:value-of select="concat($CatRefIdPre,'_',$CatRefIdPost)"/> 
</xsl:copy>
 </xsl:otherwise>
 </xsl:choose>

 
                                                      </xsl:when>
                                                      <xsl:otherwise>
								<xsl:copy>
									<xsl:apply-templates select="$CatRefId"/>
								</xsl:copy>
                                                    </xsl:otherwise>
                                                    </xsl:choose>
						</xsl:attribute>

						<xsl:attribute name="dimension-id">
						</xsl:attribute>


						<xsl:attribute name="context">global</xsl:attribute>
					</xsl:element>
				</xsl:element>
			</xsl:if>

			<!-- subject-list -->

			<xsl:if test="//MIF/Subject_Entity">
				<xsl:element name="subject-list">
					<xsl:element name="subject-ref">

						<xsl:attribute name="id">
								<xsl:copy>
									<xsl:apply-templates select="//MIF/Subject_Entity"/>
								</xsl:copy>
						</xsl:attribute>

						<xsl:attribute name="context">global</xsl:attribute>
					</xsl:element>
				</xsl:element>
			</xsl:if>

			<!-- keyword-list -->
			<xsl:if test="//MIF/Theme_Keyword">
				<xsl:element name="keyword-list">

					<xsl:for-each select="//MIF/Theme_Keyword">
					<xsl:element name="keyword">
                                        <xsl:value-of select="."/>
                                         </xsl:element> 	
				</xsl:for-each>		


				</xsl:element>
			</xsl:if>

			<!-- size-list -->

			<xsl:element name="size-list">
				<xsl:element name="size">
 
					<xsl:attribute name="type">BYTES</xsl:attribute>

						<xsl:copy>
							<xsl:apply-templates select="//MIF/Transfer_Size"/>
						</xsl:copy>
				</xsl:element>
			</xsl:element>

			<!-- contact-ref -->
			<xsl:element name="contact-ref">
				<xsl:attribute name="id">
				</xsl:attribute>
				<xsl:attribute name="context">program</xsl:attribute>
			</xsl:element>



			<!-- closing root element and top tags -->
		</xsl:element>
	</xsl:template>
</xsl:stylesheet><!-- Stylus Studio meta-information - (c) 2004-2005. Progress Software Corporation. All rights reserved.
<metaInformation>
<scenarios/><MapperMetaTag><MapperInfo srcSchemaPathIsRelative="yes" srcSchemaInterpretAsXML="no" destSchemaPath="" destSchemaRoot="" destSchemaPathIsRelative="yes" destSchemaInterpretAsXML="no"/><MapperBlockPosition></MapperBlockPosition></MapperMetaTag>
</metaInformation>
-->
