Re: [xsl] Variable Scope

Subject: Re: [xsl] Variable Scope
From: Karl Stubsjoen <kstubs@xxxxxxxxx>
Date: Wed, 11 May 2005 13:12:07 -0700
Ok, Templates are:
validate_base.xsl  (base template)
validate_sped.xsl  (imports validate_base; includes incl_GradAge.xsl)
incl_GradeAge.xsl (included file with MAX_DATE problem)

(much code has been removed)
Also, below you'll find comments for the LIST with the following syntax:

<!-- LIST LOOK HERE:  ... -->

validate_base.xsl
========================================
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0"
	xmlns:sd="http://ade.az.gov/studentdetail";
	xmlns:dt="urn:schemas-microsoft-com:xslt">

<xsl:output method="xml" encoding="UTF-8"/>
<xsl:param name="STEP">1</xsl:param>

<!-- copy root -->
	<xsl:variable name="CURRENT" select="/"/>

<!-- global lookups -->
	<xsl:variable name="TRACE" select="/root/StudentDetail/@Trace" />
	<xsl:variable name="VERBOSE">
		<xsl:choose>
		<xsl:when test="string(/root/StudentDetail/@Verbose)"><xsl:value-of
select="/root/StudentDetail/@Verbose" /></xsl:when>
		<xsl:otherwise>1</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:variable name="FISCAL_YEAR" select="/root/StudentDetail/@FiscalYear" />
	<xsl:variable name="REGULAR_AZ_COUNTIES"
select="$sd_lookups//recordset[@rsName='Counties']/record[field[@fieldname='r
egular-az-county']
= '1']" />
	<xsl:variable name="PUBLIC_REFTYPES"
select="$sd_lookups//recordset[@rsName='RefTypes']/record[field[@fieldname='D
OR-Type']='PUBLIC']/field[@fieldname='RefTypeID']"/>
	<xsl:variable name="CHARTER_REFTYPES"
select="$sd_lookups//recordset[@rsName='RefTypes']/record[field[@fieldname='D
OR-Type']='CHARTER']/field[@fieldname='RefTypeID']"/>
	<xsl:variable name="NEEDS"
select="$sd_lookups//recordset[@rsName='NeedCodes']/record/field[@fieldname='
NeedCode']"/>

<!-- global constants -->
	<xsl:variable name="REFTYPE_ACCOMMODATION">1029</xsl:variable>
	<xsl:variable name="COUNTYTYPE_OUTOFSTATE">353</xsl:variable>
	<xsl:variable name="ACCOMMODATION">01</xsl:variable><!-- this is the
"T" value of CTDS -->
	<xsl:variable name="ELEMENTARY">04</xsl:variable><!-- this is the "T"
value of CTDS -->
	<xsl:variable name="HIGHSCHOOL">05</xsl:variable><!-- this is the "T"
value of CTDS -->
	<xsl:variable name="D_LEVEL_ACCOMMODATION">00</xsl:variable><!-- this
is the "D" value of CTDS -->

<!-- LIST LOOK HERE:  this is the global variable which loses scope -->
	<xsl:variable name="MAX_DATE">
		<xsl:call-template name="CreateNewDate">
			<xsl:with-param name="start_year" select="$FISCAL_YEAR" />
			<xsl:with-param name="start_month">06</xsl:with-param>
			<xsl:with-param name="start_day">30</xsl:with-param>
		</xsl:call-template>
	</xsl:variable>

	<!-- DOA -->
	<xsl:variable name="DOA_REFTYPE"
select="root/recordset[@rsName='EntityOwnerInfo']/record/field[@fieldname='Re
fTypeID']"
/>
	<xsl:variable name="DOA_ENTITYID"
select="root/recordset[@rsName='EntityOwnerInfo']/record/field[@fieldname='En
tityID']"
/>
	<xsl:variable name="DOA_CTDS"
select="root/recordset[@rsName='EntityOwnerInfo']/record/field[@fieldname='CT
DS']"
/>
	<xsl:variable name="DOA_COUNTY"
select="root/recordset[@rsName='EntityOwnerInfo']/record/field[@fieldname='Re
fCountyID']"
/>

	<!-- ENTITY -->
	<xsl:variable name="ENTITY_NATURE"
select="root/recordset[@rsName='EntityInfo']/record/field[@fieldname='Nature'
]"
/>
	<xsl:variable name="ENTITY_REFTYPE"
