<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
	version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:msxsl="urn:schemas-microsoft-com:xslt"
	xmlns:xz="urn:frontpage:ddw:xsltgen"
	xmlns:out="urn:frontpage:ddw:xsltout"
	xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime">

	<xsl:param name="fldTypes" select="document('fldtypes.xml')"/>
	<xsl:param name="viewName">{76853A94-4A67-4336-AF12-9C751A25AEA1}</xsl:param>
	<xsl:param name="formType">EditForm</xsl:param>
	<xsl:param name="isAViewAndNotAForm">1</xsl:param>
	<xsl:param name="isThreaded"></xsl:param>
	<xsl:param name="FreeForm">0</xsl:param>
	
	<xsl:variable name="generalDisplayPattern">
		<xsl:choose>
			<xsl:when test="$isAViewAndNotAForm = '1' ">DisplayPattern</xsl:when>
			<xsl:when test="$formType = 'DisplayForm'">DisplayPattern</xsl:when>
			<xsl:otherwise>EditPattern</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<xsl:namespace-alias stylesheet-prefix="xz" result-prefix="xsl"/>
	<xsl:namespace-alias stylesheet-prefix="out" result-prefix="msxsl"/>

	<xsl:template match="/">
		<xsl:text disable-output-escaping="yes">
			<![CDATA[ <!DOCTYPE camlxsl [<!ENTITY nbsp " ">]> ]]>
		</xsl:text>
		<xsl:choose>
			<xsl:when test="$isAViewAndNotAForm = '1'">
				<xsl:apply-templates select="//View[@Name=$viewName or not(@Name)]"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="//Form[@Type=$formType]"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
<!-- here be view stuff -->
	<xsl:template match="View">
		<xz:stylesheet version="1.0" 
			xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
			xmlns:o="urn:schemas-microsoft-com:office"
			xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" 
			xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" 
			xmlns:rs="urn:schemas-microsoft-com:rowset" 
			xmlns:z="#RowsetSchema"
			xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
			exclude-result-prefixes="rs z o s ddwrt dt msxsl">

			<xz:output method="html" indent="no"/>
			
		 	<xsl:call-template name="genParams">
			</xsl:call-template>
							
			<xsl:apply-templates select=".//*" mode="genParams">
				<xsl:with-param name="currentList" select="."/>
				<xsl:with-param name="viewNode" select="."/>
				<xsl:sort select="name()"/>
			</xsl:apply-templates>

			<xsl:apply-templates select=".//Length|.//FieldSwitch" mode="genVariables">
				<xsl:with-param name="currentView" select="."/>
			</xsl:apply-templates>

			<!-- look in the head fields for more params -->
			<!-- url node without attributes will be ddwrt which is context sensitive, it won't be processed here
			       Instead it'll be processed on per field basis-->
			<xsl:apply-templates select="/List/MetaData/Fields//*[not(name(.) = 'URL' and not( @*))]" mode="genParams">
				<xsl:with-param name="currentList" select="/List/MetaData/Fields"/>
				<xsl:with-param name="viewNode" select="."/>
			</xsl:apply-templates>

			<!-- we also need to look at url:lookup in the fldtypes.xml file,
			      be more specific to avoid duplication.-->
			<xsl:apply-templates select="$fldTypes//URL" mode="genParams">
				<xsl:with-param name="currentList" select="$fldTypes"/>
				<xsl:with-param name="viewNode" select="."/>
			</xsl:apply-templates>			

			<!-- come back here -->
			<xsl:variable name="renderPatternName">DisplayPattern</xsl:variable>

		 	<xsl:call-template name="addScript">
			</xsl:call-template>

			<xsl:apply-templates select="." mode="genRootTemplate">
				<xsl:with-param name="currentView" select="."/>
			</xsl:apply-templates>

			<xsl:apply-templates select="ViewBody" mode="genTemplates">
				<xsl:with-param name="currentView" select="."/>
				<xsl:with-param name="renderPattern" select="$renderPatternName"/>
				<xsl:with-param name="fieldData" select="."/>
				<xsl:with-param name="Field" select="."/>			
			</xsl:apply-templates>

			<!--<xsl:apply-templates select="GroupByHeader" mode="genTemplates">
				<xsl:with-param name="currentView" select="."/>
				<xsl:with-param name="renderPattern" select="$renderPatternName"/>
				<xsl:with-param name="fieldData" select="."/>
				<xsl:with-param name="Field" select="."/>			
			</xsl:apply-templates>
			
			<xsl:apply-templates select="GroupByFooter" mode="genTemplates">
				<xsl:with-param name="currentView" select="."/>
				<xsl:with-param name="renderPattern" select="$renderPatternName"/>
				<xsl:with-param name="fieldData" select="."/>
				<xsl:with-param name="Field" select="."/>			
			</xsl:apply-templates>-->
			
			<xsl:call-template name="GenStringFunction"/>
			
			<xsl:apply-templates select="Toolbar" mode="genTemplates">
					<xsl:with-param name="currentView" select="."/>
					<xsl:with-param name="fieldData" select="."/>
					<xsl:with-param name="renderPattern" select="'HeaderPattern'"/>
					<xsl:with-param name="Field" select="."/>						
			</xsl:apply-templates>
			
		</xz:stylesheet>
	</xsl:template>
<!-- end of view stuff -->

<!-- here be form stuff -->
	<xsl:template match="Form">
		<xz:stylesheet version="1.0" 
			xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
			xmlns:o="urn:schemas-microsoft-com:office"
			xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" 
			xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" 
			xmlns:rs="urn:schemas-microsoft-com:rowset" 
			xmlns:z="#RowsetSchema"
			xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime">

			<!-- come back here -->
			<xsl:variable name="renderPatternName">
				<xsl:choose>
					<xsl:when test="@Type = 'EditForm'">EditPattern</xsl:when>
					<xsl:when test="@Type = 'NewForm'">NewPattern</xsl:when>
					<xsl:when test="@Type = 'DisplayForm'">DisplayPattern</xsl:when>
					<xsl:otherwise>DisplayPattern</xsl:otherwise>
				</xsl:choose>
			</xsl:variable>
			
		 	<xsl:call-template name="genParams">
			</xsl:call-template>

			<xsl:apply-templates select=".//*" mode="genParams">
				<xsl:with-param name="currentList" select="."/>
				<xsl:with-param name="viewNode" select="."/>				
				<xsl:sort select="name()"/>
			</xsl:apply-templates>
		
			<!-- we also need to look at url:lookup in the fldtypes.xml file-->
			<xsl:apply-templates select="$fldTypes//URL" mode="genParams">
				<xsl:with-param name="currentList" select="$fldTypes"/>
				<xsl:with-param name="viewNode" select="."/>				
			</xsl:apply-templates>			

			<xsl:apply-templates select="$fldTypes//RenderPattern[@Name = $renderPatternName]//Switch | .//Switch" mode="genVariables">
				<xsl:with-param name="currentView" select="."/>
				<xsl:with-param name="renderPattern" select="$renderPatternName"/>
				<xsl:with-param name="fieldData" select="."/>
			</xsl:apply-templates>

		 	<xsl:call-template name="addScript">
			</xsl:call-template>
			
			<xsl:apply-templates select="." mode="genRootTemplate">
				<xsl:with-param name="currentView" select="."/>
			</xsl:apply-templates>
			
			<xsl:apply-templates select="ListFormBody" mode="genTemplates">
				<xsl:with-param name="currentView" select="."/>
				<xsl:with-param name="renderPattern" select="$renderPatternName"/>
				<xsl:with-param name="fieldData" select="."/>
				<xsl:with-param name="Field" select="."/>							
			</xsl:apply-templates>
		</xz:stylesheet>
	</xsl:template>
<!-- end of form stuff -->

	<!-- generate params that are common to all pages, like PageUrl, list name, etc-->
	<xsl:template name="genParams">
		<xz:param>
			<xsl:attribute name="name">PageUrl</xsl:attribute>
		</xz:param>
		
		<xz:param>
			<xsl:attribute name="name">HttpHost</xsl:attribute>
		</xz:param>

		<xz:param>
			<xsl:attribute name="name">HttpPath</xsl:attribute>
		</xz:param>

		<xz:param>
			<xsl:attribute name="name">List</xsl:attribute>
		</xz:param>
		
		<xz:param>
			<xsl:attribute name="name">URL_Display</xsl:attribute>
		</xz:param>

		<xz:param>
			<xsl:attribute name="name">HttpVDir</xsl:attribute>
		</xz:param>

		<xz:param>
			<xsl:attribute name="name">View</xsl:attribute>
		</xz:param>

		<xz:param>
			<xsl:attribute name="name">FilterLink</xsl:attribute>
			<xsl:attribute name="select">ddwrt:FilterLink()</xsl:attribute>
		</xz:param>

		<xsl:if test="$isThreaded">
			<xz:param name="isThreaded">TRUE</xz:param>
		</xsl:if>

		<xz:param name="Language">1033</xz:param>
		<xz:param name="dvt_adhocmode">sort</xz:param>
		<xz:param name="dvt_adhocfiltermode">xsl</xz:param>
		<xz:param name="dvt_fieldsort">1</xz:param>
		<xz:param name="dvt_sortfield"></xz:param>
		<xz:param name="dvt_groupfield"></xz:param>
		<xz:param name="dvt_groupdisplay"></xz:param>
		<xz:param name="dvt_sortdir"></xz:param>
		<xz:param name="dvt_groupdir"></xz:param>
		<xz:param name="dvt_filterfield"></xz:param>
		<xz:param name="dvt_filterval"></xz:param>
		<xz:param name="dvt_filtertype"></xz:param>
		<xz:param name="dvt_firstrow">1</xz:param>
		<xz:param name="dvt_p2plinkfields"></xz:param>
		<xz:param name="dvt_nextpagedata"></xz:param>
		<xz:param name="dvt_grouptype"></xz:param>
		<xz:param name="dvt_sorttype"></xz:param>
		<xz:param name="dvt_groupsorttype"></xz:param>
		<xz:param name="dvt_apos">&#x27;</xz:param>
		<xz:param name="filterParam" ddwrt:NoCAMLVariable="1"></xz:param>		
		<xz:param name="ImagesPath"></xz:param>
		<xz:param name="ListUrlDir"></xz:param>
		<xz:param name="EMail">EMail</xz:param>

	</xsl:template>

	<!-- this is the stuff for adding script in ddrwt run time -->
	<xsl:template name="addScript">
	</xsl:template>

    <xsl:template name="GenStringFunction">
        <xz:template name="stringreplace">
            <xz:param name="stringvalue"/>
            <xz:param name="from"/>
            <xz:param name="to"/>
            
            <xz:choose> 
                <xz:when test="contains($stringvalue, $from)">
                    <xz:value-of>
                        <xsl:attribute name="select">substring-before($stringvalue, $from)</xsl:attribute>
                    </xz:value-of>
                    <xz:if test="contains(substring($stringvalue, 1, string-length($stringvalue) - 1), $from)">
                        <xz:value-of>
                            <xsl:attribute name="select">$to</xsl:attribute>
                        </xz:value-of>
                        <xz:call-template name="stringreplace">
                            <xz:with-param name="stringvalue">
                                <xsl:attribute name="select">substring-after($stringvalue, $from)</xsl:attribute>
                            </xz:with-param>
                            <xz:with-param name="from">
                                <xsl:attribute name="select">$from</xsl:attribute>
                            </xz:with-param>
                            <xz:with-param name="to">
                                <xsl:attribute name="select">$to</xsl:attribute>
                            </xz:with-param>
                        </xz:call-template>
                    </xz:if>
                </xz:when>
                <xz:otherwise>
                    <xz:value-of>
                        <xsl:attribute name="select">$stringvalue</xsl:attribute>
                    </xz:value-of>
                </xz:otherwise>
            </xz:choose>
        </xz:template>
    </xsl:template>
    
	<!-- this is the stuff for generating the many params at the top
		of the output stylesheet -->
	<xsl:template match="*" mode="genParams"/>
	
	<xsl:template match="ID|Identity|WebQueryInfo" mode="genParams">
		<!-- only one -->		
		<xsl:param name="currentList"/>
		<xsl:param name="viewNode"/>
		
		 <xsl:call-template name="genParamTag">
			<xsl:with-param name="currentness2" select="$currentList"/>
			<xsl:with-param name="nodeness" select="."/>
			<xsl:with-param name="viewNode" select="$viewNode"/>
		</xsl:call-template>
	</xsl:template>
		
 	<!-- generate variables in the header of the view files
            to get rid of the annoying "alt=switch" problem.    -->
	<xsl:template match="Switch|FieldSwitch" mode="genVariables">
		<xsl:param name="currentView"/>
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>

		<xsl:variable name="expr" select="Expr"/>
		<xsl:variable name="Str" select="string(preceding-sibling::*[1])"/>
		<xsl:variable name="singleQuote">&#x27;</xsl:variable>
		<xsl:variable name="prevNode" select="preceding-sibling::*[1]"/>		
		<xsl:variable name="prevStr"     select="normalize-space(string(preceding-sibling::*[1]))"/>

		<!-- if the previous nodes are "fieldsortimageURL" or the previous node is html node
		      that ends with " or ' or =, we'll do special add the special handlding code to have var first,
		      then directly call the var in the usage-->
		<xsl:if test="name(preceding-sibling::*[2]) = 'FieldSortImageURL' or 
				(name($prevNode) = 'HTML' and (substring($prevStr, string-length($prevStr), 1) = '&quot;'  
				or substring($prevStr, string-length($prevStr), 1) = $singleQuote and not(substring($Str, string-length($Str), 1) = ' '))) or (name($prevNode) = 'HTML' and (substring($prevStr, string-length($prevStr), 1) = $singleQuote or 
substring($prevStr, string-length($prevStr), 1) = '='))">
			<xz:variable>
				<xsl:attribute name="name"><xsl:value-of select="generate-id(.)"/></xsl:attribute>
					<xz:choose>
					<xsl:for-each select="Case">
						<xz:when>
							<xsl:attribute name="test">
								<xsl:apply-templates mode="genTemplates" select="$expr/*">
									<xsl:with-param name="currentView" select ="$currentView"/>
									<xsl:with-param name="fieldData" select="$fieldData"/>
									<xsl:with-param name="renderPattern" select="$renderPattern"/>
								</xsl:apply-templates>='<xsl:value-of select="@Value"/>'</xsl:attribute>
								
							<xsl:apply-templates mode="genTemplates" select="*">
								<xsl:with-param name="currentView" select="$currentView"/>
								<xsl:with-param name="fieldData" select="."/>
								<xsl:with-param name="renderPattern" select="."/>
								<xsl:with-param name="Field" select="."/>
							</xsl:apply-templates>	
						</xz:when>
					</xsl:for-each>
					<xsl:for-each select="Default">
						<xz:otherwise>
							<xsl:apply-templates mode="genTemplates" select="*">
								<xsl:with-param name="currentView" select="$currentView"/>
								<xsl:with-param name="fieldData" select="."/>
								<xsl:with-param name="renderPattern" select="."/>
								<xsl:with-param name="Field" select="."/>
							</xsl:apply-templates>
						</xz:otherwise>
					</xsl:for-each>
					</xz:choose>
			</xz:variable>
		</xsl:if>
	</xsl:template>

 	<!-- generate variables before the fields -->
	<xsl:template match="Switch|FieldSwitch" mode="genVariablesInField">
		<xsl:param name="currentView"/>
		<xsl:param name="fieldData"/>
		<xsl:param name="realFieldData"/>
		<xsl:param name="renderPattern"/>

		<xsl:variable name="expr" select="Expr"/>
		<xsl:variable name="prevNode" select="preceding-sibling::*[1]"/>		
		<xsl:variable name="prevStr"     select="normalize-space(string(preceding-sibling::*[1]))"/>

		<!-- if the previous nodes are "fieldsortimageURL" or the previous node is html node
		      that ends with " or ' or =, we'll do special add the special handlding code to have var first,
		      then directly call the var in the usage-->
		<xsl:if test="name(preceding-sibling::*[1])='ImagesPath' or (name($prevNode) = 'HTML' and (substring($prevStr, string-length($prevStr), 1) = '&quot;' 
							or substring($prevStr, string-length($prevStr), 1) = '=') or substring($prevStr, string-length($prevStr)-6, 7)='images/')
							or (name(ancestor::*[1])='ViewBody' and not(preceding-sibling::*[1]))">
			<xz:variable>
				<xsl:attribute name="name"><xsl:value-of select="generate-id(.)"/></xsl:attribute>
					<xz:choose>
					<xsl:for-each select="Case">
						<xz:when>
							<xsl:attribute name="test">
								<xsl:apply-templates mode="genTemplates" select="$expr/*">
									<xsl:with-param name="currentView" select ="$currentView"/>
									<xsl:with-param name="fieldData" select="$fieldData"/>
									<xsl:with-param name="renderPattern" select="$renderPattern"/>
								</xsl:apply-templates>='<xsl:value-of select="@Value"/>'</xsl:attribute>
								
							<xsl:apply-templates mode="genTemplates" select="child::node()">
								<xsl:with-param name="currentView" select="$currentView"/>
								<xsl:with-param name="fieldData" select="$fieldData"/>
								<xsl:with-param name="renderPattern" select="$renderPattern"/>
								<xsl:with-param name="Field" select="."/>
							</xsl:apply-templates>	
						</xz:when>
					</xsl:for-each>
					<xsl:for-each select="Default">
						<xz:otherwise>
							<xsl:apply-templates mode="genTemplates" select="child::node()">
								<xsl:with-param name="currentView" select="$currentView"/>
								<xsl:with-param name="fieldData" select="$fieldData"/>
								<xsl:with-param name="renderPattern" select="$renderPattern"/>
								<xsl:with-param name="Field" select="."/>
							</xsl:apply-templates>
						</xz:otherwise>
					</xsl:for-each>
					</xz:choose>
			</xz:variable>
		</xsl:if>
	</xsl:template>

	<!-- generate variables before the fields -->
	<xsl:template match="LookupColumn" mode="genVariablesInField">
		<xsl:param name="currentView"/>
		<xsl:param name="fieldData"/>
		<xsl:param name="realFieldData"/>
		<xsl:param name="renderPattern"/>

		<xsl:variable name="expr" select="Expr"/>
		<xsl:variable name="prevNode" select="preceding-sibling::*[1]"/>		
		<xsl:variable name="prevStr"     select="normalize-space(string(preceding-sibling::*[1]))"/>

		<xsl:if test="@ShowField and not(@ShowField='EMail')">
			<xz:variable>
				<xsl:attribute name="name">UserLookup<xsl:value-of select="generate-id($realFieldData)"/><xsl:value-of select="generate-id(.)"/></xsl:attribute>
				<xsl:attribute name="select">ddwrt:UserLookup(string(@<xsl:value-of select='$realFieldData/@Name'/>), '<xsl:value-of select="@ShowField"/>')</xsl:attribute>
			</xz:variable>			
		</xsl:if>
	</xsl:template>
	
 	<!-- generate variables in the header of the view files
            to get rid of the annoying "path=<MapToIcon>" problem.    -->
	<xsl:template match="MapToIcon" mode="genVariables">
		<xsl:param name="currentView"/>
		<xsl:variable name="expr" select="Expr"/>
	
		<xz:variable>
			<xsl:attribute name="name">MapToIcon<xsl:value-of select="generate-id(.)"/></xsl:attribute>
			<xsl:attribute name="select">ddwrt:MapToIcon(<xsl:for-each select="Column">string('<xsl:value-of select="@Name"/>')<xsl:if test="not (position() = last())">,</xsl:if></xsl:for-each>)</xsl:attribute>
		</xz:variable>
	</xsl:template>	

 	<!-- generate variables in the header of the view files
            to get rid of the annoying "width=length()" problem.    -->
	<xsl:template match="Length" mode="genVariables">
		<xsl:param name="currentView"/>
		<xsl:variable name="expr" select="Expr"/>

		<xz:variable>
			<xsl:attribute name="name">curString<xsl:value-of select="generate-id(.)"/></xsl:attribute>
			<xsl:attribute name="select">
				<xsl:apply-templates mode="genTemplates" select="*">
				<xsl:with-param name="currentView" select="$currentView"/>
				<xsl:with-param name="fieldData" select="."/>
				<xsl:with-param name="renderPattern" select="."/>
				<xsl:with-param name="Field" select="."/>						
				</xsl:apply-templates>
			</xsl:attribute>
		</xz:variable>
		
		<xz:variable>	
			<xsl:attribute name="name">Length<xsl:value-of select="generate-id(.)"/></xsl:attribute>
			<xsl:attribute name="select">
				<xsl:choose>
					<xsl:when test="@Div">
						<xsl:text/>string-length($curString<xsl:value-of select="generate-id(.)"/>) / <xsl:value-of select="@Div"/><xsl:text/>
					</xsl:when>
					<xsl:when test="@Sub">
						<xsl:text/>string-length($curString<xsl:value-of select="generate-id(.)"/>) - <xsl:value-of select="@Sub"/><xsl:text/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text/>string-length($curString<xsl:value-of select="generate-id(.)"/>)<xsl:text/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
		</xz:variable>
	</xsl:template>

	<xsl:template match="Length" mode="genTemplates">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>
		<xsl:param name="ThisField"/>
		
		<xsl:choose>
			<xsl:when test="GetVar">
				<xsl:text/>string-length(ddwrt:GetVar('<xsl:value-of select="GetVar/@Name"/>'))<xsl:text/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text/>{$Length<xsl:value-of select="generate-id(.)"/>}<xsl:text/>
			</xsl:otherwise>
		</xsl:choose>

	</xsl:template>


	<xsl:template match="CrossProjectLink" mode="genTemplates">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>
		<xsl:param name="ThisField"/>
		
		<xz:choose>
			<xz:when>
				<xsl:attribute name="test">not(@WorkspaceLink='1' or @WorkspaceLink='-1')</xsl:attribute>
				<IMG border="0" width="16" height="16" SRC="{{$ImagesPath}}blank.gif"/>
			</xz:when>
			<xz:otherwise>
				<xz:variable>
					<xsl:attribute name="name">meetingName</xsl:attribute>
					<xsl:attribute name="select">substring-before(@Workspace, ', ')</xsl:attribute> 
				</xz:variable>
				<A HREF="{{$meetingName}}" target="_self"><IMG border="0" SRC="{{$ImagesPath}}{$fieldData/@DisplayImage}"/></A>
			</xz:otherwise>
		</xz:choose>
	</xsl:template>

	<xsl:template match="XML" mode="genTemplates">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>
		<xsl:param name="ThisField"/>
		
		<xsl:apply-templates mode="genTemplates" select="*">
			<xsl:with-param name="currentView" select="$currentView"/>
			<xsl:with-param name="fieldData" select="$fieldData"/>
			<xsl:with-param name="renderPattern" select="$renderPattern"/>
			<xsl:with-param name="Field" select="$Field"/>
			<xsl:with-param name="ThisField" select="$ThisField"/>
		</xsl:apply-templates>
	</xsl:template>

	<xsl:template match="LookupColumn" mode="genParams">
		<xsl:param name="currentList"/>
		<xsl:param name="viewNode"/>
	
		<!-- if the lookupcolumn tag is inside the form, and inside a scriptquote tag, we'll add the variable-->
		<xsl:if test="$isAViewAndNotAForm = '0' and ancestor::ScriptQuote">
			<xz:variable>
				<xsl:attribute name="name"><xsl:value-of select="name(.)"/><xsl:value-of select="generate-id(.)"/></xsl:attribute>
				<xsl:attribute name="select">/dsQueryResponse/Rows/Row[@ID = $ID]/@<xsl:value-of select="@Name"/></xsl:attribute>
			</xz:variable>			
		</xsl:if>
	</xsl:template>

	<xsl:template match="Column" mode="genParams">
		<!-- only one -->		
		<xsl:param name="currentList"/>
		<xsl:param name="viewNode"/>
		
		<xsl:variable name="prevNode" select="preceding-sibling::*[1]"/>		
		<xsl:variable name="prevStr"     select="normalize-space(string(preceding-sibling::*[1]))"/>
				
		<!--make a variable when the preceding node of this current column ends with a " -->		
		<xsl:if test="not(name($viewNode) = 'View')">
		<xsl:choose>
		<xsl:when test="generate-id(current()) = generate-id(($currentList//*[name(current())=name(.) and @HTMLEncode = 'TRUE' and name($prevNode) = 'HTML' and substring($prevStr, string-length($prevStr), 1) = '&quot;'])[1])">
			<xz:variable><xsl:attribute name="name"><xsl:value-of select="name($viewNode)"/></xsl:attribute>
				<xsl:attribute name="select">@<xsl:value-of select="$viewNode/@Name"/></xsl:attribute>
			</xz:variable>
		</xsl:when>
		<!-- make a variable when the preceding node ends with '='-->
		<xsl:when test="generate-id(current()) = generate-id(($currentList//*[name(current())=name(.) and name($prevNode) = 'HTML' and substring($prevStr, string-length($prevStr), 1) = '='])[1])">
			<xz:variable><xsl:attribute name="name"><xsl:value-of select="generate-id($viewNode)"/></xsl:attribute>
				<xsl:attribute name="select">@<xsl:value-of select="$viewNode/@Name"/></xsl:attribute>
			</xz:variable>
		</xsl:when>
		</xsl:choose>
		</xsl:if>
	</xsl:template>
	
	<xsl:template name="genParamTag">
		<xsl:param name="currentness2"/>
		<xsl:param name="nodeness"/>
		<xsl:param name="viewNode" />
		<!-- we don't do param when there is attributes to the node (it's probably a run time stuff)-->
		<!-- when viewNode is different from currentness2, it's a field node, we need to make sure 
		      no duplication-->
		<xsl:if test="generate-id($nodeness) = generate-id(($currentness2//*[name($nodeness)=name(.) and not (@*)])[1]) and not(not ($viewNode = $currentness2) and $viewNode//*[name(.)=name($nodeness) and not(@*)])">
			<xz:param><xsl:attribute name="name"><xsl:value-of select="name($nodeness)"/></xsl:attribute>
			</xz:param>
		</xsl:if>
	</xsl:template>

	<xsl:template name="genRuntimeParamTag">
		<xsl:param name="currentness2"/>
		<xsl:param name="nodeness"/>
		<xsl:param name="viewNode" />
		<!-- when viewNode is different from currentness2, it's a field node, we need to make sure 
		      no duplication-->
		<xsl:if test="generate-id($nodeness) = generate-id(($currentness2//*[name($nodeness)=name(.) and not (@*)])[1]) and not(not ($viewNode = $currentness2) and $viewNode//*[name(.)=name($nodeness) and not(@*)])">
			<xz:param>
				<xsl:attribute name="name"><xsl:value-of select="name($nodeness)"/></xsl:attribute>
               	<xsl:attribute name="select">ddwrt:<xsl:value-of select="name($nodeness)" disable-output-escaping="yes"/>()</xsl:attribute>
			</xz:param>
		</xsl:if>
	</xsl:template>

	<xsl:template name="genParamRt">
		<xsl:param name="currentness2"/>
		<xsl:param name="nodeness"/>
		<xsl:param name="viewNode" />
		<!-- we don't do param when there is attributes to the node (it's probably a run time stuff)-->
		<!-- when viewNode is different from currentness2, it's a field node, we need to make sure 
		      no duplication-->
		<xsl:if test="generate-id($nodeness) = generate-id(($currentness2//*[name($nodeness)=name(.) and not (@*)])[1])">
			<xz:variable><xsl:attribute name="name"><xsl:value-of select="name($nodeness)" disable-output-escaping="yes"/>_<xsl:value-of select="name($viewNode)"/></xsl:attribute>
               	<xsl:attribute name="select">ddwrt:<xsl:value-of select="name($nodeness)" disable-output-escaping="yes"/>()</xsl:attribute>
			</xz:variable>
		</xsl:if>
	</xsl:template>

