<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
							  xmlns:exslt="http://exslt.org/common "
                              xmlns:func="http://exslt.org/functions"
							  xmlns:edate="http://exslt.org/dates-and-times"
                              exclude-result-prefixes="edate">
	<xsl:output method="xml"/>
	<!-- ***************************************************************-->
	<!--<xsl:param name="m_DataStartDate"/>
	<xsl:param name="m_DataEndDate"/> -->
	<xsl:param name="m_DataStartDate">7/1/2004 12:00:00 AM </xsl:param>
	<xsl:param name="m_DataEndDate">6/30/2005 11:59:59 PM </xsl:param>
	<xsl:variable name="warning">1</xsl:variable>
	<xsl:variable name="error">0</xsl:variable>
	<xsl:variable name="number">1</xsl:variable>
	<xsl:variable name="date">2</xsl:variable>
	<!-- ***************************************************************-->
	<xsl:template match="/">
		<ROOT>
			<xsl:choose>
				<xsl:when test="ROOT/PIF">
					<xsl:apply-templates select="ROOT/PIF"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:call-template name="ErrorHandling">
						<xsl:with-param name="isWarning">0</xsl:with-param>
						<xsl:with-param name="errorDesc">The Record Type Does Not Match the File Type.</xsl:with-param>
					</xsl:call-template>
				</xsl:otherwise>
			</xsl:choose>
		</ROOT>
	</xsl:template>
	<xsl:template match="PIF">
		<PIF>
			<xsl:copy-of select="INTERNAL_ID"/>
			<!--Start Testing PIFDATE-->
			<xsl:for-each select="PIFDATE">
				<xsl:call-template name="ValidateData">
					<xsl:with-param name="testDataType">
						<xsl:value-of select="$date"/>
					</xsl:with-param>
					<xsl:with-param name="warnFlag">
						<xsl:value-of select="$warning"/>
					</xsl:with-param>
				</xsl:call-template>
			</xsl:for-each>
			<!--End Testing PIFDATE-->
			<!--Start Testing AETC-->
			<!--First make sure an AETC node exists-->
			<xsl:call-template name="TestRequiredField">
				<xsl:with-param name="fieldName">AETC</xsl:with-param>
			</xsl:call-template>
			<!--Now test for valid data-->
			<xsl:apply-templates select="AETC"/>
			<!--End Testing AETC-->
		</PIF>
	</xsl:template>
	<!-- **************************SharedXSLT data*************************************-->
	<xsl:template name="TestRequiredField">
		<xsl:param name="fieldName"/>
		<!-- Unless user passes in a value for parameter isWarningType, then assume it's an error
			if a required field is missing.-->
		<xsl:param name="isWarningType">
			<xsl:value-of select="$error"/>
		</xsl:param>
		<xsl:if test="not(child::node()[name(.) = $fieldName])">
			<!--Because template "ErrorHandling" doesn't work at the level of the PIF/PR/ICC... element node, but rather
			 at the level of its child nodes, we will go down to the second child node of the PIF/PR/ICC... node 
			 before calling the template.  NOTE we go to the SECOND child node so that we can get the value
			 of INTERNAL_ID too, which is referenced in "ErrorHandling" template as PRECEDING::node(), since
			 INTERNAL_ID is the first node in every one of the xml documents.-->
			<xsl:for-each select="child::node()[2]">
				<xsl:call-template name="ErrorHandling">
					<xsl:with-param name="isWarning">
						<xsl:value-of select="$isWarningType"/>
					</xsl:with-param>
					<xsl:with-param name="questionNum">
						<xsl:value-of select="$fieldName"/>
					</xsl:with-param>
					<xsl:with-param name="valueEntered"/>
					<xsl:with-param name="errorDesc">
						<xsl:value-of select="$fieldName"/> is a required field.</xsl:with-param>
					<xsl:with-param name="recID">
						<xsl:call-template name="CreateRecID"/>
					</xsl:with-param>
				</xsl:call-template>
			</xsl:for-each>
		</xsl:if>
	</xsl:template>
	<xsl:template name="ErrorHandling">
		<xsl:param name="isWarning">
			<xsl:value-of select="1"/>
		</xsl:param>
		<xsl:param name="questionNum"/>
		<xsl:param name="valueEntered"/>
		<xsl:param name="errorDesc"/>
		<xsl:param name="recID"/>
		<ERROR>
			<WARNING_FLAG>
				<xsl:value-of select="$isWarning"/>
			</WARNING_FLAG>
			<xsl:if test="$questionNum">
				<DATA_ELEMENT>
					<xsl:value-of select="$questionNum"/>
				</DATA_ELEMENT>
			</xsl:if>
			<xsl:if test="$valueEntered">
				<VALUE>
					<xsl:value-of select="$valueEntered"/>
				</VALUE>
			</xsl:if>
			<ERROR_DESCRIPTION>
				<xsl:value-of select="$errorDesc"/>
			</ERROR_DESCRIPTION>
			<xsl:if test="$recID">
				<RECORD_ID>
					<xsl:value-of select="$recID"/>
				</RECORD_ID>
			</xsl:if>
			<INTERNAL_ID>
				<xsl:value-of select="preceding-sibling::INTERNAL_ID"/>
			</INTERNAL_ID>
		</ERROR>
	</xsl:template>
	<xsl:template name="CreateRecID">
		<xsl:choose>
			<xsl:when test="/ROOT/PIF">
				<xsl:choose>
					<xsl:when test="name(.) = 'PIF_ID'">
						<xsl:value-of select="text()"/>
					</xsl:when>
					<xsl:when test="preceding-sibling::PIF_ID">
						<xsl:value-of select="preceding-sibling::PIF_ID"/>
					</xsl:when>
					<xsl:when test="following-sibling::PIF_ID">
						<xsl:value-of select="following-sibling::PIF_ID"/>
					</xsl:when>
				</xsl:choose>
			</xsl:when>
		</xsl:choose>
	</xsl:template>
	<xsl:template name="CreateChildElement">
		<xsl:param name="childElementName"/>
		<xsl:choose>
			<xsl:when test="substring($childElementName, string-length($childElementName), 1) = '_'">
				<xsl:element name="{substring($childElementName, 1, string-length($childElementName)-1)}">
					<xsl:attribute name="LKUP_ID"><xsl:value-of select="substring-after(name(), $childElementName)"/></xsl:attribute>
					<xsl:attribute name="VALUE"><xsl:value-of select="."/></xsl:attribute>
				</xsl:element>
			</xsl:when>
			<xsl:otherwise>
				<xsl:element name="{$childElementName}">
					<xsl:attribute name="LKUP_ID"><xsl:value-of select="substring-after(name(), $childElementName)"/></xsl:attribute>
					<xsl:attribute name="VALUE"><xsl:value-of select="."/></xsl:attribute>
				</xsl:element>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template name="ValidateData">
		<xsl:param name="testDataType"/>
		<xsl:param name="testDataLength"/>
		<xsl:param name="testDataRange"/>
		<xsl:param name="warnFlag"/>
		<xsl:param name="childNodeTrunc"/>
		<xsl:choose>
			<xsl:when test="$testDataType">
				<xsl:choose>
					<!--Conduct test for verifying valid number if testDataType = $number
						  AND also if text() value trimmed of extra white spaces has at least one character.
						  This second test is necessary because in SPSS files, values may be a string of
                    white space characters when the value has not been entered (e.g. PIF6 which is zipcode),
						  and if we test this string of white spaces to see if it's a number, of course the result
						  will be 'NaN'.-->
					<xsl:when test="$testDataType = $number and string-length(normalize-space(text())) > 0">
						<xsl:choose>
							<xsl:when test="string(number(text())) != 'NaN'">
								<xsl:choose>
									<xsl:when test="string-length($testDataLength) or string-length($testDataRange)">
										<xsl:call-template name="ValidateData">
											<xsl:with-param name="testDataType"/>
											<xsl:with-param name="testDataLength" select="$testDataLength"/>
											<xsl:with-param name="testDataRange" select="$testDataRange"/>
											<xsl:with-param name="warnFlag">
												<xsl:value-of select="$warnFlag"/>
											</xsl:with-param>
											<xsl:with-param name="childNodeTrunc">
												<xsl:value-of select="$childNodeTrunc"/>
											</xsl:with-param>
										</xsl:call-template>
									</xsl:when>
									<xsl:otherwise>
										<xsl:choose>
											<xsl:when test="string-length($childNodeTrunc)">
												<xsl:call-template name="CreateChildElement">
													<xsl:with-param name="childElementName">
														<xsl:value-of select="$childNodeTrunc"/>
													</xsl:with-param>
												</xsl:call-template>
											</xsl:when>
											<xsl:otherwise>
												<xsl:copy-of select="."/>
											</xsl:otherwise>
										</xsl:choose>
									</xsl:otherwise>
								</xsl:choose>
							</xsl:when>
							<xsl:otherwise>
								<xsl:call-template name="ErrorHandling">
									<xsl:with-param name="isWarning">
										<xsl:value-of select="$warnFlag"/>
									</xsl:with-param>
									<xsl:with-param name="questionNum">
										<xsl:value-of select="name(.)"/>
									</xsl:with-param>
									<xsl:with-param name="valueEntered">
										<xsl:value-of select="text()"/>
									</xsl:with-param>
									<xsl:with-param name="errorDesc">The answer must be numeric.</xsl:with-param>
									<xsl:with-param name="recID">
										<xsl:call-template name="CreateRecID"/>
									</xsl:with-param>
								</xsl:call-template>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:when>
					<xsl:when test="$testDataType = $date"> 
						<xsl:variable name="dateField" select="edate:parse-date(text(), 'M/d/yyyy hh:mm:ss tt')"/>
						<xsl:variable name="startDate" select="edate:parse-date($m_DataStartDate, 'M/d/yyyy hh:mm:ss tt')"/>
						<xsl:variable name="endDate" select="edate:parse-date($m_DataEndDate, 'M/d/yyyy hh:mm:ss tt')"/>
						<xsl:choose>
							<xsl:when test="string-length($dateField) != 0">
								<xsl:choose>
									<!--Make sure the date falls within the data collection period, as indicated by the
											 two parameters m_DataStartDate and m_DataEndDate that are passed
											 to this xslt as arguments.  m_DataStartDate and m_DataEndDate are parsed into 
											 the correct format as variables startDate and endDate respectively.
											 NOTE - edate:difference returns a value of datatype duration,  and will be positive if first date param occurs before 
													  the second date. So we'll just check to see if the duration value returned starts with the negative sign or not.-->
									<xsl:when test="substring(edate:difference($startDate, $dateField), 1, 1) != '-' and substring(edate:difference($dateField, $endDate), 1, 1) != '-'">
										<xsl:choose>
											<xsl:when test="string-length($testDataLength) or string-length($testDataRange)">
												<xsl:call-template name="ValidateData">
													<xsl:with-param name="testDataType"/>
													<xsl:with-param name="testDataLength" select="$testDataLength"/>
													<xsl:with-param name="testDataRange" select="$testDataRange"/>
													<xsl:with-param name="warnFlag">
														<xsl:value-of select="$warnFlag"/>
													</xsl:with-param>
													<xsl:with-param name="childNodeTrunc">
														<xsl:value-of select="$childNodeTrunc"/>
													</xsl:with-param>
												</xsl:call-template>
											</xsl:when>
											<xsl:otherwise>
												<xsl:copy-of select="."/>
											</xsl:otherwise>
										</xsl:choose>
									</xsl:when>
									<xsl:otherwise>
										<xsl:call-template name="ErrorHandling">
											<xsl:with-param name="isWarning">
												<xsl:value-of select="$warnFlag"/>
											</xsl:with-param>
											<xsl:with-param name="questionNum">
												<xsl:value-of select="name(.)"/>
											</xsl:with-param>
											<xsl:with-param name="valueEntered">
												<xsl:value-of select="text()"/>
											</xsl:with-param>
											<xsl:with-param name="errorDesc">The date is not in the valid range for the Data Collection Period.</xsl:with-param>
											<xsl:with-param name="recID">
												<xsl:call-template name="CreateRecID"/>
											</xsl:with-param>
										</xsl:call-template>
									</xsl:otherwise>
								</xsl:choose>
							</xsl:when>
							<xsl:otherwise>
								<xsl:call-template name="ErrorHandling">
									<xsl:with-param name="isWarning">
										<xsl:value-of select="$warnFlag"/>
									</xsl:with-param>
									<xsl:with-param name="questionNum">
										<xsl:value-of select="name(.)"/>
									</xsl:with-param>
									<xsl:with-param name="valueEntered">
										<xsl:value-of select="text()"/>
									</xsl:with-param>
									<xsl:with-param name="errorDesc">The answer must be a valid date in format mm/dd/yy.</xsl:with-param>
									<xsl:with-param name="recID">
										<xsl:call-template name="CreateRecID"/>
									</xsl:with-param>
								</xsl:call-template>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:when>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="string-length($testDataLength) &gt; 0">
				<!--Before testing the lenght of data, we will remove leading and trailing white spaces,
					and repeating internal white spaces are replaced with a single white space.  This
					is needed, for example, because character data from SPSS files is often padded with white
					spaces.
					To achieve this, we will wrap the 'text()' segment of $testDataLength with the 
					normalize-space() xpath function-->
				<xsl:variable name="normalizedTestLength" select="string-length(normalize-space(text())) = $testDataLength"/>
				<xsl:choose>
					<xsl:when test="$normalizedTestLength">
						<xsl:choose>
							<xsl:when test="string-length($testDataRange)">
								<xsl:call-template name="ValidateData">
									<xsl:with-param name="testDataType"/>
									<xsl:with-param name="testDataLength"/>
									<xsl:with-param name="testDataRange" select="$testDataRange"/>
									<xsl:with-param name="warnFlag">
										<xsl:value-of select="$warnFlag"/>
									</xsl:with-param>
									<xsl:with-param name="childNodeTrunc">
										<xsl:value-of select="$childNodeTrunc"/>
									</xsl:with-param>
								</xsl:call-template>
							</xsl:when>
							<xsl:otherwise>
								<xsl:choose>
									<xsl:when test="string-length($childNodeTrunc)">
										<xsl:call-template name="CreateChildElement">
											<xsl:with-param name="childElementName">
												<xsl:value-of select="$childNodeTrunc"/>
											</xsl:with-param>
										</xsl:call-template>
									</xsl:when>
									<xsl:otherwise>
										<xsl:copy-of select="."/>
									</xsl:otherwise>
								</xsl:choose>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:when>
					<xsl:otherwise>
						<xsl:call-template name="ErrorHandling">
							<xsl:with-param name="isWarning">
								<xsl:value-of select="$warnFlag"/>
							</xsl:with-param>
							<xsl:with-param name="questionNum">
								<xsl:value-of select="name(.)"/>
							</xsl:with-param>
							<xsl:with-param name="valueEntered">
								<xsl:value-of select="text()"/>
							</xsl:with-param>
							<xsl:with-param name="errorDesc">The answer is not of the correct length.</xsl:with-param>
							<xsl:with-param name="recID">
								<xsl:call-template name="CreateRecID"/>
							</xsl:with-param>
						</xsl:call-template>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="string-length($testDataRange) &gt; 0">
				<xsl:choose>
					<xsl:when test="$testDataRange">
						<xsl:choose>
							<xsl:when test="string-length($childNodeTrunc)">
								<xsl:call-template name="CreateChildElement">
									<xsl:with-param name="childElementName">
										<xsl:value-of select="$childNodeTrunc"/>
									</xsl:with-param>
								</xsl:call-template>
							</xsl:when>
							<xsl:otherwise>
								<xsl:copy-of select="."/>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:when>
					<xsl:otherwise>
						<xsl:call-template name="ErrorHandling">
							<xsl:with-param name="isWarning">
								<xsl:value-of select="$warnFlag"/>
							</xsl:with-param>
							<xsl:with-param name="questionNum">
								<xsl:value-of select="name(.)"/>
							</xsl:with-param>
							<xsl:with-param name="valueEntered">
								<xsl:value-of select="text()"/>
							</xsl:with-param>
							<xsl:with-param name="errorDesc">The answer is not in the correct range.</xsl:with-param>
							<xsl:with-param name="recID">
								<xsl:call-template name="CreateRecID"/>
							</xsl:with-param>
						</xsl:call-template>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
		</xsl:choose>
	</xsl:template>
	<xsl:template match="AETC">
		<!--Valid AETC Numbers are:  1, 2, 4, 5, 6, 7, 8, 10, 11, 12, 13, 15, 16-->
		<xsl:call-template name="ValidateData">
			<xsl:with-param name="testDataType">
				<xsl:value-of select="$number"/>
			</xsl:with-param>
			<xsl:with-param name="testDataRange" select="number(text()) = 1 or number(text()) = 2 or (number(text()) &gt;= 4 and number(text()) &lt;=8) or (number(text()) &gt;= 10 and number(text()) &lt;=13) or number(text()) = 15 or number(text()) = 16"/>
			<xsl:with-param name="warnFlag">
				<xsl:value-of select="$error"/>
			</xsl:with-param>
		</xsl:call-template>
	</xsl:template>
	<xsl:template match="SUBSITE">
		<xsl:call-template name="ValidateData">
			<xsl:with-param name="testDataType">
				<xsl:value-of select="$number"/>
			</xsl:with-param>
			<xsl:with-param name="testDataRange" select="number(text()) &gt;= 0 and number(text()) &lt; 1000"/>
			<xsl:with-param name="warnFlag">
				<xsl:value-of select="$error"/>
			</xsl:with-param>
		</xsl:call-template>
	</xsl:template>
	<xsl:template name="PROG_ID">
		<xsl:call-template name="ValidateData">
			<xsl:with-param name="testDataType">
				<xsl:value-of select="$number"/>
			</xsl:with-param>
			<xsl:with-param name="testDataRange" select="number(text()) &gt;= 0 and number(text()) &lt; 100000000"/>
			<xsl:with-param name="warnFlag">
				<xsl:value-of select="$error"/>
			</xsl:with-param>
		</xsl:call-template>
	</xsl:template>
	<!-- ***********************************************************************************-->
