<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="no"/>	

<!-- Map Viewer (Third Edition) by Howie Sternberg at howies@snet.net. Map Viewer is placed in the public domain and is "Freeware". Map Viewer may be freely used and redistributed, is provided "AS-IS" without warranty of any kind, and there is no technical support provided. --> 

<!-- input parameters and defauts --> 
<xsl:param name="layerIndex" select="0"/>
<xsl:param name="layerName" select="'Data layer'"/>
<xsl:param name="featureClass" select="'polygon'"/>
<xsl:param name="searchField" select="'#ID#'"/>
<xsl:param name="searchFieldVal" select="'1'"/>
<xsl:param name="searchFieldDataType" select="'number'"/>
<xsl:param name="searchFeatureLimit" select="25"/>
<xsl:param name="searchBeginRecord" select="1"/>
<xsl:param name="searchListFeatureLimit" select="25"/>
<xsl:param name="searchListBeginRecord" select="1"/>
<xsl:param name="sortField" select="'#ID#'"/>
<xsl:param name="sortFieldDataType" select="'number'"/>
<xsl:param name="sortFieldDataTypeSort" select="'number'"/>
<xsl:param name="sortOrder" select="'ascending'"/>
<xsl:param name="rowIdField" select="'#ID#'"/>
<xsl:param name="dateFields" select="'[]'"/>

<!-- Return features results for Find function (search) as HTML -->

<!-- set globals for feature count and highest record number to possibly list --> 
<xsl:variable name="featureCount">
	<xsl:number value="/ARCXML/RESPONSE/FEATURES/FEATURECOUNT/@count" format="1"/>
</xsl:variable>
<xsl:variable name="searchListMaxRecord">
	<xsl:number value="$searchListBeginRecord + $searchListFeatureLimit" format="1"/>
</xsl:variable>
<xsl:variable name="zoomToAllOperator">
	<xsl:choose>
		<xsl:when test="contains($searchFieldDataType,'text')">
			<!-- use like if data type is text-->
			<xsl:value-of select="'like'"/>
		</xsl:when>
		<xsl:otherwise>
			<!-- use equal to if data type is number -->
			<xsl:value-of select="'='"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:variable>
		
<!-- format search results by first sorting records and writing table --> 	
<xsl:template match="/">
<table border="0" cellpadding="2" cellspacing="2">
<tr><td>
	<!-- search layer title, found feature counts and page links-->
	<table width="100%" border="0" cellpadding="2" cellspacing="0">
		<tr><td>
		Searched For: <em><xsl:value-of select="$searchFeatureLimit"/></em><xsl:text disable-output-escaping = "yes">&amp;nbsp;&amp;nbsp;</xsl:text>
		Found: <em><xsl:value-of select="$featureCount"/></em><xsl:text disable-output-escaping = "yes">&amp;nbsp;&amp;nbsp;</xsl:text>
		|<xsl:text disable-output-escaping = "yes">&amp;nbsp;&amp;nbsp;</xsl:text>Page Results:<xsl:text disable-output-escaping = "yes">&amp;nbsp;</xsl:text><xsl:call-template name="build-page-links"/>
		</td></tr>
		<tr><td class="mv_data_table_bar">
		<img src="images/layer{$featureClass}.jpg" border="0" alt="{$featureClass} data" width="15" heigth="13"/><xsl:text disable-output-escaping = "yes">&amp;nbsp;</xsl:text><xsl:value-of select="$layerName"/><xsl:text disable-output-escaping = "yes">&amp;nbsp;&amp;nbsp;&amp;nbsp;</xsl:text><a href="JavaScript:setSelection('{$layerIndex}','{$featureClass}','false','true','{$searchField}','{$searchFieldVal}','{$searchFieldDataType}','{$zoomToAllOperator}','','All','1','{$searchListFeatureLimit}','1')" title="Zoom to all features">Zoom to all</a><xsl:text disable-output-escaping = "yes">&amp;nbsp;</xsl:text>
		</td></tr>
		</table>
	<!-- search layer feature results -->
	<table  width="100%" class="mv_data_table" border="0" cellpadding="2" cellspacing="0">
		<xsl:for-each select="ARCXML/RESPONSE/FEATURES/FEATURE">
			<xsl:sort select="FIELDS/FIELD[@name = $sortField]/@value" order="{$sortOrder}" data-type="{$sortFieldDataTypeSort}" />
			<xsl:call-template name="write-table"/>
		</xsl:for-each>
	</table>
</td></tr>
</table>	
</xsl:template>