<!--	
current thinking has GetVar being a runtime function
only getvars that needs to be parameterized are done.
-->		

	<xsl:template mode="genParams" match="ListUrlDir">
		<xsl:param name="currentList"/>
		<xsl:param name="viewNode"/>
			<xsl:choose>
				<xsl:when test="@WebRel">
					<xsl:call-template name="genParamTagEx">
					<xsl:with-param name="currentness2" select="$currentList"/>
					<xsl:with-param name="nodeness" select="."/>
					<xsl:with-param name="viewNode" select="$viewNode"/>
					<xsl:with-param name="attr">WebRel</xsl:with-param>
					<xsl:with-param name="attr-value" select="@WebRel"></xsl:with-param>
					</xsl:call-template>
				</xsl:when>
				<xsl:otherwise>
				</xsl:otherwise>
			</xsl:choose>
	</xsl:template>

	<xsl:template mode="genParams" match="URL">
		<xsl:param name="currentList"/>
		<xsl:param name="viewNode" />
			<xsl:choose>
				<xsl:when test="@Cmd and not(@Cmd='Display')">
					<xsl:call-template name="genParamTagEx">
						<xsl:with-param name="currentness2" select="$currentList"/>
						<xsl:with-param name="nodeness" select="."/>
						<xsl:with-param name="viewNode" select="$viewNode"/>
						<xsl:with-param name="attr">Cmd</xsl:with-param>
						<xsl:with-param name="attr-value" select="@Cmd"></xsl:with-param>
					</xsl:call-template>
				</xsl:when>
				<xsl:otherwise>
				</xsl:otherwise>
			</xsl:choose>			
	</xsl:template>

	<xsl:template mode="genParams" match="LocaleInfo">
		<xsl:param name="currentList"/>
		<xsl:param name="viewNode"/>
		 <xsl:call-template name="genParamTagEx">
			<xsl:with-param name="currentness2" select="$currentList"/>
			<xsl:with-param name="nodeness" select="."/>
			<xsl:with-param name="viewNode" select="$viewNode"/>			
			<xsl:with-param name="attr">LCTYPE</xsl:with-param>
			<xsl:with-param name="attr-value" select="@LCTYPE"></xsl:with-param>
		</xsl:call-template>
	</xsl:template>

	<xsl:template name="genParamTagRt">
		<xsl:param name="currentness2"/>
		<xsl:param name="nodeness"/>
		<xsl:param name="viewNode"/>
		<xsl:param name="attr"/>
		<xsl:param name="attr-value"/>
		<!-- when viewNode is different from currentness2, it's a field node, we need to make sure 
		      no duplication-->		
		<xsl:if test="generate-id($nodeness) = generate-id(($currentness2//*[name($nodeness)=name(.) and @*[$attr] = $attr-value])[1]) and not(not ($viewNode = $currentness2) and $viewNode//*[name(.)=name($nodeness) and @*[$attr] = $attr-value])">
			<xz:param>
				<xsl:attribute name="name"><xsl:value-of select="name($nodeness)" disable-output-escaping="yes"/>_<xsl:value-of select="$attr-value"/></xsl:attribute>
               	<xsl:attribute name="select">ddwrt:<xsl:value-of select="name($nodeness)" disable-output-escaping="yes"/>('<xsl:value-of select="$attr-value"/>')</xsl:attribute>
			</xz:param>
		</xsl:if>
	</xsl:template>
		
	<xsl:template name="genParamTagEx">
		<xsl:param name="currentness2"/>
		<xsl:param name="nodeness"/>
		<xsl:param name="viewNode"/>
		<xsl:param name="attr"/>
		<xsl:param name="attr-value"/>
		<!-- when viewNode is different from currentness2, it's a field node, we need to make sure 
		      no duplication-->		
		<xsl:if test="generate-id($nodeness) = generate-id(($currentness2//*[name($nodeness)=name(.) and @*[name(.) = $attr] = $attr-value])[1]) and not(not ($viewNode = $currentness2) and $viewNode//*[name(.)=name($nodeness) and @*[name(.) = $attr] = $attr-value])">
			<xz:param>
				<xsl:attribute name="name">
					<xsl:value-of select="name($nodeness)"/>_<xsl:value-of select="$attr-value"/>
				</xsl:attribute>
			</xz:param>
		</xsl:if>
	</xsl:template>
	
	<!-- this is the stuff for generating the root template, which is 
		basically just a series of call-template and apply-templates -->
	<xsl:template match="*" mode="genRootTemplate">
		<xsl:param name="currentView"/>

		<xz:template match="/">
			<xz:call-template name="dvt_1" />
		</xz:template>
		
		<xz:template name="dvt_1">
			<xz:variable name="StyleName">Table</xz:variable>
			<xz:variable name="Rows">
				<xsl:attribute name="select"><xsl:text>/dsQueryResponse/Rows/Row</xsl:text></xsl:attribute>
			</xz:variable>
			<xz:variable name="RowCount">
				<xsl:attribute name="select"><xsl:text>count($Rows)</xsl:text></xsl:attribute>
			</xz:variable>
			<xsl:variable name="RowLimit" select="RowLimit"/>
			<xz:variable name="RowLimit">
				<xsl:choose>
					<xsl:when test="$RowLimit"><xsl:value-of select="number($RowLimit)"/></xsl:when>
					<xsl:otherwise>100</xsl:otherwise>
				</xsl:choose>
			</xz:variable>
			<xsl:if test="RowLimit[@Paged='TRUE']">
				<xz:variable>
					<xsl:attribute name="name">FirstRow</xsl:attribute>
					<xsl:attribute name="select">$dvt_firstrow</xsl:attribute>	
				</xz:variable>

				<xz:variable>
					<xsl:attribute name="name">LastRow</xsl:attribute>
					<xz:choose>
						<xz:when test="($FirstRow + $RowLimit - 1) &gt; $RowCount"><xz:value-of><xsl:attribute name="select">$RowCount</xsl:attribute></xz:value-of></xz:when>
						<xz:otherwise><xz:value-of><xsl:attribute name="select">$FirstRow + $RowLimit - 1</xsl:attribute></xz:value-of></xz:otherwise>
					</xz:choose>
				</xz:variable>			
			</xsl:if>

			<xz:variable name="IsEmpty">
				<xsl:attribute name="select"><xsl:text>$RowCount = 0</xsl:text></xsl:attribute>
			</xz:variable>

			<!-- view node attribute FreeForm=TRUE means set the FreeForm var-->
			<xsl:if test="$currentView/@FreeForm='TRUE'">
				<xz:value-of>
					<xsl:attribute name="select">
						<xsl:text/>ddwrt:SetVar('FreeForm', 'TRUE')<xsl:text/>
					</xsl:attribute>
				</xz:value-of>
			</xsl:if>

			<!-- the big html tag to enclose all inside tags, also exluce all the redundant
			       tags -->
			
			
			<!--no matter where toolbar is in the xml, it'll be shown the earliest. 
			      so we do it first, then do the rest (excluding the toolbar)-->
			<xsl:if test="Toolbar[not(@Position='After')]">
				<xz:call-template name="dvt_1.toolbar">
					<xz:with-param name="Rows">
						<xsl:attribute name="select">$Rows</xsl:attribute>
					</xz:with-param>
				</xz:call-template>
			</xsl:if>
			
			<!--<xsl:apply-templates select="Toolbar[not(@Position='After')]" mode="doGenRootTemplate">
				<xsl:with-param name="currentView" select="$currentView"/>
			</xsl:apply-templates>-->

			<!-- if we have viewheaderbidi, use it in place of viewheader. -->
			<xsl:variable name="hasBidi" select="./ViewBidiHeader"/>
			<xsl:variable name="Direction" select="/List/@Direction"/>
		
			<xz:choose>
				<xz:when>
					<xsl:attribute name="test"><xsl:text>$IsEmpty</xsl:text></xsl:attribute>
					<xsl:apply-templates mode="genTemplates" select="*[name(.) = 'ViewEmpty']/*">
						<xsl:with-param name="currentView" select="$currentView"/>
						<xsl:with-param name="fieldData" select="*"/>
						<xsl:with-param name="renderPattern" select="'HeaderPattern'"/>
						<xsl:with-param name="Field" select="."/>
					</xsl:apply-templates>
					
					<xsl:if test="Toolbar[@Position='After']">
						<xz:call-template name="dvt_1.toolbar">
							<xz:with-param name="Rows">
								<xsl:attribute name="select">$Rows</xsl:attribute>
							</xz:with-param>
						</xz:call-template>
					</xsl:if>

				</xz:when>
				<xz:otherwise>		
					<xsl:choose>
						<xsl:when test="$hasBidi and not ($Direction = '' ) and not ($Direction='0')">	
							<xsl:apply-templates select="ViewBidiHeader" mode="doGenRootTemplate">
								<xsl:with-param name="currentView" select="$currentView"/>
							</xsl:apply-templates>
						</xsl:when>
						<xsl:otherwise>
						<xsl:apply-templates select="ViewHeader" mode="doGenRootTemplate">
							<xsl:with-param name="currentView" select="$currentView"/>
						</xsl:apply-templates>							
						</xsl:otherwise>
					</xsl:choose>	
					
					<xsl:apply-templates select="ViewBody" mode="doGenRootTemplate">
						<xsl:with-param name="currentView" select="$currentView"/>
					</xsl:apply-templates>

					<xsl:apply-templates select="ViewFooter" mode="doGenRootTemplate">
						<xsl:with-param name="currentView" select="$currentView"/>
					</xsl:apply-templates>

					<xsl:if test="Toolbar[@Position='After']">
						<xz:call-template name="dvt_1.toolbar">
							<xz:with-param name="Rows">
								<xsl:attribute name="select">$Rows</xsl:attribute>
							</xz:with-param>
						</xz:call-template>
					</xsl:if>

					<!--<xsl:apply-templates select="Toolbar[@Position='After']" mode="doGenRootTemplate">
						<xsl:with-param name="currentView" select="$currentView"/>
					</xsl:apply-templates>-->
				</xz:otherwise>	
			</xz:choose>
	
			
		</xz:template>
	</xsl:template>
	
	<xsl:template match="*" mode="doGenRootTemplate">
		<xsl:param name="currentView"/>
		<xsl:choose>
			<xsl:when test="name() = 'ListFormBody'">
				<xz:apply-templates select="/dsQueryResponse/Rows/Row" mode="row">
				</xz:apply-templates>
			</xsl:when>
			<xsl:when test="name() = 'PagedRecurrenceRowset'">
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="*" mode="genTemplates">
					<xsl:with-param name="currentView" select="$currentView"/>
					<xsl:with-param name="fieldData" select="*"/>
					<xsl:with-param name="renderPattern" select="'HeaderPattern'"/>
					<xsl:with-param name="Field" select="."/>								
				</xsl:apply-templates>
			</xsl:otherwise>
		</xsl:choose>		
	</xsl:template>
		
	<xsl:template match="ListFormBody" mode="doGenRootTemplate">
		<xsl:param name="currentView"/>
		<xsl:choose>  <!-- for display form, only show the items that's selected by ID -->
			<xsl:when test="$formType = 'DisplayForm' or $formType = 'EditForm'">
				<xz:apply-templates>
					<xsl:attribute name="select">/dsQueryResponse/Rows/Row</xsl:attribute>
					<xsl:attribute name="mode">row</xsl:attribute>
				</xz:apply-templates>
			</xsl:when>
			<!--new form doesn't really take z:row as input, instead it'll always output one set. -->
			<xsl:when test="($formType = 'NewForm') and ($generalDisplayPattern = 'EditPattern')">
				<xz:call-template name="ListFormBody"/>
			</xsl:when>
			<xsl:otherwise>
				<xz:apply-templates select=""/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<!-- when body is empty, show the text in viewempty node-->	
	<xsl:template match="ViewBody" mode="doGenRootTemplate">
		<xsl:param name="currentView"/>
				
		<xz:call-template name="dvt_1.body">
			<xz:with-param name="Rows" select="$Rows"/>
			<xz:with-param name="FirstRow" select="1"/>
			<xz:with-param name="LastRow" select="5"/>
		</xz:call-template>
	</xsl:template>

	<!-- viewfields and query do nothing
		  they are useful for the fields representation 
		  viewEmpty is used when viewbody is empty.-->
	<xsl:template match="ViewFields|Query|ViewEmpty" mode="doGenRootTemplate"/>

	<!-- this is the stuff for generating the various second-level templates
		which correspond to the top level nodes beneath form and view -->
	<xsl:template mode="genTemplates" match="ListFormOpening|ListFormButtons|ListFormBody|ListFormClosing">
		<xsl:param name="currentView"/>
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>		
		<xsl:param name="Field"/>

		<xz:template name="{name()}">
		<!--z:row is not for the NewPattern in form -->
		<xsl:if test="name() = 'ListFormBody' and not($renderPattern = 'NewPattern' and $generalDisplayPattern = 'EditPattern')">
			<xsl:attribute name="match">*</xsl:attribute>
			<xsl:attribute name="mode">row</xsl:attribute>
		</xsl:if>
		
		<xsl:apply-templates mode="genTemplates" select="*">
			<xsl:with-param name="currentView" select="$currentView"/>
			<xsl:with-param name="fieldData" select="$fieldData"/>
			<xsl:with-param name="renderPattern" select="$renderPattern"/>
			<xsl:with-param name="Field" select="."/>						
		</xsl:apply-templates>
		</xz:template>
	</xsl:template>

	<xsl:template mode="doGenRootTemplate" match="PagedRowset">
		<xsl:param name="currentView"/>
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>		
		<xsl:param name="Field"/>

		<xsl:if test="not(preceding-sibling::*[name(.) = name(current())])">
		<xsl:variable name="RowLimit" select="number(following-sibling::*[name(.) = 'RowLimit']|preceding-sibling::*[name(.) = 'RowLimit'])"/>
		<xz:variable>
			<xsl:attribute name="name">DataSize</xsl:attribute>
			<xsl:attribute name="select"><xsl:text>count(/dsQueryResponse/Rows/Row)</xsl:text></xsl:attribute>
		</xz:variable>
		<xz:if>
		<xsl:attribute name="test"><xsl:text><![CDATA[ $DataSize >= ]]></xsl:text><xsl:value-of select="$RowLimit"/><xsl:text disable-output-escaping="yes"><![CDATA[ +$FirstRow]]></xsl:text></xsl:attribute>
				<xsl:apply-templates select="*" mode="genTemplates">
					<xsl:with-param name="currentView" select="$currentView"/>
					<xsl:with-param name="fieldData" select="*"/>
					<xsl:with-param name="renderPattern" select="'HeaderPattern'"/>
					<xsl:with-param name="Field" select="."/>								
				</xsl:apply-templates>
		</xz:if>
		</xsl:if>
    </xsl:template>

	<xsl:template mode="doGenRootTemplate" match="RowLimitExceeded">
		<xsl:param name="currentView"/>
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>		
		<xsl:param name="Field"/>

		<xsl:if test="not(preceding-sibling::*[name(.) = name(current())])">
		<xsl:variable name="RowLimit" select="number(following-sibling::*[name(.) = 'RowLimit']|preceding-sibling::*[name(.) = 'RowLimit'])"/>
		<xz:variable>
			<xsl:attribute name="name">DataSize</xsl:attribute>
			<xsl:attribute name="select"><xsl:text>count(/dsQueryResponse/Rows/Row)</xsl:text></xsl:attribute>		
		</xz:variable>
		<xz:if>
		<xsl:attribute name="test"><xsl:text disable-output-escaping="yes"><![CDATA[ $DataSize > ]]></xsl:text><xsl:value-of select="$RowLimit"/></xsl:attribute>
				<xsl:apply-templates select="*" mode="genTemplates">
					<xsl:with-param name="currentView" select="$currentView"/>
					<xsl:with-param name="fieldData" select="*"/>
					<xsl:with-param name="renderPattern" select="'HeaderPattern'"/>
					<xsl:with-param name="Field" select="."/>								
				</xsl:apply-templates>
		</xz:if>
		</xsl:if>
    </xsl:template>

	<xsl:template mode="genTemplates" match="ViewBody">
		<xsl:param name="currentView"/>
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>		
		<xsl:param name="Field"/>
		
		<xz:template name="dvt_1.body">
			<xz:param name="Rows"/>
			<xz:param name="FirstRow"/>
			<xz:param name="LastRow"/>
			<xz:for-each select="$Rows">
			
		<xz:variable>
			<xsl:attribute name="name">KeepItemsTogether</xsl:attribute>
			<xsl:attribute name="select">false()</xsl:attribute>
		</xz:variable>
		<xz:variable>
			<xsl:attribute name="name">HideGroupDetail</xsl:attribute>
			<xsl:attribute name="select">false()</xsl:attribute>
		</xz:variable>
		<xz:variable>
			<xsl:attribute name="name">GroupStyle</xsl:attribute>
			<xsl:attribute name="select">'auto'</xsl:attribute>
		</xz:variable>

		<xz:if>
			<xsl:attribute name="test"><xsl:text><![CDATA[ (position() >=$FirstRow and position() <= $LastRow) or $KeepItemsTogether]]></xsl:text></xsl:attribute>
			<xz:if>
				<xsl:attribute name="test">not($HideGroupDetail)</xsl:attribute>
				<xsl:attribute name="ddwrt:cf_ignore">1</xsl:attribute>

				<xsl:apply-templates select=".//Switch" mode="genVariablesInField">
					<xsl:with-param name="currentView" select="$currentView"/>
					<xsl:with-param name="fieldData" select="$fieldData"/>
					<xsl:with-param name="renderPattern" select="$renderPattern"/>
					<xsl:with-param name="Field" select="."/>						
				</xsl:apply-templates>		
				
				<xsl:apply-templates mode="genTemplates" select="*">
					<xsl:with-param name="currentView" select="$currentView"/>
					<xsl:with-param name="fieldData" select="$fieldData"/>
					<xsl:with-param name="renderPattern" select="$renderPattern"/>
					<xsl:with-param name="Field" select="."/>						
				</xsl:apply-templates>
			</xz:if>
		</xz:if>
		</xz:for-each>	
		</xz:template>
	</xsl:template>

	<xsl:template mode="genTemplates" match="GroupByHeader">
		<xsl:param name="currentView"/>
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>		
		<xsl:param name="Field"/>
		
		<xz:template name="dvt_1.groupheader">
			<xz:param name="Rows" />
		
			<xsl:apply-templates mode="genTemplates" select="*">
				<xsl:with-param name="currentView" select="$currentView"/>
				<xsl:with-param name="fieldData" select="$fieldData"/>
				<xsl:with-param name="renderPattern" select="$renderPattern"/>
				<xsl:with-param name="Field" select="$Field"/>						
			</xsl:apply-templates>
		</xz:template>
	</xsl:template>

	<xsl:template mode="genTemplates" match="GroupByFooter">
		<xsl:param name="currentView"/>
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>		
		<xsl:param name="Field"/>
		
		<xz:template name="dvt_1.groupfooter">
			<xz:param name="Rows" />
		
			<xsl:apply-templates mode="genTemplates" select="*">
				<xsl:with-param name="currentView" select="$currentView"/>
				<xsl:with-param name="fieldData" select="$fieldData"/>
				<xsl:with-param name="renderPattern" select="$renderPattern"/>
				<xsl:with-param name="Field" select="$Field"/>						
			</xsl:apply-templates>
		</xz:template>
	</xsl:template>
	
	<xsl:template mode="genTemplates" match="Toolbar">
		<xsl:param name="currentView"/>
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>		
		<xsl:param name="Field"/>
		
		<xz:template name="dvt_1.toolbar">
			<xz:param name="Rows" />
		
			<xsl:apply-templates mode="genTemplates" select="*">
				<xsl:with-param name="currentView" select="$currentView"/>
				<xsl:with-param name="fieldData" select="$fieldData"/>
				<xsl:with-param name="renderPattern" select="$renderPattern"/>
				<xsl:with-param name="Field" select="$Field"/>						
			</xsl:apply-templates>
			
			<!-- add in the ddw add hoc filter -->
			<!-- (leave the statement empty, it will get filled in by FP) -->
			<xz:if test="$dvt_adhocmode = 'filter'" ddwrt:cf_ignore="1">
			</xz:if>
				
		</xz:template>
	</xsl:template>

	<!-- FieldProperty is not documented and so far no text to show -->
	<xsl:template mode="genTemplates" match="FieldProperty">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>		
		
		<!-- if inside expr and single child, we'll output the ''-->
		<xsl:choose>
		<xsl:when test="(name(ancestor::*[1])='Expr' or name(ancestor::*[1])='Expr1' or name(ancestor::*[1])='Expr2') and not(preceding-sibling::*[1]) and not(following-sibling::*[1])">
			<xsl:text/>'<xsl:value-of select="/List/MetaData/Fields/Field[@Name=current()/@Name]/@*[name()=current()/@Select]"></xsl:value-of>'<xsl:text/>
		</xsl:when>		
		<xsl:otherwise>
			<xsl:text/><xsl:value-of select="/List/MetaData/Fields/Field[@Name=current()/@Name]/@*[name()=current()/@Select]"></xsl:value-of><xsl:text/>
		</xsl:otherwise>			
		</xsl:choose>
	</xsl:template>

	<xsl:template mode="genTemplates" match="*">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>

		<xsl:choose>
			<xsl:when test="name(ancestor::*[1])='Expr' or name(ancestor::*[1])='Expr1' or name(ancestor::*[1])='Expr2'">''</xsl:when>
			<xsl:when test="(name(ancestor::*[1])='Expr' or name(ancestor::*[1])='Expr1' or name(ancestor::*[1])='Expr2') and name(.)='ServerProperty' and @Select='HtmlTransform'">''</xsl:when>
			<xsl:when test="text()"><xsl:value-of select="."/></xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template mode="genTemplates" match="CAMLScript">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>
		
		<xsl:apply-templates mode="genTemplates-inscript" select="*">
			<xsl:with-param name="currentView" select="$currentView"/>
			<xsl:with-param name="fieldData" select="$fieldData"/>
			<xsl:with-param name="renderPattern" select="$renderPattern"/>
		</xsl:apply-templates>
	</xsl:template>	

	<xsl:template mode="genTemplates" match="TodayISO">
		<xsl:text/>			
			<xz:value-of>
					<xsl:attribute name="select">ddwrt:TodayISO()</xsl:attribute>
			</xz:value-of><xsl:text/>
	</xsl:template>

	<!-- for MapToIcon nodes, just use the variable we've already defined.-->
	<xsl:template mode="genTemplates" match="MapToAll">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>

		<xsl:text/>{ddwrt:MapToAll(<xsl:for-each select="Column">string(@<xsl:value-of select="@Name"/>)<xsl:if test="not (position() = last())">,</xsl:if></xsl:for-each>)}<xsl:text/>
	</xsl:template>


	<!-- for MapToIcon nodes, just use the variable we've already defined.-->
	<xsl:template mode="genTemplates" match="MapToIcon">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>

		<xsl:variable name="prevNode" select="preceding-sibling::*[1]"/>
		<xsl:choose>
			<xsl:when test="name($prevNode)='HTML'">
				<xsl:text/>{ddwrt:MapToIcon(<xsl:for-each select="Column">string(@<xsl:value-of select="@Name"/>)<xsl:if test="not (position() = last())">,</xsl:if></xsl:for-each>)}<xsl:text/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text/><xz:value-of>
					<xsl:attribute name="select">ddwrt:MapToIcon(<xsl:for-each select="Column">string(@<xsl:value-of select="@Name"/>)<xsl:if test="not (position() = last())">,</xsl:if></xsl:for-each>)</xsl:attribute>
				</xz:value-of><xsl:text/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template mode="genTemplates" match="MapToControl">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>

		<xsl:choose>
			<xsl:when test="ancestor::Expr or ancestor::Expr1 or ancestor::Expr2">
				<xsl:text/>ddwrt:MapToControl('', string(<xsl:apply-templates mode="genTemplates-inscript" select="GetFileExtension">
					<xsl:with-param name="currentView" select="$currentView"/>
					<xsl:with-param name="fieldData" select="$fieldData"/>
					<xsl:with-param name="renderPattern" select="$renderPattern"/>
					<xsl:with-param name="Field" select="."/>							
				</xsl:apply-templates>))<xsl:text/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text/><xz:value-of>
				<xsl:attribute name="select">ddwrt:MapToControl('', string(<xsl:apply-templates mode="genTemplates-inscript" select="GetFileExtension">
					<xsl:with-param name="currentView" select="$currentView"/>
					<xsl:with-param name="fieldData" select="$fieldData"/>
					<xsl:with-param name="renderPattern" select="$renderPattern"/>
					<xsl:with-param name="Field" select="."/>							
				</xsl:apply-templates>))</xsl:attribute>
				</xz:value-of><xsl:text/>
			</xsl:otherwise>
		</xsl:choose>	
	</xsl:template>

	<xsl:template mode="genTemplates-inscript" match="MapToIcon">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>

		<xsl:text/>{ddwrt:MapToIcon(<xsl:for-each select="Column">string(@<xsl:value-of select="@Name"/>)<xsl:if test="not (position() = last())">,</xsl:if></xsl:for-each>)}<xsl:text/>
	</xsl:template>

	<xsl:template mode="genTemplates-inscript" match="GetFileExtension">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>
		
		<xsl:text/>ddwrt:<xsl:value-of select="name(.)"/>(ddwrt:ListProperty('<xsl:value-of select="ListProperty/@Select"/>'))<xsl:text/>

	</xsl:template>

	<xsl:template mode="genTemplates" match="UrlDirName[LookupColumn/@Name]">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>
		
		<xsl:text/>{ddwrt:UrlDirName(string(@<xsl:value-of select="LookupColumn/@Name"/>))}<xsl:text/>
	</xsl:template>

	<!-- UrlBaseName is a SharePoint runtime function that takes a string url as input and 
	      output the basename.-->
	<!-- GetFileExtension is a SharePoint runtime function that takes a string url as input and 
	      output the file extension.-->
	<xsl:template mode="genTemplates" match="UrlBaseName|GetFileExtension">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>

		<xsl:variable name="prevNode" select="preceding-sibling::*[1]"/>		
		<xsl:variable name="prevStr"     select="normalize-space(string(preceding-sibling::*[1]))"/>

		<xsl:choose>
			<xsl:when test="$fieldData/@RenderXMLUsingPattern='TRUE' and Field[@Name='EncodedAbsUrl']">
				<xsl:text/>{ddwrt:<xsl:value-of select="name(.)"/>(concat($HttpHost, @FileRef))}<xsl:text/>
			</xsl:when>
			<xsl:when test="name($prevNode) = 'HTML' and substring($prevStr, string-length($prevStr), 1) = '&quot;'">
				<xsl:text/>{ddwrt:<xsl:value-of select="name(.)"/>(<xsl:apply-templates mode="genTemplates" select="*">
				<xsl:with-param name="currentView" select="$currentView"/>
				<xsl:with-param name="fieldData" select="$fieldData"/>
				<xsl:with-param name="renderPattern" select="$renderPattern"/>
				<xsl:with-param name="Field" select="."/>							
			</xsl:apply-templates>)}<xsl:text/>
			</xsl:when>
			<xsl:when test="ancestor::Expr or ancestor::Expr1 or ancestor::Expr2">
				<xsl:text/>ddwrt:<xsl:value-of select="name(.)"/>(<xsl:apply-templates mode="genTemplates" select="*">
				<xsl:with-param name="currentView" select="$currentView"/>
				<xsl:with-param name="fieldData" select="$fieldData"/>
				<xsl:with-param name="renderPattern" select="$renderPattern"/>
				<xsl:with-param name="Field" select="."/>							
				</xsl:apply-templates>)<xsl:text/>
			</xsl:when>
			<xsl:when test="*[name() = 'LookupColumn']">
				<xz:value-of>
					<xsl:attribute name="select">ddwrt:<xsl:value-of select="name(.)"/>(string(@LinkFilename))</xsl:attribute>
				</xz:value-of>
			</xsl:when>
			<xsl:otherwise>
				<xz:value-of>
					<xsl:attribute name="select">ddwrt:<xsl:value-of select="name(.)"/>(<xsl:apply-templates mode="genTemplates" select="*">
						<xsl:with-param name="currentView" select="$currentView"/>
						<xsl:with-param name="fieldData" select="$fieldData"/>
						<xsl:with-param name="renderPattern" select="$renderPattern"/>
						<xsl:with-param name="Field" select="."/>							
					</xsl:apply-templates>)</xsl:attribute>
				</xz:value-of>	
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<!-- ThreadStamp is a SharePoint runtime function that returns renders the current server's 
			local time in a specific 	format (yyyyMMddHHmmss) for sorting purposes. -->
	<xsl:template mode="genTemplates" match="ThreadStamp">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>

		<xsl:choose>
			<xsl:when test="ancestor::Expr or ancestor::Expr1 or ancestor::Expr2 or ancestor::Limit">ddwrt:ThreadStamp(<xsl:value-of select="@StripWS"/>)	</xsl:when>
			<xsl:otherwise>
				<xsl:text/>{ddwrt:ThreadStamp(<xsl:value-of select="@StripWS"/>)}<xsl:text/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<!-- ThreadStamp is a SharePoint runtime function that rgenerates a globally unique identifier 
		  as returned by the Microsoft Component Object Model function CoCreateGuid.	-->
	<xsl:template mode="genTemplates" match="GUID">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>

		<xsl:text/>{ddwrt:GUID()}<xsl:text/>
	</xsl:template>

	<xsl:template mode="genTemplates-inscript" match="Counter">
		<xsl:text/><xz:value-of><xsl:attribute name="select">ddwrt:Counter()</xsl:attribute></xz:value-of><xsl:text/>
	</xsl:template>
	
	<xsl:template mode="genTemplates" match="Counter">
		<xsl:param name="currentView"/>
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>

		<xsl:choose>
			<xsl:when test="@Type='View'">99</xsl:when>
			<xsl:otherwise><xsl:text/>{ddwrt:Counter()}<xsl:text/></xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template mode="genTemplates" match="HTML">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		
		<!-- if current node is html node, and it ends with quote, doesn't start with empty space, and 
		      doesn't end with empty space, then we need to normalize-space this node in output! -->
			<xsl:variable name="Str"     select="string(.)"/>
			<xsl:variable name="StrText"     select="text()"/>
			<xsl:variable name="NormalizedStr"     select="normalize-space(string(.))"/>
			<xsl:variable name="singleQuote">&#x27;</xsl:variable>
			<xsl:choose>
				<xsl:when test="Switch">
					<xsl:apply-templates mode="genTemplates" select="*">
						<xsl:with-param name="currentView" select="$currentView"/>
						<xsl:with-param name="fieldData" select="$fieldData"/>
						<xsl:with-param name="renderPattern" select="$renderPattern"/>
						<xsl:with-param name="Field" select="."/>							
					</xsl:apply-templates>	
				</xsl:when>
				<xsl:when test="ancestor::ScriptQuote[not(@NotAddingQuote='TRUE')]">
					<xz:text script="1">
						<xsl:value-of select="text()"/>	
					</xz:text>
				</xsl:when>
				<!-- if we have { in the string, presumably it's script, enclose it with CDATA and
				     xsl:text tag.-->
				<xsl:when test="(contains($StrText, '{') and not (contains($Str, '&quot;{'))) or contains($Str, 'SCRIPT') or
								 contains($StrText, '&lt;%') or (contains($StrText, '%&gt;') and not(contains($StrText, '0%&gt;')))">
					<xz:text disable-output-escaping="yes">
						<xsl:value-of select="text()"/>	
					</xz:text>
				</xsl:when>
				<xsl:when test="contains($StrText, '&amp;') and (contains($StrText, '&lt;&gt;') or contains($StrText, 'SCRIPT&gt;'))">
					<xz:text disable-output-escaping="yes">
						<xsl:value-of select="text()"/>	
					</xz:text>
				</xsl:when>
				<xsl:when test="(substring($NormalizedStr, string-length($NormalizedStr) - 3, 4) = '&lt;tr&gt;' and
								not(substring($NormalizedStr, 1, 1) = '&quot;')) or
								substring($NormalizedStr, 1, 18)= '&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD ' or
								($NormalizedStr = '&lt;/TR&gt;' and not(position()=last())) or 
								($NormalizedStr = '&lt;TR&gt;' and not(position()=1))">
					<xz:text disable-output-escaping="yes">
						<xsl:value-of select="text()"/>	
					</xz:text>
				</xsl:when>
				<xsl:when test="
					(substring($NormalizedStr, string-length($NormalizedStr), 1) = '&quot;' or
					substring($NormalizedStr, string-length($NormalizedStr), 1) = $singleQuote) and
					not (substring($Str, 1, 1) = ' ') and not(substring($Str, string-length($Str), 1) = ' ')">
					
						<xsl:value-of select="normalize-space(text())" disable-output-escaping="yes"/>
						
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="text()" disable-output-escaping="yes"/>	
				</xsl:otherwise>
			</xsl:choose>
	</xsl:template>

	<!--ifnew is not a param, since the value of ifnew depends on the current field
	      being evaluated. It's definitely a runtime function provided by SP code which
	     maintains such state. -->
	<xsl:template mode="genTemplates" match="IfNew">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>

		<xz:if test="ddwrt:IfNew(string(@Created))">
			<xsl:apply-templates mode="genTemplates" select="*">
				<xsl:with-param name="currentView" select="$currentView"/>
				<xsl:with-param name="fieldData" select="$fieldData"/>
				<xsl:with-param name="renderPattern" select="$renderPattern"/>
				<xsl:with-param name="Field" select="."/>							
			</xsl:apply-templates>		
		</xz:if>
   </xsl:template>
	
	<xsl:template mode="genTemplates" match="ProjectProperty">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>

		<xsl:text/>web<xsl:text/>		
	</xsl:template>

	<xsl:template mode="genTemplates" match="ListProperty">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>

		<xsl:variable name="prevNode" select="preceding-sibling::*[1]"/>		
		<xsl:variable name="prevStr"     select="normalize-space(string(preceding-sibling::*[1]))"/>
		<xsl:variable name="nextStr"	 select="normalize-space(string(following-sibling::*[1]))"/>		
		<xsl:variable name="StrText"     select="$prevNode/text()"/>
		<xsl:variable name="singleQuote">&#x27;</xsl:variable>
		<xsl:choose>
			<!-- a heuristic : gleaning of sample caml yields the 
				observation that if the htmlencode attribute is set to true, 
				we are looking at something outside of a tag, otherwise
				part of an attribute value 
				an additional heuristic would be to look at the previous sibling and check 
				to see whether it was an <HTML> tag that ended in a quote. 
				the string in use is normalized.
				if the previous node ends with =, then we're inside the <script>tag 
				if it's inside the <script> tag, then we need to do the right thing.
				One more twist: in <ViewEmpty> tag, it's certain that it's simple text.-->	
			<!-- if we're inside or beside a <script>-->
			<xsl:when test="name($prevNode)='HTML' and (contains($StrText, '{') and not (contains($prevStr, '&quot;{'))) or contains($prevStr, 'SCRIPT') or
								 contains($StrText, '&lt;%') or contains($StrText, '%&gt;')">
				<xz:value-of>
					<xsl:attribute name="select">ddwrt:ListProperty('<xsl:value-of select="@Select"/>')</xsl:attribute>
				</xz:value-of>	
			</xsl:when>
			<xsl:when test="substring($nextStr, 1, 1)=';'">
				<xz:value-of>
					<xsl:attribute name="select">ddwrt:ListProperty('<xsl:value-of select="@Select"/>')</xsl:attribute>
				</xz:value-of>
			</xsl:when>
			<xsl:when test="name($prevNode) = 'HTML' and substring($prevStr, string-length($prevStr), 1) = '=' and not(substring($nextStr, 1, 1) = '&quot;') and not(substring($nextStr, 1, 1) = $singleQuote)">
				<xsl:apply-templates mode="genTemplates-inscript" select="."/>
			</xsl:when>				
			<xsl:when test="(@HTMLEncode = 'TRUE' and not(name($prevNode) = 'HTML' and
					(substring($prevStr, string-length($prevStr), 1) = '&quot;' or
					substring($prevStr, string-length($prevStr), 1) = $singleQuote))) or
					(ancestor::ViewEmpty and not(substring($prevStr, 1, 1) = '&lt;'))">
				<xz:value-of>
					<xsl:attribute name="select">ddwrt:ListProperty('<xsl:value-of select="@Select"/>')</xsl:attribute>
				</xz:value-of>
			</xsl:when>
			<xsl:when test="ancestor::Expr or ancestor::Expr1 or ancestor::Expr2">
					<xsl:text/>ddwrt:ListProperty('<xsl:value-of select="@Select"/>')<xsl:text/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text/>{ddwrt:ListProperty('<xsl:value-of select="@Select"/>')}<xsl:text/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
		
	<xsl:template mode="genTemplates-inscript" match="ListProperty">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
			
			<xsl:text/>{ddwrt:ListProperty('<xsl:value-of select="@Select"/>')}<xsl:text/>
	</xsl:template>
		
	<xsl:template mode="genTemplates-inscript" match="ListUrlDir">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
				
			<xsl:choose>
				<xsl:when test="@WebRel">					
					<xz:value-of>
						<xsl:attribute name="select">$<xsl:value-of select="name(.)" disable-output-escaping="yes"/>_<xsl:value-of select="@WebRel"/></xsl:attribute>
					</xz:value-of>
				</xsl:when>
				<xsl:otherwise>
					<xz:value-of>
						<xsl:attribute name="select">$<xsl:value-of select="name(.)" disable-output-escaping="yes"/></xsl:attribute>
					</xz:value-of>
				</xsl:otherwise>
			</xsl:choose>
		
	</xsl:template>

	<xsl:template mode="genTemplates-inscript" match="URL">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>

        <xsl:choose>
            <xsl:when test="@Cmd='Edit'">{$<xsl:value-of select="name(.)" disable-output-escaping="yes"/>_<xsl:value-of select="@Cmd"/>}?ID={@ID}</xsl:when>
            <xsl:otherwise>{$<xsl:value-of select="name(.)" disable-output-escaping="yes"/>_<xsl:value-of select="@Cmd"/>}</xsl:otherwise>
        </xsl:choose>				
	</xsl:template>

	<xsl:template mode="genTemplates-inscript" match="HTML">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>


		<xsl:choose>
			<xsl:when test="ancestor::ScriptQuote[@NotAddingQuote='TRUE']">
				<xsl:value-of select="text()" disable-output-escaping="yes"/>	
			</xsl:when>
			<xsl:otherwise>
				<xz:text script="1">
					<xsl:value-of select="text()"/>	
				</xz:text>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
		
	<xsl:template mode="genTemplates-inscript" match="ScriptQuote">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
			<xsl:if test="not(@NotAddingQuote='TRUE')">
				<xsl:choose>
					<xsl:when test="ancestor::ScriptQuote">
						<xsl:text/>\u0022<xsl:text/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text disable-output-escaping="yes">"</xsl:text>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:if>
			<xsl:choose>
				<xsl:when test="*">
					<xsl:apply-templates mode="genTemplates-inscript" select="*">
						<xsl:with-param name="currentView" select="$currentView"/>
						<xsl:with-param name="fieldData" select="$fieldData"/>
						<xsl:with-param name="renderPattern" select="$renderPattern"/>
						<xsl:with-param name="Field" select="."/>							
					</xsl:apply-templates>					
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="."/>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:if test="not(@NotAddingQuote='TRUE')">
				<xsl:choose>
					<xsl:when test="ancestor::ScriptQuote">
						<xsl:text/>\u0022<xsl:text/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text disable-output-escaping="yes">"</xsl:text>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:if>
	</xsl:template>
	
	<!-- default is to call the regular one -->
	<xsl:template mode="genTemplates-inscript" match="*">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:apply-templates mode="genTemplates" select=".">
			<xsl:with-param name="currentView" select="$currentView"/>
			<xsl:with-param name="fieldData" select="$fieldData"/>
			<xsl:with-param name="renderPattern" select="$renderPattern"/>
			<xsl:with-param name="Field" select="."/>
		</xsl:apply-templates>
	</xsl:template>		
	
	<xsl:template mode="genTemplates-inscript" match="LocaleInfo">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
				
		<xsl:text/><xz:value-of><xsl:attribute name="select">$<xsl:value-of select="name(.)" disable-output-escaping="yes"/>_<xsl:value-of select="@LCTYPE"/></xsl:attribute></xz:value-of><xsl:text/>
	</xsl:template>