select="root/recordset[@rsName='EntityInfo']/record/field[@fieldname='RefType
ID']"
/>
	<xsl:variable name="ENTITY_ENTITYID"
select="root/recordset[@rsName='EntityInfo']/record/field[@fieldname='EntityI
D']"
/>
	<xsl:variable name="ENTITY_CTDS"
select="root/recordset[@rsName='EntityInfo']/record/field[@fieldname='CTDS']"
/>
	<xsl:variable name="ENTITY_COUNTY"
select="root/recordset[@rsName='EntityInfo']/record/field[@fieldname='RefCoun
tyID']"
/>

<!--
	resulting xml source should resemble
    	<results>
        	<result rule="name of rule" pass="1 | 0"  1 = pass, 2 = fail
| non-existent may not be tested">

            </result>
        </results>

-->


<xsl:template match="/">
	<xsl:element name="results">

	<xsl:choose>
		<xsl:when test="$STEP=1">

<!-- LIST LOOK HERE:  this is the default step to follow (and only
implemented step so far) -->
			<xsl:call-template name="STEP_1"/>

		</xsl:when>
	<!-- .. code removed -->

	</xsl:choose>

	</xsl:element>
</xsl:template>

	<!-- .. code removed -->

<xsl:template name="calculatestudentage">
	<xsl:param name="student_dob"/>
	<xsl:param name="date_of_occurrence"/>

	<xsl:choose>
		<xsl:when test="not(string($student_dob)) or
not(string($date_of_occurrence))">
			<xsl:text>-1</xsl:text>
		</xsl:when>
		<xsl:otherwise>
			<xsl:variable name="Yr1" select="dt:format-date($student_dob,'yyyy')"/>
			<xsl:variable name="Yr2"
select="dt:format-date($date_of_occurrence,'yyyy')"/>
			<xsl:variable name="MoDa1"
select="concat(dt:format-date($student_dob,'MM'),dt:format-date($student_dob,
'dd'))"/>
			<xsl:variable name="MoDa2"
select="concat(dt:format-date($date_of_occurrence,'MM'),dt:format-date($date_
of_occurrence,'dd'))"/>

			<!-- year diff calculation -->
			<xsl:variable name="sYrDiff" select="$Yr2 - $Yr1"/>

			<!-- final age calculation consider month and day of birth -->
			<xsl:variable name="student_age">
				<xsl:choose>
					<xsl:when test="$MoDa1 &gt; $MoDa2">
						<xsl:value-of select="$sYrDiff - 1"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$sYrDiff"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:variable>

		<!-- return result -->
			<xsl:value-of select="$student_age"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- LIST LOOK HERE:  if you stumble upon this, I could use help with
a better date adder function : ) -->
<xsl:template name="fn_date_add">
	<xsl:param name="current_date"/>
	<xsl:param name="days"/>

	<xsl:variable name="Yr" select="dt:format-date($current_date,'yyyy')"/>
	<xsl:variable name="Mo" select="dt:format-date($current_date,'MM')"/>
	<xsl:variable name="Da"
select="number(dt:format-date($current_date,'dd')) + $days"/>

	<xsl:value-of select="concat($Yr, '-', $Mo, '-', $Da)"/>

</xsl:template>



</xsl:stylesheet>
========================================

validate_sped.xsl
========================================
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0"
	xmlns:sd="http://ade.az.gov/studentdetail";
	xmlns:fx="urn:schemas-microsoft-com:xslt">

	<xsl:import href="../../ValidationTemplates/validate_base.xsl"/>

<!-- LIST LOOK HERE:  this is the included file which references
MAX_DATE, look below for execution/flow -->
	<xsl:include href="SPED/fn_Concurrency.xsl"/>


	<!-- <xsl:include href="SupportPrograms/incl_JohnsonOMalley.xsl" /> -->

	<xsl:include href="SPED/incl_DistrictOfAttendance.xsl"/>
	<xsl:include href="SPED/incl_SchoolLevelCheck.xsl"/>
	<xsl:include href="SPED/incl_DistrictOfResidence.xsl"/>
	<xsl:include href="SPED/incl_CED-DOA-DOR.xsl"/>
	<xsl:include href="SPED/incl_SPEDServiceCode.xsl"/>
	<xsl:include href="SPED/incl_NeedCodes.xsl"/>

	<xsl:include href="SPED/incl_ArizonaSchoolDeafBlind.xsl"/>
	<xsl:include href="SPED/incl_SPEDDors.xsl"/>