<!-- write table --> 	
<xsl:template name="write-table">		
	<xsl:variable name="featurePosition">
		<xsl:number value="position()" format="1"/>
	</xsl:variable>
	<!-- write column headings (field names) --> 	
	<xsl:if test="$featurePosition = 1">
		<tr>
		<xsl:call-template name="zoom-to-column-header"/>
		<xsl:apply-templates select="FIELDS/FIELD" mode="build-headers"/>
		</tr>
	</xsl:if>
	<!-- write records within current range to list --> 	
	<xsl:if test="$featurePosition &gt;= $searchListBeginRecord and $featurePosition &lt; $searchListMaxRecord">
		<tr>	
		<xsl:call-template name="write-link"/>
		<xsl:call-template name="write-data"/>
		</tr>
	</xsl:if>
</xsl:template>

<!-- write Zoom To link --> 	
<xsl:template name="write-link">
	<xsl:for-each select="FIELDS/FIELD">
		<xsl:choose>
			<xsl:when test="@name=$rowIdField">
				<xsl:variable name="rowIdfieldValue" select="@value"/>
				<td class="mv_data" nowrap="nowrap"><a href="JavaScript:setSelection('{$layerIndex}','{$featureClass}','false','true','{$rowIdField}','{$rowIdfieldValue}','number','=','','25','1','25','1')" title="Zoom to this map feature">Zoom to</a></td>
			</xsl:when>	
		</xsl:choose>
	</xsl:for-each>
</xsl:template>

<!-- write field values, including sorted fields with links used to reselect records --> 	
<xsl:template name="write-data">
	<xsl:for-each select="FIELDS/FIELD">
		<xsl:variable name="fieldValue" select="@value"/>
		<xsl:variable name="fieldNameFull" select="@name"/>		
		<xsl:variable name="fieldName">	
		<xsl:choose>
			<xsl:when test="contains($fieldNameFull,'.') and contains(substring-after($fieldNameFull,'.'),'.')">
				<!-- extract field mame from layer.table.field name notation (arcsde data source)-->
				<xsl:value-of select="substring-after(substring-after($fieldNameFull,'.'),'.')"/>
			</xsl:when>
			<xsl:when test="contains($fieldNameFull,'.')">
				<!-- extract field mame from table.field name notation (shapefile joined to dbase table)-->
				<xsl:value-of select="substring-after($fieldNameFull,'.')"/>
			</xsl:when>					
			<xsl:otherwise>
				<!-- extract field mame (shapefile data source) -->
				<xsl:value-of select="$fieldNameFull"/>
			</xsl:otherwise>
		</xsl:choose>
		</xsl:variable>
		<xsl:variable name="dateFieldNameFull">
			<text>[</text><xsl:value-of select="@name"/><text>]</text>
		</xsl:variable>			
		<xsl:choose>
			<!-- ignore certain standard Coverage, Shapefile and other non-rowid fields -->
			<xsl:when test="$fieldName='OBJECTID'"/>
			<xsl:when test="$fieldName='OID'"/>
			<xsl:when test="$fieldName='ID'"/>
			<xsl:when test="$fieldName='#ID#'"/>
			<xsl:when test="$fieldName='#SHAPE#'"/>
			<xsl:when test="$fieldName='OBJECTID'"/>
			<xsl:when test="$fieldName='AREA'"/>
			<xsl:when test="$fieldName='PERIMETER'"/>
			<xsl:when test="$fieldName='LENGTH'"/>
			<xsl:when test="$fieldName='FNODE'"/>
			<xsl:when test="$fieldName='TNODE'"/>
			<xsl:when test="$fieldName='SHAPE_AREA'"/>
			<xsl:when test="$fieldName='SHAPE_LEN'"/>
			<xsl:when test="$fieldNameFull='SHAPE.LEN'"/>
			<xsl:when test="$fieldNameFull='SHAPE.AREA'"/>
			<xsl:when test="$fieldName='Shape_Length'"/>
			<xsl:when test="$fieldName='Shape_Area'"/>
			<!-- include values only if valued -->
			<xsl:when test="@value != ''">
				<xsl:choose>
					<!-- Date field (if sorted with joined tables) - Work around bug/known limit associated with not being able to query date field if tables are joined. So test for joined (e.g. date field name contains '.') and do not include Reselect (zoom to) link -->
					<xsl:when test="@name=$sortField and contains($dateFields,$dateFieldNameFull) and contains($dateFieldNameFull,'.')">				
						<td class="mv_data" name="mapDateField" id="mapDateField" nowrap="nowrap"><xsl:value-of select="@value"/></td>
					</xsl:when>
					<!-- Date Field (if sorted) - Add Zoom to (reselect) links to allow to select features by date only if tables are not joined -->
					<xsl:when test="@name=$sortField and contains($dateFields,$dateFieldNameFull)">					
						<td class="mv_data" nowrap="nowrap"><span class="italic">Zoom to </span><a href="JavaScript:setSelection('{$layerIndex}','{$featureClass}','false','false','{$fieldNameFull}','{$fieldValue}','{$sortFieldDataType}','=','AND','All','1','{$searchListFeatureLimit}','1')" title="Zoom to all features with value of '{$fieldValue}'"><span name="mapDateField" id="mapDateField"><xsl:value-of select="@value"/></span></a></td>
					</xsl:when>
					<!-- Date Field (if not sorted)) -->
					<xsl:when test="contains($dateFields,$dateFieldNameFull)">
						<td class="mv_data" name="mapDateField" id="mapDateField" nowrap="nowrap"><xsl:value-of select="@value"/></td>
					</xsl:when>
					<!-- All other fields (if sorted) -->
					<xsl:when test="@name=$sortField">
						<td class="mv_data" nowrap="nowrap">Zoom to <a href="JavaScript:setSelection('{$layerIndex}','{$featureClass}','false','false','{$fieldNameFull}','{$fieldValue}','{$sortFieldDataType}','=','AND','All','1','{$searchListFeatureLimit}','1')" title="Zoom to all features with value of '{$fieldValue}'"><xsl:value-of select="@value"/></a></td>
					</xsl:when>	
					<!-- All other fields -->
					<xsl:otherwise>
						<td class="mv_data" nowrap="nowrap"><xsl:value-of select="@value"/></td>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="fill-empty-cell"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:for-each>