<!-- current thinking has getvar being a runtime function		
-->
	<xsl:template mode="genTemplates-inscript" match="GetVar">
		<xz:value-of>
			<xsl:attribute name="select">ddwrt:GetVar('<xsl:value-of select="@Name"/>')</xsl:attribute>
		</xz:value-of>
	</xsl:template>
		
	<xsl:template mode="genTemplates-inscript" match="FieldPrefix">urn:schemas-microsoft-com:office:office#</xsl:template>
	<xsl:template mode="genTemplates" match="FieldPrefix">urn:schemas-microsoft-com:office:office#</xsl:template>

	<xsl:template mode="genTemplates-inscript" match="HttpVDir|ImagesPath|HttpPath|List|ID|Identity">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		
			<xsl:text/>{$<xsl:value-of select="name(.)"/>}<xsl:text/>

	</xsl:template>
		
		
	<xsl:template mode="genTemplates" match="WebQueryInfo">
		<xsl:choose>
		<xsl:when test="ancestor::ScriptQuote">
			<xsl:text/>{$List}-{$View} Summary={$List} xmlns:o=urn:schemas-microsoft-com:office:office o:WebQuerySourceHref={$HttpPath}<xz:text disable-output-escaping="yes">&amp;XMLDATA=1&amp;RowLimit=0&amp;</xz:text>View={$View}<xsl:text/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:text/>{$List}-{$View}" Summary="{$List}" xmlns:o="urn:schemas-microsoft-com:office:office" o:WebQuerySourceHref="{$HttpPath}&amp;XMLDATA=1&amp;RowLimit=0&amp;View={$View}<xsl:text/>
		</xsl:otherwise>	
		</xsl:choose>
	</xsl:template>	
	
	<xsl:template mode="genTemplates" match="HttpVDir|ImagesPath|HttpPath|List|ID|FilterLink|PageUrl|URL">
		<xsl:variable name="prevNode" select="preceding-sibling::*[1]"/>		
		<xsl:variable name="prevStr"     select="normalize-space(string($prevNode))"/>
		<xsl:variable name="StrText"     select="$prevNode/text()"/>

		<xsl:choose>
			<!-- a heuristic : gleaning of sample caml yields the 
				observation that if the htmlencode attribute is set to true, 
				we are looking at something outside of a tag, otherwise
				part of an attribute value 
				an additional heuristic would be to look at the previous sibling and check 
				to see whether it was an <HTML> tag that ended in a quote. 
				the string in use is normalized.
				if the previous node ends with =, then we're inside the <script>tag 
				
				one more thing: if the var is inside listformopening node, it means it's probably
				inside script, except that we're passed </script>, which means the node is preceded
				by some <form or <input tag.-->	
			<xsl:when test="ancestor::ListFormOpening and not(substring-after($prevStr, '&lt;Form')
						or substring-after($prevStr, '&lt;INPUT')) or 
						((parent::Then or parent::Else or parent::Case or parent::Default) and not(preceding-sibling::*[1]))">
				<xz:value-of>
					<xsl:attribute name="select">$<xsl:value-of select="name(.)" disable-output-escaping="yes"/></xsl:attribute>
				</xz:value-of>
			</xsl:when>
			<xsl:otherwise>{$<xsl:value-of select="name(.)" disable-output-escaping="yes"/>}</xsl:otherwise>
		</xsl:choose>	
	</xsl:template>


	<!-- Same as before except that the name also incldes the field name.-->
	<xsl:template mode="genTemplates" match="FieldFilterImageURL">
		<xsl:param name="currentView"/>
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>
		<xsl:param name="ThisField"/>					

		<xsl:variable name="prevStr"     select="normalize-space(string(preceding-sibling::*[1]))"/>
		<xsl:choose>
			<xsl:when test="ancestor::ListFormOpening and not(substring-after($prevStr, '&lt;Form')
						or substring-after($prevStr, '&lt;INPUT'))">
				<xz:value-of>
					<xsl:attribute name="select">$<xsl:value-of select="name(.)" disable-output-escaping="yes"/>_<xsl:value-of select="$fieldData/@Name" disable-output-escaping="yes"/></xsl:attribute>
				</xz:value-of>
			</xsl:when>
			<xsl:otherwise><xsl:text/>{ddwrt:FieldFilterImageUrl('<xsl:value-of select="$fieldData/@Name" disable-output-escaping="yes"/>')}<xsl:text/></xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<!-- Same as before except that the name also incldes the field name.-->
	<xsl:template mode="genTemplates" match="FieldSortImageURL">
		<xsl:param name="currentView"/>
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>
		<xsl:param name="ThisField"/>					

		<xsl:variable name="prevStr"     select="normalize-space(string(preceding-sibling::*[1]))"/>
		<xsl:choose>
			<xsl:when test="ancestor::ListFormOpening and not(substring-after($prevStr, '&lt;Form')
						or substring-after($prevStr, '&lt;INPUT'))">
				<xz:value-of>
					<xsl:attribute name="select">$<xsl:value-of select="name(.)" disable-output-escaping="yes"/>_<xsl:value-of select="$fieldData/@Name" disable-output-escaping="yes"/></xsl:attribute>
				</xz:value-of>
			</xsl:when>
			<xsl:otherwise><xsl:text/>{ddwrt:FieldSortImageUrl('<xsl:value-of select="$fieldData/@Name" disable-output-escaping="yes"/>')}<xsl:text/></xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<!-- Field sort param will be replaced by our DDW sort stuffs, so just output nothing. -->
	<xsl:template mode="genTemplates" match="FieldSortParams">
		<xsl:param name="currentView"/>
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>
		<xsl:param name="ThisField"/>					

	</xsl:template>

	<xsl:template mode="genTemplates-inscript" match="LookupColumn">
		<!-- for stuffs inside urlencode, we just do the {$}, otherwise the full swing-->
		<xsl:choose>
			<xsl:when test="ancestor::ScriptQuote[@NotAddingQuote='TRUE']">{@<xsl:value-of select="@ShowField"/>}</xsl:when>
			<xsl:when test="not(ancestor::URLEncode)">{@<xsl:value-of select="name(.)"/>}<xsl:text/></xsl:when>
			<xsl:otherwise>
				<xz:value-of>
					<xsl:attribute name="select">
						<xsl:text/>/dsQueryResponse/Rows/Row[@ID = $ID]/@<xsl:value-of select="@Name"/><xsl:text/>
					</xsl:attribute>
				</xz:value-of>
			</xsl:otherwise>
		</xsl:choose>
		
	</xsl:template>

	<xsl:template mode="genTemplates" match="HttpHost">
		<xsl:variable name="prevStr"     select="normalize-space(string(preceding-sibling::*[1]))"/>
		
		<xsl:choose>
			<!--an additional heuristic would be to look at the previous sibling and check 
				to see whether it was an <HTML> tag that ended in a quote. 
				the string in use is normalized.
				if the previous node ends with =, then we're inside the <script>tag 
				
				one more thing: if the var is inside listformopening node, it means it's probably
				inside script, except that we're passed </script>, which means the node is preceded
				by some <form or <input tag.-->	
			<xsl:when test="ancestor::ListFormOpening and not(substring-after($prevStr, '&lt;Form')
						or substring-after($prevStr, '&lt;INPUT')) or ((ancestor::URLEncode) and $isAViewAndNotAForm = '0')
						or ((parent::Then or parent::Else or parent::Case or parent::Default) and not(preceding-sibling::*[1]))">
				<xz:value-of>
					<xsl:attribute name="select">$<xsl:value-of select="name(.)" disable-output-escaping="yes"/></xsl:attribute>
				</xz:value-of>
			</xsl:when>
			<xsl:when test="ancestor::SetVar">
				<xsl:text/>$<xsl:value-of select="name(.)" disable-output-escaping="yes"/><xsl:text/>
			</xsl:when>
			<xsl:otherwise>{$<xsl:value-of select="name(.)" disable-output-escaping="yes"/>}</xsl:otherwise>
		</xsl:choose>	
	</xsl:template>

	<!-- these are special "prerendered" caml tags -->		
	<xsl:template mode="genTemplates" match="Identity">
			<xz:value-of><xsl:attribute name="select">$<xsl:value-of select="name(.)" disable-output-escaping="yes"/></xsl:attribute></xz:value-of>
	</xsl:template>
		
	<!-- this sounds crazy, but all the localeInfo should be inside the <script>tag -->
	<xsl:template mode="genTemplates" match="LocaleInfo">
		<xsl:text/><xz:value-of><xsl:attribute name="select">$<xsl:value-of select="name(.)" disable-output-escaping="yes"/>_<xsl:value-of select="@LCTYPE"/></xsl:attribute></xz:value-of><xsl:text/>
	</xsl:template>

	<xsl:template mode="genTemplates" match="Fields">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>
		
		<!-- save this tag in a variable -->
		<xsl:variable name="thisFieldsNode" select="."/>
		<xsl:variable name="formIsNotReadOnly" select="not($isAViewAndNotAForm = '1')"/>
							
		<!-- for each field -->
		<xsl:choose>
			<xsl:when test="$currentView/ViewFields">				
				<xsl:choose>
				<xsl:when test="Switch/Expr/GetVar[@Name='First']">
					<xsl:for-each select="$currentView/ViewFields/FieldRef[position() = 1 and not(@Name = $currentView/ViewBody//FieldProperty/@Name) and not (@Name = $currentView/ViewBody//Field/@Name)]">
					<xsl:variable name="currentField" select="/List/MetaData/Fields/Field[@Name=current()/@Name]"/>					
					<xsl:comment><xsl:value-of select="@Name"/></xsl:comment>
					<xsl:comment><xsl:value-of select="/List/MetaData/Fields/Field[@Name=current()/@Name]/@DisplayName"/></xsl:comment>
					<xsl:choose>
						<xsl:when test="$thisFieldsNode/Switch/Case[@Value='1']">
							<xsl:apply-templates mode="expandCAML" select="$thisFieldsNode/Switch/Case">
								<xsl:with-param name="currentView" select="$currentView"/>
								<xsl:with-param name="currentField" select="$currentField"/>
								<xsl:with-param name="renderPattern" select="$renderPattern"/>
								<xsl:with-param name="Field" select="$Field"/>
							</xsl:apply-templates>				
						</xsl:when>
						<xsl:otherwise>
							<xsl:apply-templates mode="expandCAML" select="$thisFieldsNode/Switch/Default">
								<xsl:with-param name="currentView" select="$currentView"/>
								<xsl:with-param name="currentField" select="$currentField"/>
								<xsl:with-param name="renderPattern" select="$renderPattern"/>
								<xsl:with-param name="Field" select="$Field"/>
							</xsl:apply-templates>				
						</xsl:otherwise>						
					</xsl:choose>	
					
					<xsl:apply-templates mode="genTemplates" select="$thisFieldsNode/Field">
						<xsl:with-param name="currentView" select="$currentView"/>
						<xsl:with-param name="fieldData" select="$currentField"/>
						<xsl:with-param name="renderPattern" select="$renderPattern"/>
						<xsl:with-param name="Field" select="$Field"/>
					</xsl:apply-templates>				

					</xsl:for-each>	
					
					<xsl:for-each select="$currentView/ViewFields/FieldRef[not(position() = 1) and not(@Explicit='TRUE')]">
					<xsl:variable name="currentField" select="/List/MetaData/Fields/Field[@Name=current()/@Name]"/>
					<xsl:comment><xsl:value-of select="@Name"/></xsl:comment>
					<xsl:comment><xsl:value-of select="/List/MetaData/Fields/Field[@Name=current()/@Name]/@DisplayName"/></xsl:comment>
					<xsl:choose>
						<xsl:when test="$thisFieldsNode/Switch/Case[@Value='1']">
							<xsl:apply-templates mode="expandCAML" select="$thisFieldsNode/Switch/Default">
								<xsl:with-param name="currentView" select="$currentView"/>
								<xsl:with-param name="currentField" select="$currentField"/>
								<xsl:with-param name="renderPattern" select="$renderPattern"/>
								<xsl:with-param name="Field" select="$Field"/>
							</xsl:apply-templates>				
						</xsl:when>
						<xsl:otherwise>
							<xsl:apply-templates mode="expandCAML" select="$thisFieldsNode/Switch/Case">
								<xsl:with-param name="currentView" select="$currentView"/>
								<xsl:with-param name="currentField" select="$currentField"/>
								<xsl:with-param name="renderPattern" select="$renderPattern"/>
								<xsl:with-param name="Field" select="$Field"/>
							</xsl:apply-templates>				
						</xsl:otherwise>						
					</xsl:choose>
					
					<xsl:apply-templates mode="genTemplates" select="$thisFieldsNode/Field">
						<xsl:with-param name="currentView" select="$currentView"/>
						<xsl:with-param name="fieldData" select="$currentField"/>
						<xsl:with-param name="renderPattern" select="$renderPattern"/>
						<xsl:with-param name="Field" select="$Field"/>
					</xsl:apply-templates>				
					</xsl:for-each>					
					
				</xsl:when>
				<xsl:otherwise>
				<xsl:for-each select="$currentView/ViewFields/FieldRef[not(@Explicit='TRUE')]">
					<xsl:variable name="currentField" select="/List/MetaData/Fields/Field[@Name=current()/@Name]"/>
					<xsl:comment><xsl:value-of select="@Name"/></xsl:comment>
					<xsl:comment><xsl:value-of select="/List/MetaData/Fields/Field[@Name=current()/@Name]/@DisplayName"/></xsl:comment>
					<xsl:apply-templates mode="expandCAML" select="$thisFieldsNode">
						<xsl:with-param name="currentView" select="$currentView"/>
						<xsl:with-param name="currentField" select="$currentField"/>
						<xsl:with-param name="renderPattern" select="$renderPattern"/>
						<xsl:with-param name="Field" select="$Field"/>
				</xsl:apply-templates>				
				</xsl:for-each>
				</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:for-each select=
					"	/List/MetaData/Fields/Field[
												(	not($currentView/ViewFields) 
												or	$currentView/ViewFields/FieldRef[@Name= ./@Name])
										and	(	not($formIsNotReadOnly) 
												or	not(@ReadOnly='TRUE'))
										and	(not(@Hidden = 'TRUE'))]">

					<!-- make sure the name is only those in the viewfields -->
					<xsl:variable name="currentField" select="."/>
					<xsl:comment><xsl:value-of select="@Name"/></xsl:comment>
					<xsl:comment><xsl:value-of select="@DisplayName"/></xsl:comment>
					<!-- eval this field using the expandCAML template -->
					<xsl:apply-templates mode="expandCAML" select="$thisFieldsNode">
						<xsl:with-param name="currentView" select="$currentView"/>
						<xsl:with-param name="currentField" select="."/>
						<xsl:with-param name="renderPattern" select="$renderPattern"/>
						<xsl:with-param name="Field" select="$Field"/>				
					</xsl:apply-templates>
				</xsl:for-each>			
			</xsl:otherwise>
		</xsl:choose>

	</xsl:template>
	
	<xsl:template mode="expandCAML" match="Fields|Case|Default">
		<xsl:param name="currentView"/>		
		<xsl:param name="currentField"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>
		<xsl:apply-templates mode="genTemplates" select="*">
			<xsl:with-param name="currentView" select="$currentView"/>
			<xsl:with-param name="fieldData" select="$currentField"/>
			<xsl:with-param name="renderPattern" select="$renderPattern"/>
			<xsl:with-param name="Field" select="$Field"/>						
		</xsl:apply-templates>
	</xsl:template>
	
	<!-- genTemplatesInFields generally just falls through to genTemplates
		but in some cases (Property) we want to handle it specially -->