<!-- LIST LOOK HERE:  this is the included file which call the above
included file and eventually has trouble with MAX_DATE -->
	<xsl:include href="SPED/incl_GradeAge.xsl"/>

	<xsl:include href="SPED/incl_OverlappingServicesForSPED.xsl"/>
	<xsl:include href="SPED/incl_SPEDMultiGradeService.xsl"/>
	<xsl:include href="SPED/incl_SPEDExitReason.xsl"/>
	<xsl:include href="SPED/incl_SynchronizeSpedGrade.xsl"/>

	<xsl:variable name="sd_rules" select="document('SPED/SPED_rules.xml')"/>
	<xsl:variable name="sd_lookups" select="document('SPED/SPED_lookups.xml')"/>
	<xsl:variable name="sd_matrix" select="document('SPED/SPED_matrix.xml')"/>

<!--
SPED INTEGRITY VALIDATION

-->

<xsl:template name="STEP_1">
<!-- LIST LOOK HERE:  entry template rule called from validate_base.xsl -->


	<!-- TODO's -->
		<xsl:element name="TODO">3.1.9 CTD's 000400 and 000500 not
valid</xsl:element>
		<xsl:element name="TODO">3.1 (10) Each school approved to teach
student grade</xsl:element>
		<xsl:element name="NOT_IMPLEMENTED">Rule 3.1.12.12 pending business
rule analysis</xsl:element>


	<!--
		Rule 3.1
		Student Counts Needs Rules	-->

			<!-- 3. District of Attendance -->
			<!-- 4. School -->
			<!-- 5. District of Residence -->
			<!-- 6. CEC/DOA/DOR Valid -->
			<!-- 7, 8 -->
			<!-- 11. SPED Service Code -->
			<!-- 12. Valid Need Code characteristics -->
			<!-- 13, 14, 15 -->
			<!-- 16 -->

<!-- LIST LOOK HERE:  this is the call to incl_GradeAge.xsl -->
			<!-- 17. Age -->
					<xsl:call-template name="GradeAge"/>

	<!--
		Rule 3.2
		SPED DORs	-->
	<!--
		Rule 3.3
		Overlapping Services for a SPED Need	-->
	<!--
		Rule 3.4
		Multiple Grades or Services	-->
	<!--
		Rule 3.5
		Initial IEP
	<!--
		Rule 3.6
		SPED Exit Reason/Age/Grade Validation	-->
	<!--
		Rule 3.7
		Synchronize SPED Grade / Membership Grade	-->

</xsl:template>

</xsl:stylesheet>
========================================

incl_GradeAge.xsl
========================================
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0" xmlns:sd="http://ade.az.gov/studentdetail";
	xmlns:fx="urn:schemas-microsoft-com:xslt"
	xmlns:fn="urn:function-element-results"
	exclude-result-prefixes="sd fx fn">

	<xsl:variable name="DORExceptions"
select="$sd_lookups//recordset[@rsName='RefTypes']/record[field[@fieldname='v
alidates-GradeDOR']='1']/field[@fieldname='RefTypeID']"
/>
	<xsl:variable name="SPEDDor"
select="/root/recordset[@rsName='SPEDDor']/record[field[@fieldname='Residence
RefTypeID'
and not(.= $DORExceptions)]]" />
	<xsl:variable name="AllowedGradeID"
select="$sd_lookups//recordset[@rsName='GradeLookup']/record[field[@fieldname
='Grade'
and (.= 'KG' or .= 'PS' or .= 'UE')]]/field[@fieldname='GradeID']" />
	<xsl:variable name="UE_Grade_ID"
select="$sd_lookups//recordset[@rsName='GradeLookup']/record[field[@fieldname
='Grade']
= 'UE']/field[@fieldname='GradeID']" />
	<xsl:variable name="PS_Grade_ID"
select="$sd_lookups//recordset[@rsName='GradeLookup']/record[field[@fieldname
='Grade']
= 'PS']/field[@fieldname='GradeID']" />
	<xsl:variable name="Group_B_Need_Codes"
select="$sd_lookups//recordset[@rsName='NeedCodes']/record[field[@fieldname='
validates-GradeAge']='1']/field[@fieldname='NeedCode']"
/>
	<xsl:variable name="KG_Grade_ID"
select="$sd_lookups//recordset[@rsName='GradeLookup']/record[field[@fieldname
='Grade']
= 'KG']/field[@fieldname='GradeID']" />