</xsl:template>

<!-- Make empty field values look correct in HTML table --> 	
<xsl:template name="fill-empty-cell">
	<td class="mv_data" ><xsl:text disable-output-escaping = "yes">&amp;nbsp;</xsl:text></td>
</xsl:template>

<!-- Column heading for Zoom To link--> 
<xsl:template name="zoom-to-column-header">
	<th class="mv_data_header"><xsl:text disable-output-escaping = "yes">Map&lt;br&gt;Feature</xsl:text></th>
</xsl:template>

<!-- Field name column headings with links for sorting. Field names converted to lower case except for first character. --> 
<xsl:template match="FIELD" mode="build-headers">	
	<xsl:variable name="fieldValue">
		<xsl:value-of select="@value"/>
	</xsl:variable>
	<xsl:variable name="fieldNameFull">
		<xsl:value-of select="@name"/>
	</xsl:variable>
	<xsl:variable name="fieldName">	
		<xsl:choose>
			<xsl:when test="contains($fieldNameFull,'.') and contains(substring-after($fieldNameFull,'.'),'.')">
				<!-- extract field mame from layer.table.field name notation (arcsde data source)-->
				<xsl:value-of select="substring-after(substring-after($fieldNameFull,'.'),'.')"/>
			</xsl:when>
			<xsl:when test="contains($fieldNameFull,'.')">
				<!-- extract field mame from table.field name notation (shapefile joined to dbase table)-->
				<xsl:value-of select="substring-after($fieldNameFull,'.')"/>
			</xsl:when>					
			<xsl:otherwise>
				<!-- extract field mame (shapefile data source) -->
				<xsl:value-of select="$fieldNameFull"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:choose>
		<!-- ignore certain standard Coverage, Shapefile and other non-rowid fields -->
		<xsl:when test="$fieldName='OBJECTID'"/>
		<xsl:when test="$fieldName='OID'"/>
		<xsl:when test="$fieldName='ID'"/>
		<xsl:when test="$fieldName='#ID#'"/>
		<xsl:when test="$fieldName='#SHAPE#'"/>
		<xsl:when test="$fieldName='OBJECTID'"/>
		<xsl:when test="$fieldName='AREA'"/>
		<xsl:when test="$fieldName='PERIMETER'"/>
		<xsl:when test="$fieldName='LENGTH'"/>
		<xsl:when test="$fieldName='FNODE'"/>
		<xsl:when test="$fieldName='TNODE'"/>
		<xsl:when test="$fieldName='SHAPE_AREA'"/>
		<xsl:when test="$fieldName='SHAPE_LEN'"/>
		<xsl:when test="$fieldNameFull='SHAPE.LEN'"/>
		<xsl:when test="$fieldNameFull='SHAPE.AREA'"/>
		<xsl:when test="$fieldName='Shape_Length'"/>
		<xsl:when test="$fieldName='Shape_Area'"/>
		<xsl:otherwise>
		<th class="mv_data_header" nowrap="nowrap"><xsl:value-of select="concat(substring($fieldName,1,1),translate(substring($fieldName,2),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'))"></xsl:value-of><br/><a href="JavaScript:sortSearchFeatures('{@name}','ascending')"><img src="images/sortascending_0.gif" border="0" alt="Sort Ascending" width="9" height="9" onmouseout="sortAscendOut(this)" onmouseover="sortAscendOver(this)"/></a> | <a href="JavaScript:sortSearchFeatures('{@name}','descending')"><img src="images/sortdescending_0.gif" border="0" alt="Sort Descending" width="9" height="9" onmouseout="sortDescendOut(this)" onmouseover="sortDescendOver(this)"/></a></th>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- build page links -->