<!--
	<xsl:template mode="genTemplates" match="*">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:apply-templates mode="genTemplates" select=".">
			<xsl:with-param name="currentView" select="$currentView"/>
			<xsl:with-param name="fieldData" select="$fieldData"/>
		</xsl:apply-templates>
	</xsl:template>
-->
	<xsl:template mode="genTemplates" match="Switch|FieldSwitch">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>	
		<xsl:param name="Field"/>
	
		<xsl:variable name="expr" select="Expr"/>
		<xsl:variable name="Str"     select="string(preceding-sibling::*[1])"/>		
		<xsl:variable name="prevNode" select="preceding-sibling::*[1]"/>		
		<xsl:variable name="singleQuote">&#x27;</xsl:variable>			
		<xsl:variable name="prevStr"     select="normalize-space(string(preceding-sibling::*[1]))"/>

		<!-- if the value of the Expr tag is a GetVar, then we have 
		to do runtime evaluation of the switch. If, however, the value
		is a Property (and we have a valid fieldData) then we can do 
		immediate evaluation of the switch and write the result to our
		output xsl. We need to do this for the sake of certain CAML
		for FieldSortImageURL, it means we're inside the <img > tag,
		so we handle it differently, using param.
	      -->
		<xsl:choose>
			<xsl:when test="name(preceding-sibling::*[2]) = 'FieldSortImageURL' or name(preceding-sibling::*[1])='ImagesPath'">{$<xsl:value-of select="generate-id(.)"/>}</xsl:when>
			<xsl:when test="name($prevNode) = 'HTML' and substring($prevStr, string-length($prevStr), 1) = '&quot;' and not (substring($Str, string-length($Str), 1) = ' ')">
				<xsl:text/>{$<xsl:value-of select="generate-id(.)"/>}<xsl:text/>
			</xsl:when>
			<xsl:when test="name(ancestor::*[1])='ViewBody' and not(preceding-sibling::*[1])">
				<xsl:text/>{$<xsl:value-of select="generate-id(.)"/>}<xsl:text/>
			</xsl:when>
			<xsl:when test="name($prevNode) = 'HTML' and (substring($prevStr, string-length($prevStr), 1) = $singleQuote or substring($prevStr, string-length($prevStr)-6, 7)='images/')">
				<xsl:text/>{$<xsl:value-of select="generate-id(.)"/>}<xsl:text/>
			</xsl:when>
			<xsl:when test="Expr/GetVar/@Name='CalView' or Expr/GetVar/@Name='GridView' or Expr/GetVar/@Name='OrderedView'">
				<xsl:choose>
					<xsl:when test="(Case[@Value='1'])">
						<xsl:apply-templates mode="genTemplates" select="Default/*">
							<xsl:with-param name="currentView" select="$currentView"/>
							<xsl:with-param name="fieldData" select="$fieldData"/>
							<xsl:with-param name="renderPattern" select="$renderPattern"/>
							<xsl:with-param name="Field" select="$Field"/>
						</xsl:apply-templates>
					</xsl:when>
					<xsl:otherwise>
						<xsl:apply-templates mode="genTemplates" select="Case/*">
							<xsl:with-param name="currentView" select="$currentView"/>
							<xsl:with-param name="fieldData" select="$fieldData"/>
							<xsl:with-param name="renderPattern" select="$renderPattern"/>
							<xsl:with-param name="Field" select="$Field"/>
						</xsl:apply-templates>										
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="$fieldData and Expr/Property and not($fieldData/@Type='Lookup')">
				<xsl:variable name="propName" select="Expr/Property/@Select"/>
				<xsl:variable name="switchExprValue" select="string($fieldData/@*[name() = $propName])"/>
				<!-- look at the when tags and see if we have a match. -->
				<xsl:variable name="evalWhens" select="Case[@Value = $switchExprValue]"/>
				<xsl:for-each select="Case">
					<xsl:if test="@Value = $switchExprValue">
						<xsl:apply-templates mode="genTemplates" select="*">
							<xsl:with-param name="currentView" select="$currentView"/>
							<xsl:with-param name="fieldData" select="$fieldData"/>
							<xsl:with-param name="renderPattern" select="$renderPattern"/>
							<xsl:with-param name="Field" select="$Field"/>
						</xsl:apply-templates>
					</xsl:if>
				</xsl:for-each>
				
				<xsl:if test="not($evalWhens)">
					<xsl:choose>
					<xsl:when test="Default/*">
					<xsl:apply-templates mode="genTemplates" select="Default/*">
						<xsl:with-param name="currentView" select="$currentView"/>
						<xsl:with-param name="fieldData" select="$fieldData"/>
						<xsl:with-param name="renderPattern" select="$renderPattern"/>
						<xsl:with-param name="Field" select="$Field"/>
					</xsl:apply-templates>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="Default"/>
					</xsl:otherwise>
					</xsl:choose>
				</xsl:if>
			</xsl:when>
			<xsl:when test="Expr/GetVar[@Name='FreeForm'] and count(Expr/*) = 1">
				<xsl:choose>
					<xsl:when test="(Case[@Value='TRUE'] and $FreeForm='0') or
									(Case[@Value='FALSE'] and $FreeForm='1')">
						<xsl:choose>
						<xsl:when test="Default/*">
						<xsl:apply-templates mode="genTemplates" select="Default/*">
							<xsl:with-param name="currentView" select="$currentView"/>
							<xsl:with-param name="fieldData" select="$fieldData"/>
							<xsl:with-param name="renderPattern" select="$renderPattern"/>
							<xsl:with-param name="Field" select="$Field"/>
						</xsl:apply-templates>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="Default"/>
						</xsl:otherwise>
						</xsl:choose>
					</xsl:when>
					<xsl:otherwise>
						<xsl:apply-templates mode="genTemplates" select="Case/*">
							<xsl:with-param name="currentView" select="$currentView"/>
							<xsl:with-param name="fieldData" select="$fieldData"/>
							<xsl:with-param name="renderPattern" select="$renderPattern"/>
							<xsl:with-param name="Field" select="$Field"/>
						</xsl:apply-templates>					
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<!-- below is the version where we basically convert the switch tag to the xsl
			equivalent, the choose tag -->
			<xsl:otherwise>
				<xz:choose>
				<xsl:for-each select="Case">
					<xz:when>
						<xsl:attribute name="test">
							<xsl:choose>
								<xsl:when test="count($expr/*) &gt; 1">
									<xsl:text/>concat(<xsl:for-each select="$expr/*"><xsl:apply-templates mode="genTemplates" select="."><xsl:with-param name="currentView" select="$currentView"/><xsl:with-param name="fieldData" select="$fieldData"/></xsl:apply-templates><xsl:if test="not(position() = last())">,</xsl:if></xsl:for-each>)='<xsl:value-of select="@Value"/>'<xsl:text/>
								</xsl:when>
								<xsl:otherwise>
									<xsl:choose>
										<xsl:when test="$expr/*"><xsl:apply-templates mode="genTemplates" select="$expr/*"><xsl:with-param name="currentView" select="$currentView"/><xsl:with-param name="fieldData" select="$fieldData"/></xsl:apply-templates>='<xsl:value-of select="@Value"/>'<xsl:text/></xsl:when>
										<xsl:otherwise>'<xsl:value-of select="$expr"/>'='<xsl:value-of select="@Value"/>'<xsl:text/></xsl:otherwise>
									</xsl:choose>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:attribute>
						<xsl:choose>
						<xsl:when test="*">
							<xsl:apply-templates mode="genTemplates" select="*">
								<xsl:with-param name="currentView" select="$currentView"/>
								<xsl:with-param name="fieldData" select="$fieldData"/>
								<xsl:with-param name="renderPattern" select="$renderPattern"/>
								<xsl:with-param name="Field" select="$Field"/>
							</xsl:apply-templates>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="."/>
						</xsl:otherwise>
						</xsl:choose>
					</xz:when>
				</xsl:for-each>
				<xsl:for-each select="Default">
					<xz:otherwise>
						<xsl:choose>
						<xsl:when test="*">
							<xsl:apply-templates mode="genTemplates" select="*">
								<xsl:with-param name="currentView" select="$currentView"/>
								<xsl:with-param name="fieldData" select="$fieldData"/>
								<xsl:with-param name="renderPattern" select="$renderPattern"/>
								<xsl:with-param name="Field" select="$Field"/>
							</xsl:apply-templates>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="."/>
						</xsl:otherwise>
						</xsl:choose>
					</xz:otherwise>
				</xsl:for-each>
				</xz:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template mode="genTemplates" match="SetVar">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>

		<xsl:variable name="prevNode" select="preceding-sibling::*[1]"/>		
		<xsl:variable name="prevStr"     select="normalize-space(string(preceding-sibling::*[1]))"/>
		<xsl:variable name="singleQuote">&#x27;</xsl:variable>	


		<xsl:choose>
			<!-- if we're inside a quote, that means we can't put <xsl:value-of> stuffs, instead we need to
				      use a variable, which fortunately has been defined by us.-->
			<xsl:when test="name($prevNode) = 'HTML' and substring($prevStr, string-length($prevStr), 1) = '&quot;'">
				<xsl:apply-templates mode="genTemplates" select="*">
					<xsl:with-param name="currentView" select="$currentView"/>
					<xsl:with-param name="fieldData" select="$fieldData"/>
					<xsl:with-param name="renderPattern" select="$renderPattern"/>
					<xsl:with-param name="Field" select="$Field"/>						
				</xsl:apply-templates>
			</xsl:when>
			<xsl:when test="ListUrlDir"> <!--this is a string, so don't put '' around. -->
				<xz:value-of>
					<xsl:attribute name="select">
						<xsl:text/>ddwrt:SetVar('<xsl:value-of select="@Name"/>', $ListUrlDir)<xsl:text/>
					</xsl:attribute>
				</xz:value-of>				
			</xsl:when>
			<xsl:when test="GetVar">
				<xz:value-of>
					<xsl:attribute name="select">
						<xsl:text/>ddwrt:SetVar('<xsl:value-of select="@Name"/>', concat(<xsl:for-each select="*"><xsl:apply-templates mode="genTemplates" select="."/><xsl:if test="not(position() = last())">,</xsl:if></xsl:for-each>))<xsl:text/>
					</xsl:attribute>
				</xz:value-of>				
			</xsl:when>
			<xsl:when test="*">
				<xsl:choose>
					<xsl:when test="Length">
						<xz:value-of>
							<xsl:attribute name="select">
								<xsl:text/>ddwrt:SetVar('<xsl:value-of select="@Name"/>', string(<xsl:text/><xsl:apply-templates mode="genTemplates" select="child::node()">
									<xsl:with-param name="currentView" select="$currentView"/>
									<xsl:with-param name="fieldData" select="$fieldData"/>
									<xsl:with-param name="renderPattern" select="$renderPattern"/>
									<xsl:with-param name="Field" select="$Field"/>						
								</xsl:apply-templates>
								<xsl:text/>))<xsl:text/>
							</xsl:attribute>
						</xz:value-of>
					</xsl:when>
					<xsl:otherwise>
						<xz:value-of>
							<xsl:attribute name="select">
								<xsl:text/>ddwrt:SetVar('<xsl:value-of select="@Name"/>', '<xsl:text/><xsl:apply-templates mode="genTemplates" select="child::node()">
									<xsl:with-param name="currentView" select="$currentView"/>
									<xsl:with-param name="fieldData" select="$fieldData"/>
									<xsl:with-param name="renderPattern" select="$renderPattern"/>
									<xsl:with-param name="Field" select="$Field"/>						
								</xsl:apply-templates>
								<xsl:text/>')<xsl:text/>
							</xsl:attribute>
						</xz:value-of>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xz:variable>
					<xsl:attribute name="name">Var<xsl:value-of select="generate-id(.)"/></xsl:attribute>
					<xsl:attribute name="select">
						<xsl:text/>ddwrt:SetVar('<xsl:value-of select="@Name"/>', '<xsl:choose><xsl:when test="@Value"><xsl:value-of select="@Value"/></xsl:when><xsl:otherwise><xsl:value-of select="."/></xsl:otherwise></xsl:choose>')<xsl:text/>
					</xsl:attribute>
				</xz:variable>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template mode="genTemplates" match="ScriptQuote">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>
		
		<xsl:variable name="prevNode" select="preceding-sibling::*[1]"/>		
		<xsl:variable name="prevStr"     select="normalize-space(string(preceding-sibling::*[1]))"/>
		<xsl:variable name="singleQuote">&#x27;</xsl:variable>	

		<xsl:choose>  
			<!-- if we're inside a quote, that means we can't put <xsl:value-of> stuffs, instead we need to
				      use a variable, which fortunately has been defined by us.-->
			<xsl:when test="@NotAddingQuote='TRUE'">
				<xsl:apply-templates mode="genTemplates" select="*">
					<xsl:with-param name="currentView" select="$currentView"/>
					<xsl:with-param name="fieldData" select="$fieldData"/>
					<xsl:with-param name="renderPattern" select="$renderPattern"/>
				</xsl:apply-templates>
			</xsl:when>
			<!-- another possibility is <a href="httpvdir"...-->
			<xsl:when test="substring($prevStr, string-length($prevStr), 1) = ','">
				<xsl:text/><xsl:text disable-output-escaping="yes">"</xsl:text><xsl:apply-templates mode="genTemplates" select="*">
					<xsl:with-param name="currentView" select="$currentView"/>
					<xsl:with-param name="fieldData" select="$fieldData"/>
					<xsl:with-param name="renderPattern" select="$renderPattern"/>
				</xsl:apply-templates><xsl:text disable-output-escaping="yes">"</xsl:text><xsl:text/>
			</xsl:when>
			<xsl:when test="(name($prevNode) = 'HTML' and substring($prevStr, string-length($prevStr), 1) = $singleQuote) or (ancestor::URLEncode)">
				<xsl:apply-templates mode="genTemplates-inscript" select="*">
					<xsl:with-param name="currentView" select="$currentView"/>
					<xsl:with-param name="fieldData" select="$fieldData"/>
					<xsl:with-param name="renderPattern" select="$renderPattern"/>
				</xsl:apply-templates>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates mode="genTemplates-inscript" select=".">
					<xsl:with-param name="currentView" select="$currentView"/>
					<xsl:with-param name="fieldData" select="$fieldData"/>
					<xsl:with-param name="renderPattern" select="$renderPattern"/>
				</xsl:apply-templates>
			</xsl:otherwise>
		</xsl:choose>	
	</xsl:template>
	
	<!-- render field value based on field type.-->
	<xsl:template name="GetFieldValue">
		<xsl:param name="columnName"/>
		<xsl:param name="fieldData"/>
		
		<xsl:choose>
			<xsl:when test="$fieldData/@Type='DateTime'">
			    <xsl:choose>
					<xsl:when test="$fieldData/@Format='DateOnly'">
						<xsl:text/>ddwrt:FormatDate(string(@<xsl:value-of select="$columnName"/>),number($Language),1)<xsl:text/>		
					</xsl:when>
					<xsl:otherwise>
						<xsl:text/>ddwrt:FormatDate(string(@<xsl:value-of select="$columnName"/>),number($Language),5)<xsl:text/>
					</xsl:otherwise>
			    </xsl:choose>
			</xsl:when>
			<xsl:when test="$fieldData/@Type = 'Number' or ($fieldData/@Type='Calculated' and $fieldData/@ResultType = 'Number')">
				<xsl:choose>
					<xsl:when test="$fieldData/@Percentage='TRUE'">format-number(string(@<xsl:value-of select="$columnName"/>), '#,##0%;-#')</xsl:when>
					<xsl:otherwise>format-number(string(@<xsl:value-of select="$columnName"/>), '#,##0.###;-#')</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="$fieldData/@Type = 'Counter'">
				<xsl:text/>format-number(string(@<xsl:value-of select="$columnName"/>), '#;-#')<xsl:text/>
			</xsl:when>
			<xsl:when test="$fieldData/@Type = 'URL'">
				<xsl:text/>substring-before(@<xsl:value-of select="$columnName"/>, ', ')<xsl:text/>
			</xsl:when>
			<xsl:otherwise><xsl:text/>@<xsl:value-of select="$columnName"/><xsl:text/>
			</xsl:otherwise>
		</xsl:choose>	
	</xsl:template>

	<xsl:template mode="genTemplates" match="Column">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>		
		<xsl:param name="Field"/>

		<xsl:variable name="columnName">
			<xsl:choose>
				<xsl:when test="@Name"><xsl:value-of select="@Name"/></xsl:when>
				<xsl:otherwise><xsl:value-of select="$fieldData/@Name"/></xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<xsl:variable name="currentField" select="/List/MetaData/Fields/Field[@Name=$columnName] | $fieldData[@Name=$columnName]"/>
		<xsl:variable name="prevNode" select="preceding-sibling::*[1]"/>		
		<xsl:variable name="nextNode" select="following-sibling::*[1]"/>		
		<xsl:variable name="prevStr"     select="normalize-space(string(preceding-sibling::*[1]))"/>
		<xsl:variable name="nextStr"	 select="normalize-space(string(following-sibling::*[1]))"/>

		<!-- if we don't have the column name, don't bother the @ows-->
		<xsl:choose>
		<xsl:when test="not ($columnName = '')">
			<xsl:choose>  
				<xsl:when test="name($prevNode) = 'HTML' and substring($prevStr, string-length($prevStr)-2, 3) = 'ID='">{@ID}</xsl:when>
				<!-- if we're inside a quote, that means we can't put <xsl:value-of> stuffs, instead we need to
				      use a variable, which fortunately has been defined by us.-->
				<xsl:when test="(name($prevNode) = 'HTML' and 
					(substring($prevStr, string-length($prevStr), 1) = '&quot;' or (substring($prevStr, 1, 1) = '&lt;' and not(substring($prevStr, string-length($prevStr), 1)='&gt;')))) or
					(name($nextNode) = 'HTML' and substring($nextStr, 1, 1) = '&quot;')">
					<xsl:text/>{<xsl:call-template name="GetFieldValue">
						<xsl:with-param name="columnName" select="$columnName"/>
						<xsl:with-param name="fieldData" select="$currentField"/>
					</xsl:call-template>}<xsl:text/>
				</xsl:when>
				<!-- if we're preceded by a node that ends with '=', we're already defined this variable -->
				<xsl:when test="name($prevNode) = 'HTML' and 
					substring($prevStr, string-length($prevStr), 1) = '=' and not(substring($nextStr, 1, 1) = '&quot;')">
						<xz:value-of>
							<xsl:attribute name="select">@ID</xsl:attribute>
						</xz:value-of>
				</xsl:when>
				<!-- if we're preceded by a node that ends with ':', we're inside a "" node -->
				<xsl:when test="name($prevNode) = 'HTML' and 
					substring($prevStr, string-length($prevStr), 1) = ':'">{@ID}</xsl:when>
					
				<!-- if we're in the inline field, just output the name-->
				<xsl:when test="@HTMLEncode='TRUE' and ancestor::*[Field] and (not(name($Field) = 'ViewBody') 
									and not(name($Field) = 'ListFormBody'))">
					<xz:value-of>
						<xsl:attribute name="select">
							<xsl:text/>@<xsl:value-of select="$columnName"/><xsl:text/>
						</xsl:attribute>
					</xz:value-of>
				</xsl:when>
				
				<!--if inside the script tag, we'll have to close the cdata section and reopen it afterwards. -->
				<xsl:when test="ancestor::ScriptQuote">
					<xsl:text/>{<xsl:call-template name="GetFieldValue">
						<xsl:with-param name="columnName" select="$columnName"/>
						<xsl:with-param name="fieldData" select="$currentField"/>
					</xsl:call-template>}<xsl:text/>
				</xsl:when>
				<!-- inside expr (1,2), we're just evaluating the @ in a big <xsl:when >-->
				<xsl:when test="ancestor::Expr or ancestor::Expr1 or ancestor::Expr2">
					<xsl:call-template name="GetFieldValue">
						<xsl:with-param name="columnName" select="$columnName"/>
						<xsl:with-param name="fieldData" select="$currentField"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:when test="@HTMLEncode = 'TRUE'">
					<xsl:choose>
						<xsl:when test="$currentField/@Type= 'MultiChoice'">
			                <xz:call-template name="stringreplace">
			                    <xz:with-param name="stringvalue">
			                        <xsl:attribute name="select">@<xsl:value-of select="$columnName"/></xsl:attribute>
			                    </xz:with-param>			                        
			                    <xz:with-param name="from">
			                        <xsl:attribute name="select">';#'</xsl:attribute>
			                    </xz:with-param>
			                    <xz:with-param name="to">
			                        <xsl:attribute name="select">';'</xsl:attribute>
			                    </xz:with-param>
			                </xz:call-template>
			            </xsl:when>
                        <xsl:otherwise>
					    <xz:value-of>
						    <xsl:attribute name="disable-output-escaping">
    							<xsl:choose>
								    <xsl:when test="@AutoNewLine='TRUE'">yes</xsl:when>
    								<xsl:otherwise>no</xsl:otherwise>
							    </xsl:choose>
    						</xsl:attribute>
						    <xsl:attribute name="select">
    							<xsl:choose>
	    							<xsl:when test="$columnName = 'Modified' or $columnName = 'Created'">
		    							<xsl:text/>ddwrt:FormatDate(string(@<xsl:value-of select="$columnName"/>), number($Language), 5)<xsl:text/>
			    					</xsl:when>
				    				<xsl:when test="@AutoNewLine='TRUE'"><xsl:text/>ddwrt:AutoNewLine(string(@<xsl:value-of select="$columnName"/>))<xsl:text/></xsl:when>
					    			<xsl:otherwise>
						    			<xsl:call-template name="GetFieldValue">
							    			<xsl:with-param name="columnName" select="$columnName"/>
								    		<xsl:with-param name="fieldData" select="$currentField"/>
									    </xsl:call-template>
    								</xsl:otherwise>
	    						</xsl:choose>
		    				</xsl:attribute>
			    		</xz:value-of>
			    		</xsl:otherwise>
			    	</xsl:choose>
				</xsl:when>
				<xsl:when test="ancestor::Length">
					<xsl:call-template name="GetFieldValue">
						<xsl:with-param name="columnName" select="$columnName"/>
						<xsl:with-param name="fieldData" select="$currentField"/>
					</xsl:call-template>
				</xsl:when>
				<xsl:when test="(ancestor::Case or ancestor::Default) and not(substring($nextStr, 1, 1) = '&quot;') or (name(ancestor::*[1]) = 'RenderPattern')">
				    <xsl:choose>
						<xsl:when test="$currentField/@Type= 'MultiChoice'">
			                <xz:call-template name="stringreplace">
			                    <xz:with-param name="stringvalue">
			                        <xsl:attribute name="select">@<xsl:value-of select="$columnName"/></xsl:attribute>
			                    </xz:with-param>			                        
			                    <xz:with-param name="from">
			                        <xsl:attribute name="select">';#'</xsl:attribute>
			                    </xz:with-param>
			                    <xz:with-param name="to">
			                        <xsl:attribute name="select">';'</xsl:attribute>
			                    </xz:with-param>
			                </xz:call-template>
			            </xsl:when>
                        <xsl:otherwise>
							<xsl:choose>
								<xsl:when test="$currentField/@Type='Note' and $currentField/@RichText='TRUE'">
									<xz:value-of>
										<xsl:attribute name="disable-output-escaping">yes</xsl:attribute>
			    						<xsl:attribute name="select">
				    						<xsl:call-template name="GetFieldValue">
					    						<xsl:with-param name="columnName" select="$columnName"/>
						    					<xsl:with-param name="fieldData" select="$currentField"/>
											</xsl:call-template>
    									</xsl:attribute>
	    							</xz:value-of>										
								</xsl:when>
								<xsl:otherwise>
									<xsl:text/>{<xsl:call-template name="GetFieldValue">
										<xsl:with-param name="columnName" select="$columnName"/>
										<xsl:with-param name="fieldData" select="$currentField"/>
									</xsl:call-template>}<xsl:text/>
								</xsl:otherwise>
							</xsl:choose>
		                </xsl:otherwise>
		            </xsl:choose>    							
				</xsl:when>
				<xsl:otherwise>
				    <xsl:choose>
						<xsl:when test="$currentField/@Type='Note' and $currentField/@RichText='TRUE'">
							<xz:value-of>
								<xsl:attribute name="disable-output-escaping">yes</xsl:attribute>
			    				<xsl:attribute name="select">
				    				<xsl:call-template name="GetFieldValue">
					    				<xsl:with-param name="columnName" select="$columnName"/>
						    			<xsl:with-param name="fieldData" select="$currentField"/>
									</xsl:call-template>
    							</xsl:attribute>
	    					</xz:value-of>										
						</xsl:when>
                        <xsl:otherwise>
                            <xsl:text/>{<xsl:call-template name="GetFieldValue">
								<xsl:with-param name="columnName" select="$columnName"/>
								<xsl:with-param name="fieldData" select="$currentField"/>
							</xsl:call-template>}<xsl:text/>
						</xsl:otherwise>
                    </xsl:choose>						
				</xsl:otherwise>
			</xsl:choose>
		</xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template mode="genTemplates" match="Column2">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>		
		<xsl:param name="Field"/>

		<xsl:variable name="prevNode" select="preceding-sibling::*[1]"/>		
		<xsl:variable name="prevStr"     select="normalize-space(string(preceding-sibling::*[1]))"/>
		<xsl:variable name="nextStr"	 select="normalize-space(string(following-sibling::*[1]))"/>

		<!-- column2 means the descriptive text (Comments)-->
		<xsl:variable name="columnName">
			<xsl:choose>
				<xsl:when test="@Name"><xsl:value-of select="@Name"/></xsl:when>
				<xsl:otherwise><xsl:value-of select="$fieldData/@Name"/></xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		
		<xsl:choose>  <!-- if we're in the lookupRP field, just output the name-->
			<!-- if we're inside a quote, that means we can't put <xsl:value-of> stuffs, instead we need to
				  use a variable, which fortunately has been defined by us.-->
			<xsl:when test="name($prevNode) = 'HTML' and 
				(substring($prevStr, string-length($prevStr), 1) = '&quot;' or substring($nextStr, 1, 1) = '&quot;')">
				<xsl:text/>{substring-after(@<xsl:value-of select="$columnName"/>, ', ')}<xsl:text/>
			</xsl:when>

			<xsl:when test="@HTMLEncode='TRUE' and ancestor::*[Field] and (not(name($Field) = 'ViewBody')) and (not(name($Field) = 'ListFormBody'))">
					<xsl:text/><xsl:value-of select="substring-after($columnName, ', ')"/><xsl:text/>
			</xsl:when>
			<xsl:when test="@HTMLEncode = 'TRUE' or ancestor::*[ScriptQuote]">
				 <xz:value-of>
					<xsl:attribute name="select">
						<xsl:text/>substring-after(@<xsl:value-of select="$columnName"/>, ', ')<xsl:text/>
					</xsl:attribute>
				</xz:value-of>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text/>substring-after(@<xsl:value-of select="$columnName"/>, ', ')<xsl:text/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template mode="genTemplates" match="InForm">
		<xsl:text/>'FALSE'<xsl:text/>
	</xsl:template>

	<xsl:template mode="genTemplates" match="LookupColumn">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>

		<xsl:variable name="columnName">
			<xsl:choose>
				<xsl:when test="@Name"><xsl:value-of select="@Name"/></xsl:when>
				<xsl:when test="@ShowField"><xsl:value-of select="@ShowField"/></xsl:when>
				<xsl:otherwise><xsl:value-of select="$fieldData/@Name"/></xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="prevStr" select="normalize-space(string(preceding-sibling::*[1]))"/>
		<xsl:variable name="prevNode" select="preceding-sibling::*[1]"/>
		<xsl:variable name="fieldData2" select="$fieldData[name()='Field'] | /List/MetaData/Fields/Field[@Name=current()/@Name]"/>
		
		<!-- if we don't have the column name, don't bother the @ows-->
		<xsl:choose>
		<xsl:when test="not ($columnName = '')">
			<xsl:choose>  <!-- if we're in the inline field, just output the name-->
				<xsl:when test="@HTMLEncode='TRUE' and ancestor::*[Field] and (not(name($Field) = 'ViewBody')) and (not(name($Field) = 'ListFormBody'))">
					<xz:value-of>
						<xsl:attribute name="select">
							<xsl:text/>@<xsl:value-of select="$columnName"/><xsl:text/>
						</xsl:attribute>
					</xz:value-of>
				</xsl:when>
				<!-- another heuristic: notaddingquote is set, then it should be just @..
				also anything really inside default or case tag should be enclosed with <value-of>>-->
				<xsl:when test="ancestor::ScriptQuote[@NotAddingQuote]">
					<xsl:choose>
						<xsl:when test="@ShowField='EMail'">
							<xsl:text/>{ddwrt:UserLookup(string(@<xsl:value-of select="$fieldData/@Name"/>), string($EMail))}<xsl:text/>
						</xsl:when>
						<xsl:when test="@ShowField">
							<!--<xsl:text/>{ddwrt:UserLookup("@ID", "<xsl:value-of select="@ShowField"/>")}<xsl:text/>-->
							<xsl:text/>{$UserLookup<xsl:value-of select="generate-id($fieldData)"/><xsl:value-of select="generate-id(.)"/>}<xsl:text/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text/>{@<xsl:value-of select="$columnName"/>}<xsl:text/>
						</xsl:otherwise>
					</xsl:choose>	
				</xsl:when>
				<!-- if it's after / (fieldref), or ".."-->
				<xsl:when test="name($prevNode) = 'HTML' and (substring($prevStr, string-length($prevStr), 1) = '/' or substring($prevStr, 1, 1) = '/' or substring($prevStr, string-length($prevStr), 1) = '&quot;')">
					<xsl:text/>{@<xsl:value-of select="$columnName"/>}<xsl:text/>
				</xsl:when>
				<xsl:when test="@ShowField">
					<xsl:text/>ddwrt:UserLookup(string(@<xsl:value-of select='$fieldData/@Name'/>), "<xsl:value-of select='@ShowField'/>")<xsl:text/>
				</xsl:when>				
				<xsl:when test="(@HTMLEncode = 'TRUE' and not(ancestor::RenderPattern)) or 
							(ancestor::ScriptQuote[not (@NotAddingQuote)]) or
							((ancestor::Default or ancestor::Case) and not(ancestor::Expr or ancestor::Expr1 or ancestor::Expr2))">
					<xz:value-of>
						<xsl:attribute name="select">
							<xsl:choose>
								<!-- for some internal readonly fields, we have special handling -->
								<xsl:when test="$fieldData2/@Type='Lookup' and $fieldData2/@List='Docs' and ($columnName='Last_x0020_Modified' or $columnName='Created_x0020_Date')">ddwrt:FormatDate(string(@<xsl:value-of select="$columnName"/>),number($Language),5)</xsl:when>
								<xsl:when test="$fieldData2/@Type='Lookup' and $fieldData2/@List='Docs' and $columnName='File_x0020_Size'">floor(number(@<xsl:value-of select="$columnName"/>) div 1024) + 1</xsl:when>
								<xsl:otherwise>
									<xsl:text/>@<xsl:value-of select="$columnName"/><xsl:text/>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:attribute>
					</xz:value-of>
				</xsl:when>
				<!-- if we're inside a form, we'll do the lookup-->
				<xsl:when test="$isAViewAndNotAForm = '0' and ancestor::ScriptQuote">
					<xz:value-of>
						<xsl:attribute name="select">
							<xsl:text/>/dsQueryResponse/Rows/Row[@ID = $ID]/@<xsl:value-of select="$columnName"/><xsl:text/>
						</xsl:attribute>
					</xz:value-of>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text/>@<xsl:value-of select="$columnName"/><xsl:text/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:when>
		</xsl:choose>
	</xsl:template>
	

	<xsl:template mode="genTemplates" match="FieldFilterOptions">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>
		
		<xsl:variable name="FilterOptionsListName">
			<xsl:choose>
				<xsl:when test="$Field">optionsList<xsl:value-of select="generate-id($Field)"/></xsl:when>
				<xsl:otherwise>optionsList<xsl:value-of select="generate-id($Field)"/></xsl:otherwise>
			</xsl:choose>
		</xsl:variable>		

		<xz:variable>
			<xsl:attribute name="name"><xsl:value-of select="$FilterOptionsListName"/></xsl:attribute>
			<xsl:attribute name="select">ddwrt:FieldFilterOptions('<xsl:value-of select="$fieldData/@Name"/>')</xsl:attribute>
		</xz:variable>
		<xz:for-each>
			<!-- todo: intentionally leave out the $ below since the run time is not ready yet -->
			<xsl:attribute name="select">$<xsl:value-of select="$FilterOptionsListName"/></xsl:attribute>
			<option>
				<xz:attribute name="value"><xz:value-of select="@value"/></xz:attribute>
				<xz:value-of select="@display"/>
			</option>
		</xz:for-each>	
	</xsl:template>
	
	<xsl:template mode="genTemplates" match="URLEncode">
	<xsl:param name="currentView"/>
		<!-- concat the url to be encoded, unless it has only one child-->
		<xz:variable>
			<xsl:attribute name="name">URLStr<xsl:value-of select="generate-id($currentView)"/></xsl:attribute>
			<xsl:choose>
				<xsl:when test="last() = 1">
					<xsl:apply-templates mode="genTemplates" select="*"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:attribute name="select">
						<xsl:text/>concat(<xsl:for-each select="*"><xsl:apply-templates mode="genTemplates" select="."/><xsl:if test="not(position() = last())">,</xsl:if></xsl:for-each>)<xsl:text/>
					</xsl:attribute>
				</xsl:otherwise>
			</xsl:choose>	
		</xz:variable>
		
		<xsl:variable name="name">URLStr<xsl:value-of select="generate-id($currentView)"/></xsl:variable>
		<xz:value-of>
			<xsl:attribute name="select">
				<xsl:text/>ddwrt:URLEncode('$<xsl:value-of select="$name"/>')<xsl:text/>
			</xsl:attribute>
		</xz:value-of>
	</xsl:template>
	
	<xsl:template mode="genVariables" match="Limit">
		<xsl:param name="currentView"/>		

		<xz:variable>
			<xsl:attribute name="name">LimitStr<xsl:value-of select="generate-id($currentView)"/></xsl:attribute>
			<xsl:attribute name="select">
				<xsl:text/>concat(<xsl:for-each select="*">
							<xsl:apply-templates mode="genTemplates" select="."/><xsl:if test="not(position() = last())">,</xsl:if>
							</xsl:for-each>)<xsl:text/>
			</xsl:attribute>
		</xz:variable>
		
	</xsl:template>
	
	<xsl:template mode="genTemplates" match="Limit">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>

		<!--concat all the strings inside the <limit> tag -->
		<xsl:variable name="prevNode" select="preceding-sibling::*[1]"/>		
		<xsl:variable name="prevStr"     select="normalize-space(string(preceding-sibling::*[1]))"/>

		<xsl:variable name="columnName" select="Column/@Name"/>
		<xsl:variable name="currentField" select="/List/MetaData/Fields/Field[@Name=$columnName] | $fieldData[@Name=$columnName]"/>

		<!--params-->
		<xsl:variable name="AutoHyperLink">
			<xsl:choose>
				<xsl:when test="@AutoHyperLink"><xsl:value-of select="@AutoHyperLink"/></xsl:when>
				<xsl:otherwise>TRUE</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="AutoNewLine">
			<xsl:choose>
				<xsl:when test="@AutoNewLine"><xsl:value-of select="@AutoNewLine"/></xsl:when>
				<xsl:otherwise>TRUE</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="HTMLEncode">
			<xsl:choose>
				<xsl:when test="@HTMLEncode"><xsl:value-of select="@HTMLEncode"/></xsl:when>
				<xsl:otherwise>TRUE</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="Len">
			<xsl:choose>
				<xsl:when test="@Len"><xsl:value-of select="@Len"/></xsl:when>
				<xsl:otherwise>128</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="MoreText">
			<xsl:choose>
				<xsl:when test="@MoreText"><xsl:value-of select="@MoreText"/></xsl:when>
				<xsl:otherwise>NULL</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="StripWS">
			<xsl:choose>
				<xsl:when test="@StripWS"><xsl:value-of select="@StripWS"/></xsl:when>
				<xsl:otherwise>TRUE</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<!--if the previous node is an html node that ends with ", we'll do {ddwrt:} directly.
		      by default, len is 128 and moreText is ... -->				
		<xsl:choose>
			<xsl:when test="name($prevNode) = 'HTML' and substring($prevStr, string-length($prevStr), 1) = '&quot;'">
				<xsl:text/>{ddwrt:Limit($LimitStr<xsl:value-of select="generate-id($Field)"/>, '<xsl:value-of select="$Len"/>', '<xsl:value-of select="$MoreText"/>')}<xsl:text/>
			</xsl:when>
			<xsl:when test="ancestor::Fields and not(ancestor::Field)">
				<xz:variable>
					<xsl:attribute name="name">LimitStr<xsl:value-of select="generate-id($Field)"/></xsl:attribute>
					<xsl:apply-templates mode="genTemplates" select="*">
						<xsl:with-param name="currentView" select="$currentView"/>
						<xsl:with-param name="fieldData" select="$fieldData"/>
						<xsl:with-param name="renderPattern" select="$renderPattern"/>
						<xsl:with-param name="Field" select="$Field"/>
					</xsl:apply-templates>	
				</xz:variable>
				<xz:value-of>
					<xsl:attribute name="select">
						<xsl:text/>ddwrt:Limit($LimitStr<xsl:value-of select="generate-id($Field)"/>, '<xsl:value-of select="$Len"/>', '<xsl:value-of select="$MoreText"/>')<xsl:text/>
					</xsl:attribute>
				</xz:value-of>
			</xsl:when>
			<xsl:otherwise>
				<xz:value-of>
					<xsl:attribute name="disable-output-escaping">
						<xsl:choose>
							<xsl:when test="$currentField/@Type='Note' and $currentField/@RichText='TRUE'">yes</xsl:when>
							<xsl:otherwise>no</xsl:otherwise>
						</xsl:choose>
					</xsl:attribute>
					<xsl:attribute name="select">ddwrt:Limit(string(@<xsl:value-of select="Column/@Name"/>), <xsl:value-of select="$Len"/>, '...')</xsl:attribute>
				</xz:value-of>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template mode="genTemplates" match="SelectOptions">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>
		<xsl:param name="ThisField"/>
			
		<xsl:variable name="optionsListName">FilterOptionsList<xsl:value-of select="$fieldData/@Name"/></xsl:variable>
		
		<xz:variable>
			<xsl:attribute name="name"><xsl:value-of select="$optionsListName"/></xsl:attribute>
			<xsl:attribute name="select">ddwrt:GetFieldOptions('<xsl:value-of select="$fieldData/@Name"/>')</xsl:attribute>
		</xz:variable>
		<xz:for-each>
			<!-- todo: intentionally leave out the $ below since the run time is not ready yet -->
			<xsl:attribute name="select">$<xsl:value-of select="$optionsListName"/></xsl:attribute>
			<option>
				<xz:attribute name="value"><xz:value-of select="@value"/></xz:attribute>
				<xz:value-of select="@display"/>
			</option>
		</xz:for-each>
	</xsl:template>

	<xsl:template mode="genTemplates" match="Property">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>
		
		<xsl:variable name="propertyNode" select="."/>
		<xsl:choose>
			<!-- if we have fielddata, then we resolve that way -->
			<xsl:when test="$fieldData and $fieldData/@*[name() = $propertyNode/@Select]">
				<xsl:choose>
					<!-- a heuristic : gleaning of sample caml yields the 
						observation that if the htmlencode attribute is set to true, 
						we are looking at something outside of a tag, otherwise
						part of an attribute value -->
					<xsl:when test="@HTMLEncode = 'TRUE'">
						<xsl:value-of disable-output-escaping="yes" select="ddwrt:AutoNewLine(string($fieldData/@*[name() = $propertyNode/@Select]))"/>
					</xsl:when>
					<xsl:when test="(name(ancestor::*[1])='Expr' or name(ancestor::*[1])='Expr1' or name(ancestor::*[1])='Expr2') and count(ancestor::*[1]/*) = 1"> <!--just need the field name text -->
						<xsl:text/>'<xsl:value-of select="$fieldData/@*[name() = $propertyNode/@Select]"/>'<xsl:text/>
					</xsl:when>
					<xsl:otherwise>
							<xsl:text/><xsl:value-of select="$fieldData/@*[name() = $propertyNode/@Select]"/><xsl:text/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<!-- otherwise we resolve with the ddwrt:ListProperty -->
			<xsl:otherwise>
				<xsl:choose>
					<!-- a heuristic : gleaning of sample caml yields the 
						observation that if the htmlencode attribute is set to true, 
						we are looking at something outside of a tag, otherwise
						part of an attribute value -->
					<xsl:when test="@HTMLEncode = 'TRUE'">
						<xz:value-of>
							<xsl:attribute name="select">
								<xsl:text/>ddwrt:ListProperty('<xsl:value-of select="@Select"/>')<xsl:text/>
							</xsl:attribute>
						</xz:value-of>
					</xsl:when>
					<!-- here we have a property that actually lives inside a <script> tag!-->
					<xsl:when test="(ancestor::Row[Field[@Name = 'TypeName'] = 'Text'] and ancestor::RenderPattern[@Name= 'NewPattern']) or (ancestor::Row[Field[@Name = 'TypeName'] = 'URL'] and ancestor::RenderPattern[@Name= 'NewPattern' or @Name = 'EditPattern'])">
						<xz:value-of>
							<xsl:attribute name="select">
								<xsl:text/>ddwrt:ListProperty('<xsl:value-of select="@Select"/>')<xsl:text/>
							</xsl:attribute>
						</xz:value-of>	
					</xsl:when>
					<xsl:when test="ancestor::Expr or ancestor::Expr1 or ancestor::Expr2">
							<xsl:text/>ddwrt:GetVar('<xsl:value-of select="@Select"/>')<xsl:text/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text/>{ddwrt:GetVar('<xsl:value-of select="@Select"/>')}<xsl:text/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template mode="genTemplates" match="IsPrivilegedUser">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>

		<xsl:text/>ddwrt:IsPrivilegedUser()<xsl:text/>
	</xsl:template>

	<xsl:template mode="genTemplates" match="ListUrlDir">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>
		
		<xsl:choose>
			<!-- a heuristic : gleaning of sample caml yields the 
				observation that if the htmlencode attribute is set to true, 
				we are looking at something outside of a tag, otherwise
				part of an attribute value -->

			<xsl:when test="@WebRel">
				<xsl:choose>
					<xsl:when test="@HTMLEncode = 'TRUE' or ancestor::ListFormOpening">
						<xz:value-of>
							<xsl:attribute name="select">
								<xsl:text/>$<xsl:value-of select="name(.)" disable-output-escaping="yes"/>_<xsl:value-of select="@WebRel"/><xsl:text/>
							</xsl:attribute>
						</xz:value-of>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text/>{$<xsl:value-of select="name(.)" disable-output-escaping="yes"/>_<xsl:value-of select="@WebRel"/>}<xsl:text/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:choose>
				<xsl:when test="ancestor::Expr or ancestor::Expr1 or ancestor::Expr2 or ancestor::SetVar">
					<xsl:text/>$<xsl:value-of select="name(.)" disable-output-escaping="yes"/><xsl:text/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text/>{$<xsl:value-of select="name(.)" disable-output-escaping="yes"/>}<xsl:text/>
				</xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template mode="genTemplates" match="URL">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>
		
			<xsl:choose>
			<!-- a heuristic : gleaning of sample caml yields the 
				observation that if the htmlencode attribute is set to true, 
				we are looking at something outside of a tag, otherwise
				part of an attribute value -->
			<xsl:when test="@HTMLEncode = 'TRUE'">
				<xz:value-of>
					<xsl:attribute name="select">
						<xsl:text/>concat($PageUrl, '?ID=', @ID)<xsl:text/>
					</xsl:attribute>
				</xz:value-of>
			</xsl:when>
			<xsl:when test="not(@Cmd) or ancestor::DisplayPattern or ancestor::DisplayBidiPattern">
				<xsl:choose>
					<!--if we're inside display field, should display the item url instead of the page url. -->
					<xsl:when test="ancestor::Field or not(@Cmd)">{$URL_Display}?ID={@ID}</xsl:when>
					<xsl:otherwise>{$PageUrl}?ID={@ID}</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="@Cmd='Lookup'">
				<xsl:text/>{ddwrt:URLLookup('<xsl:value-of select="$fieldData/@List"/>', '<xsl:value-of select="$fieldData/@Name"/>', string(@ID))}<xsl:text/>
			</xsl:when>
			<xsl:otherwise>
				<!-- new just return the url, otherwise add the id=? stuffs-->
				<xsl:choose>
					<xsl:when test="@Cmd = 'New' or @Cmd='Lookup'">			
						<xsl:text/>{$<xsl:value-of select="name(.)" disable-output-escaping="yes"/>_<xsl:value-of select="@Cmd"/>}<xsl:text/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text/>{$<xsl:value-of select="name(.)" disable-output-escaping="yes"/>_<xsl:value-of select="@Cmd"/>}?ID={@ID}<xsl:text/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template mode="genTemplates" match="GetVar">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>

		<xsl:variable name="prevNode" select="preceding-sibling::*[1]"/>		
		<xsl:variable name="prevStr"     select="normalize-space(string(preceding-sibling::*[1]))"/>
		<xsl:variable name="nextStr"     select="normalize-space(string(following-sibling::*[1]))"/>
		<xsl:variable name="singleQuote">&#x27;</xsl:variable>	
		<xsl:choose>
			<!-- a heuristic : gleaning of sample caml yields the 
				observation that if the htmlencode attribute is set to true, 
				we are looking at something outside of a tag, otherwise
				part of an attribute value -->
			<xsl:when test="name($prevNode)='HTML' and substring($prevStr, string-length($prevStr), 1)='=' and @Name='NumColumns'">
				<xsl:value-of select="count($currentView/ViewFields/FieldRef)"/>
			</xsl:when>
			<xsl:when test="@HTMLEncode = 'TRUE' and not(name($prevNode) = 'HTML' and 
					(substring($prevStr, string-length($prevStr), 1) = '&quot;' or
					substring($prevStr, string-length($prevStr), 1) = $singleQuote)) ">
				<xz:value-of>
					<xsl:attribute name="select">
						<xsl:text/>ddwrt:GetVar('<xsl:value-of select="@Name"/>')<xsl:text/>
					</xsl:attribute>
				</xz:value-of>
			</xsl:when>
			<!-- if we're inside a <script>-->
			<xsl:when test="substring($prevStr, 1, 1)=';' or substring($prevStr, 1, 1) = ':' or substring($prevStr, string-length($prevStr), 1) = ';' or substring($nextStr, 1, 1) = ';' or contains($prevStr, '&lt;SCRIPT&gt;')">
				<xz:value-of>
					<xsl:attribute name="select">
						<xsl:text/>ddwrt:GetVar('<xsl:value-of select="@Name"/>')<xsl:text/>
					</xsl:attribute>
				</xz:value-of>				
			</xsl:when>
			<!-- if the var is inside listformopening node, it means it's probably
				inside script, except that we're passed </script>, which means the node is preceded
				by some <form or <input tag.
				also if it's inside switch, we'll do the normal thing for it.-->	
			<xsl:when test="ancestor::ListFormOpening and not(substring-after($prevStr, '&lt;Form')
					or substring-after($prevStr, '&lt;INPUT')) and not(ancestor::Switch)">
				<xz:value-of>
					<xsl:attribute name="select">
						<xsl:text/>ddwrt:GetVar('<xsl:value-of select="@Name"/>')<xsl:text/>
					</xsl:attribute>
				</xz:value-of>					
			</xsl:when>

			<!-- if we're inside the ifEqual, we're just variables inside the a=b pattern-->
			<xsl:when test="ancestor::IfEqual and (ancestor::Expr1 or ancestor::Expr2)">
				<xsl:text/><xsl:choose>
					<xsl:when test="@Name='Threaded'"><xsl:choose><xsl:when test="$isThreaded='TRUE'">'TRUE'</xsl:when><xsl:otherwise>'FALSE'</xsl:otherwise></xsl:choose></xsl:when>
					<xsl:otherwise>
						<xsl:text/>ddwrt:GetVar('<xsl:value-of select="@Name"/>')<xsl:text/>
					</xsl:otherwise>
				</xsl:choose><xsl:text/>				
			</xsl:when>
			<xsl:when test="ancestor::Limit or ancestor::Expr or ancestor::Expr1 or ancestor::Expr2">
				<xsl:choose>
					<xsl:when test="@Name='SortDisable'">'TRUE'</xsl:when> <!-- todo: add in the schema sortable info-->
					<xsl:when test="@Name='Threaded'"><xsl:choose><xsl:when test="$isThreaded='TRUE'">'TRUE'</xsl:when><xsl:otherwise>'FALSE'</xsl:otherwise></xsl:choose></xsl:when>
					<xsl:when test="@Name='FreeForm'"><xsl:choose><xsl:when test="$FreeForm='1'">'TRUE'</xsl:when><xsl:otherwise>''</xsl:otherwise></xsl:choose></xsl:when>
					<xsl:otherwise>
						<xsl:text/>ddwrt:GetVar('<xsl:value-of select="@Name"/>')<xsl:text/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="@Name='View'">
				<xsl:text/>{$View}<xsl:text/>
			</xsl:when>
			<xsl:when test="ancestor::SetVar">
				<xsl:text/>ddwrt:GetVar('<xsl:value-of select="@Name"/>')<xsl:text/>
			</xsl:when>
			<xsl:otherwise>
			<xsl:text/>{ddwrt:GetVar('<xsl:value-of select="@Name"/>')}<xsl:text/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
		
	<xsl:template name="FieldExpandTempl" mode="genTemplates" match="Field[not (@*)]">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>
		
		<xsl:variable name="fieldData2" select="$fieldData[name()='Field'] | /List/MetaData/Fields/Field[@Name=current()/@Name]"/>
		<xsl:variable name="inlineRP" select="$fieldData2/DisplayPattern"/>
		<xsl:variable name="lookupRP" select="$fldTypes/List/Data/Rows/Row[Field[@Name = 'TypeName'] = $fieldData2/@Type]/RenderPattern[@Name = $renderPattern]"/>

		<!-- need to find the appropriate render pattern and run it -->
		<xsl:choose>
			<xsl:when test="ancestor::Expr or ancestor::Expr1 or ancestor::Expr2 or ancestor::SetVar">
				<xsl:text/>@<xsl:value-of select="$fieldData2/@Name"/><xsl:text/>
			</xsl:when>
			<xsl:when test="$inlineRP and ($renderPattern = 'DisplayPattern')">
				<!-- create the Length variable before hand, using xslt string function-->
				<xsl:apply-templates select="$inlineRP//Length | $inlineRP//Field" mode="genVariables">
					<xsl:with-param name="currentView" select="$Field"/>
					<xsl:with-param name="renderPattern" select="$renderPattern"/>
				</xsl:apply-templates>
				
				<xsl:apply-templates mode="genTemplates" select="$inlineRP/*">
					<xsl:with-param name="currentView" select="$currentView"/>
					<xsl:with-param name="fieldData" select="$fieldData"/>
					<xsl:with-param name="renderPattern" select="$renderPattern"/>
					<xsl:with-param name="Field" select="$Field"/>
					<xsl:with-param name="ThisField" select="."/>
				</xsl:apply-templates>
			</xsl:when>
			<xsl:when test="$lookupRP">		
				<!-- make reference to the @comments  and other needed <column> beforehand-->
			     <xsl:apply-templates select=" $lookupRP//Column" mode="genParams">
					<xsl:with-param name="currentList" select="$lookupRP"/>
					<xsl:with-param name="viewNode" select="$fieldData2"/>
				</xsl:apply-templates>			
	 
				<!-- variable for switch inside recurrence field -->
				<xsl:if test="$lookupRP//Switch|$lookupRP//FieldSwitch">
					<xsl:apply-templates select="$lookupRP//Switch|$lookupRP//FieldSwitch" mode="genVariablesInField">
						<xsl:with-param name="currentView" select="$Field"/>
						<xsl:with-param name="fieldData" select="$fieldData"/>
						<xsl:with-param name="realFieldData" select="$fieldData2"/>
						<xsl:with-param name="renderPattern" select="$renderPattern"/>
					</xsl:apply-templates>		
				</xsl:if>	

				<!-- variable for lookupcolumn inside user field -->
				<xsl:if test="$lookupRP//LookupColumn">
					<xsl:apply-templates select="$lookupRP//LookupColumn" mode="genVariablesInField">
						<xsl:with-param name="currentView" select="$Field"/>
						<xsl:with-param name="fieldData" select="$fieldData"/>		
						<xsl:with-param name="realFieldData" select="$fieldData2"/>
						<xsl:with-param name="renderPattern" select="$renderPattern"/>
					</xsl:apply-templates>		
				</xsl:if>	
				
				<!-- create the Limit variable before hand, using xslt string function-->
				<xsl:apply-templates select="$lookupRP//Limit" mode="genVariables">
					<xsl:with-param name="currentView" select="$Field"/>
				</xsl:apply-templates>

				<xsl:apply-templates mode="genTemplates" select="$lookupRP/*">
					<xsl:with-param name="currentView" select="$currentView"/>
					<xsl:with-param name="fieldData" select="$fieldData2"/>
					<xsl:with-param name="renderPattern" select="$renderPattern"/>
					<xsl:with-param name="Field" select="$Field"/>
					<xsl:with-param name="ThisField" select="."/>					
				</xsl:apply-templates>
			</xsl:when>
			
			<xsl:when test="$renderPattern = 'NewPattern'">
					<xsl:call-template name="FieldExpandTempl">
					<xsl:with-param name="currentView" select="$currentView"/>
					<xsl:with-param name="fieldData" select="$fieldData"/>
					<xsl:with-param name="renderPattern" select="'EditPattern'"/>
					<xsl:with-param name="Field" select="."/>		
					<xsl:with-param name="ThisField" select="."/>								
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<foobar-somethingbad>
					<xsl:copy-of select="$fieldData2"/>
				</foobar-somethingbad>
			<!--
				<xsl:call-template name="FieldExpandTempl">
					<xsl:with-param name="currentView" select="$currentView"/>
					<xsl:with-param name="fieldData" select="$fieldData"/>
					<xsl:with-param name="renderPattern" select="'DisplayPattern'"/>
				</xsl:call-template>
			-->
			</xsl:otherwise>
		</xsl:choose>

	</xsl:template>
		

	<!--Inside script, just output the field name.-->
	<xsl:template mode="genTemplates-inscript" match="Field[@Name]">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>
		
		<xsl:text/>{@<xsl:value-of select="@Name"/>}<xsl:text/>
	</xsl:template>

	<xsl:template mode="genVariables" match="Field[@Name]">
		<xsl:param name="currentView"/>
		<xsl:param name="renderPattern"/>
			
		<xsl:if test="ancestor::Field and /List/MetaData/Fields/Field[@Name=current()/@Name][@Type='Lookup']">
			<xz:variable>
				<xsl:attribute name="name">Field<xsl:value-of select="generate-id(.)"/></xsl:attribute>
					<xsl:apply-templates mode="genTemplates" select="$fldTypes/List/Data/Rows/Row[Field[@Name = 'TypeName'] = 'Lookup']/RenderPattern[@Name = $renderPattern]/*">
						<xsl:with-param name="currentView" select="$currentView"/>
						<xsl:with-param name="fieldData" select="."/>
						<xsl:with-param name="renderPattern" select="."/>
						<xsl:with-param name="Field" select="."/>						
					</xsl:apply-templates>
			</xz:variable>
		</xsl:if>
	</xsl:template>

	<xsl:template mode="genTemplates" match="Field[@Name]">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>

		<xsl:variable name="prevStr"     select="normalize-space(string(preceding-sibling::*[1]))"/>
		<xsl:choose>
			<xsl:when test="@InAttr='TRUE'">
				<xsl:text/>@<xsl:value-of select="@Name"/><xsl:text/>
			</xsl:when>		
			<xsl:when test="not(ancestor::Fields)">
				<xsl:call-template name="FieldExpandTempl">
					<xsl:with-param name="currentView" select="$currentView"/>
					<xsl:with-param name="fieldData" select="$fieldData"/>
					<xsl:with-param name="renderPattern" select="$renderPattern"/>
					<xsl:with-param name="Field" select="$Field"/>
				</xsl:call-template>
			</xsl:when>
						
			<!--if <html> <ifequal> sequence, we know it's inside a <href> tag. -->
			<xsl:when test="@StripWS = 'TRUE' or (name(preceding-sibling::*[1])='IfEqual' and name(preceding-sibling::*[2])='HTML') or (name(preceding-sibling::*[1])='HTML' and substring($prevStr, string-length($prevStr), 1)='=' )">
				<xsl:text/>{@<xsl:value-of select="@Name"/>}<xsl:text/>			
			</xsl:when>
			<xsl:when test="ancestor::ListFormOpening or ancestor::Expr or ancestor::Expr1 or ancestor::Expr2 or ancestor::SetVar or substring($prevStr, string-length($prevStr), 1)='='">
				<xsl:text/>@<xsl:value-of select="@Name"/><xsl:text/>
			</xsl:when>
			<xsl:when test="ancestor::Field and /List/MetaData/Fields/Field[@Name=current()/@Name]">
				<xsl:choose>
				<xsl:when test="/List/MetaData/Fields/Field[@Name=current()/@Name][@Type='Lookup']">
					<xsl:text/>{$Field<xsl:value-of select="generate-id(.)"/>}<xsl:text/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:call-template name="FieldExpandTempl">
						<xsl:with-param name="currentView" select="$currentView"/>
						<xsl:with-param name="fieldData" select="/List/MetaData/Fields/Field[@Name=current()/@Name]"/>
						<xsl:with-param name="renderPattern" select="$renderPattern"/>
						<xsl:with-param name="Field" select="$Field"/>
					</xsl:call-template>
				</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>		
				<xz:value-of>
					<xsl:attribute name="select">@<xsl:value-of select="@Name"/></xsl:attribute>
				</xz:value-of>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

 	<!-- generate variables in the header of the view files
            to get rid of the annoying "alt=ifequal" problem.    -->
	<xsl:template match="IfEqual" mode="genVariables">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>
		
		<xsl:variable name="prevNode" select="preceding-sibling::*[1]"/>		
		<xsl:variable name="prevStr"     select="normalize-space(string(preceding-sibling::*[1]))"/>

		<!-- if the previous node is html node that ends with ", we'll do special add the special handlding 
			 code to have var first, then directly call the var in the usage-->
		<xsl:if test="name($prevNode) = 'HTML' and substring($prevStr, string-length($prevStr), 1) = '&quot;' or name($prevNode) = 'ImagesPath'">
			<xz:variable>
				<xsl:attribute name="name"><xsl:value-of select="generate-id(.)"/></xsl:attribute>
				<xz:choose>
					<xz:when>
						<xsl:attribute name="test">
							<xsl:apply-templates mode="genTemplates" select="Expr1/*">
								<xsl:with-param name="currentView" select="$currentView"/>
								<xsl:with-param name="fieldData" select="$fieldData"/>
								<xsl:with-param name="renderPattern" select="$renderPattern"/>
								<xsl:with-param name="Field" select="$Field"/>						
							</xsl:apply-templates>
							<xsl:text/>=<xsl:text/>
							<xsl:choose>
								<xsl:when test="not(Expr2/*)">''</xsl:when>
								<xsl:otherwise>
										<xsl:apply-templates mode="genTemplates" select="Expr2/*">
											<xsl:with-param name="currentView" select="$currentView"/>
											<xsl:with-param name="fieldData" select="$fieldData"/>
											<xsl:with-param name="renderPattern" select="$renderPattern"/>
											<xsl:with-param name="Field" select="$Field"/>
									</xsl:apply-templates>		
								</xsl:otherwise>
							</xsl:choose>				
						</xsl:attribute>								
				
						<xsl:apply-templates mode="genTemplates" select="Then/*">
							<xsl:with-param name="currentView" select="$currentView"/>
							<xsl:with-param name="fieldData" select="$fieldData"/>
							<xsl:with-param name="renderPattern" select="$renderPattern"/>
							<xsl:with-param name="Field" select="$Field"/>						
						</xsl:apply-templates>			
					</xz:when>
					<xz:otherwise>
						<xsl:apply-templates mode="genTemplates" select="Else/*">
							<xsl:with-param name="currentView" select="$currentView"/>
							<xsl:with-param name="fieldData" select="$fieldData"/>
							<xsl:with-param name="renderPattern" select="$renderPattern"/>
							<xsl:with-param name="Field" select="$Field"/>						
						</xsl:apply-templates>
					</xz:otherwise>
				</xz:choose>
			</xz:variable>
		</xsl:if>
	</xsl:template>

	<xsl:template mode="genTemplates" match="Attachments">
		<xz:if>
			<xsl:attribute name="test">not (@Attachments=0)</xsl:attribute>
				<img border="0" src="{{$HttpVDir}}/_layouts/images/attach.gif"/>
		</xz:if>
	</xsl:template>

	<xsl:template mode="genTemplates" match="IfEqual">
		<xsl:param name="currentView"/>		
		<xsl:param name="fieldData"/>
		<xsl:param name="renderPattern"/>
		<xsl:param name="Field"/>
	
		<xsl:variable name="prevNode" select="preceding-sibling::*[1]"/>		
		<xsl:variable name="prevStr"     select="normalize-space(string(preceding-sibling::*[1]))"/>

		<xsl:choose>
			<xsl:when test="Expr1/GetVar[@Name='ErrorText']">
				<xsl:choose>
					<xsl:when test="string(Expr2)='TRUE'">
						<xsl:apply-templates mode="genTemplates" select="Else/*">
							<xsl:with-param name="currentView" select="$currentView"/>
							<xsl:with-param name="fieldData" select="$fieldData"/>
							<xsl:with-param name="renderPattern" select="$renderPattern"/>
							<xsl:with-param name="Field" select="$Field"/>						
						</xsl:apply-templates>															
					</xsl:when>
					<xsl:otherwise>
						<xsl:apply-templates mode="genTemplates" select="Then/*">
							<xsl:with-param name="currentView" select="$currentView"/>
							<xsl:with-param name="fieldData" select="$fieldData"/>
							<xsl:with-param name="renderPattern" select="$renderPattern"/>
							<xsl:with-param name="Field" select="$Field"/>						
						</xsl:apply-templates>																					
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="name($prevNode) = 'HTML' and substring($prevStr, string-length($prevStr), 1) = '&quot;' or name($prevNode) = 'ImagesPath'">
				<xsl:text/>{$<xsl:value-of select="generate-id(.)"/>}<xsl:text/>
			</xsl:when>
			<xsl:otherwise>
				<xz:choose>
					<xz:when>
						<xsl:attribute name="test">
							<xsl:apply-templates mode="genTemplates" select="Expr1/*">
								<xsl:with-param name="currentView" select="$currentView"/>
								<xsl:with-param name="fieldData" select="$fieldData"/>
								<xsl:with-param name="renderPattern" select="$renderPattern"/>
								<xsl:with-param name="Field" select="$Field"/>						
							</xsl:apply-templates>
							<xsl:text/>=<xsl:text/>
							<xsl:choose>
								<xsl:when test="not(Expr2/*)">'<xsl:value-of select="string(Expr2)"/>'</xsl:when>
								<xsl:when test="Expr2/ListUrlDir and Expr2/HTML">
									<xsl:text/>concat(<xsl:for-each select="Expr2/*"><xsl:if test="not(name()='ListUrlDir')">'</xsl:if><xsl:apply-templates mode="genTemplates" select="."/><xsl:if test="not(name()='ListUrlDir')">'</xsl:if><xsl:if test="not(position() = last())">,</xsl:if></xsl:for-each>)<xsl:text/>
								</xsl:when>
								<xsl:otherwise>
								<xsl:apply-templates mode="genTemplates" select="Expr2/*">
									<xsl:with-param name="currentView" select="$currentView"/>
									<xsl:with-param name="fieldData" select="$fieldData"/>
									<xsl:with-param name="renderPattern" select="$renderPattern"/>
									<xsl:with-param name="Field" select="$Field"/>						
								</xsl:apply-templates>		
								</xsl:otherwise>
							</xsl:choose>				
						</xsl:attribute>

						<xsl:choose>
							<xsl:when test="Then/*">
								<xsl:apply-templates mode="genTemplates" select="Then/*">
									<xsl:with-param name="currentView" select="$currentView"/>
									<xsl:with-param name="fieldData" select="$fieldData"/>
									<xsl:with-param name="renderPattern" select="$renderPattern"/>
									<xsl:with-param name="Field" select="$Field"/>						
								</xsl:apply-templates>									
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="Then"/>								
							</xsl:otherwise>
						</xsl:choose>
					</xz:when>
					<xz:otherwise>					
						<xsl:choose>
							<xsl:when test="Else/*">
								<xsl:apply-templates mode="genTemplates" select="Else/*">
									<xsl:with-param name="currentView" select="$currentView"/>
									<xsl:with-param name="fieldData" select="$fieldData"/>
									<xsl:with-param name="renderPattern" select="$renderPattern"/>
									<xsl:with-param name="Field" select="$Field"/>						
								</xsl:apply-templates>									
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="Else"/>		
							</xsl:otherwise>
						</xsl:choose>
					</xz:otherwise>
				</xz:choose>		
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>