</xsl:stylesheet>
<!-- Stylus Studio meta-information - (c) 2004-2007. Progress Software Corporation. All rights reserved.

<metaInformation>
	<scenarios>
		<scenario default="yes" name="Scenario1" userelativepaths="yes" externalpreview="no" url="TestingXML.xml" htmlbaseurl="" outputurl="" processortype="saxon8" useresolver="yes" profilemode="0" profiledepth="" profilelength="" urlprofilexml=""
		          commandline="" additionalpath="" additionalclasspath="" postprocessortype="none" postprocesscommandline="" postprocessadditionalpath="" postprocessgeneratedext="" validateoutput="no" validator="internal" customvalidator="">
			<advancedProp name="sInitialMode" value=""/>
			<advancedProp name="bXsltOneIsOkay" value="true"/>
			<advancedProp name="bSchemaAware" value="true"/>
			<advancedProp name="bXml11" value="false"/>
			<advancedProp name="iValidation" value="0"/>
			<advancedProp name="bExtensions" value="true"/>
			<advancedProp name="iWhitespace" value="0"/>
			<advancedProp name="sInitialTemplate" value=""/>
			<advancedProp name="bTinyTree" value="true"/>
			<advancedProp name="bWarnings" value="true"/>
			<advancedProp name="bUseDTD" value="false"/>
			<advancedProp name="iErrorHandling" value="fatal"/>
		</scenario>
	</scenarios>
	<MapperMetaTag>
		<MapperInfo srcSchemaPathIsRelative="yes" srcSchemaInterpretAsXML="no" destSchemaPath="" destSchemaRoot="" destSchemaPathIsRelative="yes" destSchemaInterpretAsXML="no"/>
		<MapperBlockPosition></MapperBlockPosition>
		<TemplateContext></TemplateContext>
		<MapperFilter side="source"></MapperFilter>
	</MapperMetaTag>
</metaInformation>
-->