<!-- LIST LOOK HERE:  this variable is a result of this call-template
in fn_Concurrency.xsl and is the template which has trouble with the
MAX_DATE global -->
	<xsl:variable name="Group_B_Needs">
		<xsl:call-template name="get_needs2">
			<xsl:with-param name="need_code_match" select="$Group_B_Need_Codes" />
			<xsl:with-param name="SORTBYNEEDID">0</xsl:with-param>
		</xsl:call-template>
	</xsl:variable>

	<xsl:template name="GradeAge">
<!-- LIST LOOK HERE:  entry template rule called from validate_sped.xsl -->

<!-- .. code removed .. -->

		<!-- Rule: 3.1.17.5
[field[@fieldname='NeedCode']=$Group_B_Need_Codes/field[@fieldname='NeedCode'
]]-->
	<xsl:element name="NOT_IMPLEMENTED">Complex rule - 3.1.17.5: SPED
Grade UE &amp; Group B</xsl:element>
	<!-- skip if student doesn't not have a Grade of UE -->
	<xsl:variable name="recent_membership"
select="/root/recordset[@rsName='SchoolMemberships']/record[last()]/field[@fi
eldname='Id']"
/>
	<xsl:if
test="/root/recordset[@rsName='Grades']/record[field[@fieldname='SchoolMember
shipID']=$recent_membership]/field[@fieldname='RefGradeID']=
$UE_Grade_ID">
		<xsl:call-template name="SPEDUEorKG" />
	</xsl:if>

<!-- .. code removed .. -->

	</xsl:template>


<!-- .. code removed .. -->



</xsl:stylesheet>
========================================

..end of code



On 5/11/05, Michael Kay <mike@xxxxxxxxxxxx> wrote:
> >
> > I could post actual code, it's a lot though.
>
> That's why I asked you to cut it down.
>
> Sometimes this not only makes it easier for other people to find your
> problem, it makes it easier for you to find it too.
>
> Michael Kay
> http://www.saxonica.com/
>
> >
> > On 5/11/05, Michael Kay <mike@xxxxxxxxxxxx> wrote:
> > > Perhaps you could put together a simple 3-module example
> > that illustrates
> > > the problem.
> > >
> > > From what you've shown us, it looks as if the variable
> > should be in scope.
> > > Once we've seen and checked your code, the next question is
> > which processor
> > > you are using.
> > >
> > > Michael Kay
> > > http://www.saxonica.com/
> > >
> > > > -----Original Message-----
> > > > From: Karl Stubsjoen [mailto:kstubs@xxxxxxxxx]
> > > > Sent: 11 May 2005 17:38
> > > > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> > > > Subject: [xsl] Variable Scope
> > > >
> > > > Hello,
> > > >
> > > > So far I have not ran into any troubles with variable
> > scope, and I am
> > > > refering to globabl variable scope - variables defined
> > within my base
> > > > template.  I am having trouble now with a MAX_DATE variable I have
> > > > defined.  So, I am wondering about execution plan and where/when
> > > > variables are available.  Given the following
> > inclusiong/import plan I
> > > > will attempt to describe where it appears that
> > MAX_DATE(s) scope is
> > > > out-of-scope.
> > > >
> > > > base_template.xsl
> > > >   MAX_DATE is defined (see definition below)
> > > >
> > > > working_template_a.xsl
> > > >   {imports} base_template
> > > >   {includes} working_template_b.xsl
> > > >   {includes} working_template_c.xsl
> > > >   {includes} working_template_d.xsl
> > > >   {includes} ... additional working templates ..
> > > >
> > > > MAX_DATE inside of any template definition inside of any
> > included file
> > > > above is fine.  MAX_DATE used as part of a variable
> > definition at the
> > > > top (outside a template rule) of any of these include
> > files throws the
> > > > error:  "The variable or param MAX_DATE is either not
> > defined or it is
> > > > out of scope".
> > > >
> > > > Any ideas on this?
> > > >
> > > >
> > > > Note:  CreateNewDate template is defined in base_template.xsl
> > > > <xsl:variable name="MAX_DATE">
> > > > <xsl:call-template name="CreateNewDate">
> > > >   <xsl:with-param name="start_year" select="$FISCAL_YEAR" />
> > > >   <xsl:with-param name="start_month">06</xsl:with-param>
> > > >   <xsl:with-param name="start_day">30</xsl:with-param>
> > > > </xsl:call-template>
> > > > </xsl:variable>

Current Thread