<xsl:template name="build-page-links">
	<xsl:call-template name="page-link">
		<xsl:with-param name="i" 			select="1"/>
		<xsl:with-param name="increment"	select="$searchListFeatureLimit"/>
		<xsl:with-param name="testValue"	select="$searchListBeginRecord"/>
		<xsl:with-param name="maxValue"		select="$featureCount"/>
		<xsl:with-param name="pageNo"		select="1"/>
	</xsl:call-template>
</xsl:template>

<!-- Write links for listing features based on the feature count and, most importantly,
the current begin record to list the number of features to list per page. Need to emulate
a for loop by recursively calling this template. This loop technique was obtained from 
a book entitled, XSLT by Doug Tidwell (O'Reilly publisher) -->
<xsl:template name="page-link">
    <xsl:param name="i"			select="1"/>
	<xsl:param name="increment" select="25"/>
	<xsl:param name="testValue" select="25"/>
	<xsl:param name="maxValue" 	select="25"/>
	<xsl:param name="pageNo" 	select="1"/>	

	<!-- flag if beyond feature count in order to bail out -->	
	<xsl:variable name="doLink">
		<xsl:if test="$i &lt;= $maxValue">
			<xsl:text>true</xsl:text>
		</xsl:if>
	</xsl:variable>

	<!-- write links -->		
	<xsl:if test="$doLink='true'">

		<!-- begin record for PREVIOUS link -->			
		<xsl:variable name="previousValue">
			<xsl:number value="$testValue - $increment" format="1"/>
		</xsl:variable>
		<!-- begin record for NEXT link -->			
		<xsl:variable name="nextValue">
			<xsl:number value="$testValue + $increment" format="1"/>
		</xsl:variable>
		<!-- Highest record number requiring a NEXT link -->		
		<xsl:variable name="maxNextValue">
			<xsl:number value="$maxValue - $increment" format="1"/>
		</xsl:variable>
		<!-- Next to highest record number requiring a NEXT link -->		
		<xsl:variable name="minNextValue">
			<xsl:number value="$maxNextValue - $increment" format="1"/>
		</xsl:variable>
	
		<!-- Test if NEXT link should follow current page link -->			
		<xsl:variable name="doNextLink">
		   <xsl:if test="$testValue &lt;= $maxNextValue">
				<xsl:if test="$i &gt; $minNextValue">	       
					<xsl:if test="$i &lt; $maxNextValue">		
						<xsl:text>true</xsl:text>
					</xsl:if>
				</xsl:if>
			</xsl:if>
		</xsl:variable>

		<!-- write PREVIOUS link -->			
		<xsl:if test="$testValue &gt;= $increment">
			<xsl:if test="$pageNo = 1">
				<xsl:text disable-output-escaping = "yes">&lt;&amp;nbsp;</xsl:text>
				<a href="JavaScript:listSearchFeatures({$previousValue})">Back</a> 
				<xsl:text disable-output-escaping = "yes">&amp;nbsp;-</xsl:text>
			</xsl:if>
		</xsl:if>
		
		<!-- write PAGE NO link -->			
		<xsl:choose>
			<xsl:when test="$i = $testValue">
					<xsl:text disable-output-escaping = "yes">&amp;nbsp;</xsl:text>
				<em> <xsl:value-of select="$pageNo"/></em>
			</xsl:when>
			<xsl:otherwise>
					<xsl:text disable-output-escaping = "yes">&amp;nbsp;</xsl:text>
				<a href="JavaScript:listSearchFeatures({$i})"><xsl:value-of select="$pageNo"/></a>
			</xsl:otherwise>
		</xsl:choose>

		<!-- increment and call template again -->			
		<xsl:call-template name="page-link">
			<xsl:with-param name="i" 			select="$i + $increment"/>
			<xsl:with-param name="increment"	select="$increment"/>
			<xsl:with-param name="testValue"	select="$testValue"/>
			<xsl:with-param name="maxValue"		select="$maxValue"/>
			<xsl:with-param name="pageNo" 		select="$pageNo + 1"/>
		</xsl:call-template>

		<!-- write NEXT link -->					
		<xsl:if test="$doNextLink='true'">
			<xsl:text disable-output-escaping = "yes">&amp;nbsp;-&amp;nbsp;</xsl:text>
			<a href="JavaScript:listSearchFeatures({$nextValue})">Next</a>
			<xsl:text disable-output-escaping = "yes">&amp;nbsp;&gt;</xsl:text>				
		</xsl:if>
	</xsl:if>
</xsl:template>
</xsl:stylesheet>