<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://icl.com/saxon" extension-element-prefixes="saxon" xmlns:gml="http://www.opengis.net/gml" xmlns="http://www.canri.nsw.gov.au/schema">
	<xsl:output method="xml" indent="yes"/>
	<!-- ====================================================================================

	wms_featureinfo_xml.xsl

		Last Updated   - 17-Dec-2007
		Updated By     - Rob Atkinson
		Converts ArcXML from ArcInfo V9.2 feature information into a generic GML GetFeatureInfo response.
		Replaces the file $TOMCAT/webapps/wmsconnector/WEB-INF/classes/wms/stylesheets/wms_featureinfo_xml.xsl
		
		The default ArcXML getFeatureInfo response from ArcIMS contains a list of database table/view 
		column (or shapefile attribute) names and value pairs in the following format:
			<FIELD value="{column value}" name="{column name}"/>
    
		If you need a valid GML:fid in the getFeatureInfo response, for example to redirect the output to 
		a specific page within an existing web-based application, you must first edit this stylesheet to work 
		with your data. For each data source where you need a valid GML:fid, add an xsl:when element for 
		the name of the column in the database table/view that contains the public feature identifier (gml:fid).
		
		You can also specify database columns to omit from the output. Several of these are defined by default.
    
		** Important ** 
		1. The column names are case sensitive.
		2. Use the fully-qualified column name as it appears in the ArcXML getFeatureInfo response.
		3. There must be no whitespace surrounding the column name text.
		4. Only one of the column names can exist in the source XML document.
		5. This is a quick hack that does not scale well.

	==================================================================================== -->
	<!-- Ignore these input elements (database column names) - omit them from the output
	      If you add another line here, be sure to also add another line where SkipColumnNames are used below -->
	<!-- Omit default shape file columns from the output-->
	<xsl:variable name="SkipColumnName1">_ID_</xsl:variable>
	<xsl:variable name="SkipColumnName2">_LAYERID_</xsl:variable>
	<xsl:variable name="SkipColumnName3">#SHAPE#</xsl:variable>
	<xsl:variable name="SkipColumnName4">_SHAPE_</xsl:variable>

	<xsl:template match="/">
		<xsl:element name="gml:featureCollection" namespace="http://www.opengis.net/gml">
			<!-- Identifies input elements (fully-qualified database column names) to be used as the GML:fid -->

			<xsl:choose>

				<!-- The three xsl:when clauses below are examples only - delete them before using the stylesheet. -->

				<!-- Groundwater station number -->
				<xsl:when test="count(FeatureInfoResponse/FIELDS/@GW.GW_WORKS_SPATIAL_POINTS.STATION_NO) = 1">
					<xsl:apply-templates select="FeatureInfoResponse/FIELDS" mode="fid">
						<xsl:with-param name="fid">
							<xsl:value-of select="FeatureInfoResponse/FIELDS/@GW.GW_WORKS_SPATIAL_POINTS.STATION_NO"/>
						</xsl:with-param>
					</xsl:apply-templates>
				</xsl:when>

				<!-- Soil profile id -->
				<xsl:when test="count(FeatureInfoResponse/FIELDS/@SALIS.SA_SOIL_PROFILE_PUBLIC_V.PROFILE_ID) = 1">
					<xsl:apply-templates select="FeatureInfoResponse/FIELDS" mode="fid">
						<xsl:with-param name="fid">
							<xsl:value-of select="FeatureInfoResponse/FIELDS/@SALIS.SA_SOIL_PROFILE_PUBLIC_V.PROFILE_ID"/>
						</xsl:with-param>
					</xsl:apply-templates>
				</xsl:when>

				<!-- Native vegetation mapping program polygon number -->
				<xsl:when test="count(FeatureInfoResponse/FIELDS/@POLYNO) = 1">
					<xsl:apply-templates select="FeatureInfoResponse/FIELDS" mode="fid">
						<xsl:with-param name="fid">
							<xsl:value-of select="FeatureInfoResponse/FIELDS/@POLYNO"/>
						</xsl:with-param>
					</xsl:apply-templates>
				</xsl:when>

				<!-- Add more "xsl:when" elements here where a new data source requires 
				a different input element to be used as the GML:fid -->
				<!-- Insert plain English description of the id field here -->
				<!--
				<xsl:when test="count(FeatureInfoResponse/FIELDS/@INSERT_FULL_INPUT_ELEMENT_NAME_HERE) = 1">
					<xsl:apply-templates select="FeatureInfoResponse/FIELDS" mode="fid">
						<xsl:with-param name="fid">
							<xsl:value-of select="FeatureInfoResponse/FIELDS/@INSERT_FULL_INPUT_ELEMENT_NAME_HERE"/>
						</xsl:with-param>
					</xsl:apply-templates>
				</xsl:when>
				-->

				<xsl:otherwise>
					<xsl:apply-templates select="FeatureInfoResponse/FIELDS" mode="generic"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:element>
	</xsl:template>

	<!-- For each FIELDS element in the ArcXML response, uses the @attributes name as the element name. 
		'mode' allows feature types with fid or without ("generic") to be processed -->
	<xsl:template match="FIELDS" mode="generic">
		<xsl:element name="gml:featureMember">
			<xsl:element name="feature" namespace="http://www.canri.nsw.gov.au/schema">
				<xsl:apply-templates select="attribute::*" mode="processFields"/>
			</xsl:element>
		</xsl:element>
	</xsl:template>
	<xsl:template match="FIELDS" mode="fid">
		<xsl:param name="fid"/>
		<xsl:element name="gml:featureMember">
			<xsl:attribute name="fid"><xsl:value-of select="$fid"/></xsl:attribute>
			<xsl:element name="feature" namespace="http://www.canri.nsw.gov.au/schema">
				<xsl:apply-templates select="attribute::*" mode="processFields"/>
			</xsl:element>
		</xsl:element>
	</xsl:template>

	<!-- Processes all of the attributes belonging to a FIELDS -->
	<xsl:template match="attribute::*" mode="processFields">
		<xsl:variable name="attrName">
			<xsl:value-of select="local-name(.)"/>
		</xsl:variable>
		<xsl:choose>
			<xsl:when test="contains($attrName, $SkipColumnName1)"/>
			<xsl:when test="contains($attrName, $SkipColumnName2)"/>
			<xsl:when test="contains($attrName, $SkipColumnName3)"/>
			<xsl:when test="contains($attrName, $SkipColumnName4)"/>

			<!-- Add more "xsl:when" elements here to identify input database columns to be 
			omitted from the output XML document.
			The database column name is stored as the FIELDS/@attribute.
			If you add more SkipColumnNames above, be sure to also add them here-->

			<xsl:otherwise>
				<xsl:variable name="shortFieldName">
					<xsl:call-template name="extractShortFieldName">
						<xsl:with-param name="longFieldName" select="$attrName"/>
					</xsl:call-template>
				</xsl:variable>
				<xsl:element name="{$shortFieldName}">
					<xsl:value-of select="."/>
				</xsl:element>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<!--  Accepts a fully qualified database column name and removes all leading database schema and 
	table names. e.g. "NSWGEN.SP_IBRA.STATE" becomes "STATE".
    -->
	<xsl:template name="extractShortFieldName">
		<xsl:param name="longFieldName"/>
		<xsl:choose>
			<xsl:when test="contains($longFieldName, '.')">
				<xsl:variable name="processedFieldName">
					<xsl:value-of select="substring-after($longFieldName, '.')"/>
				</xsl:variable>
				<!-- If there is still a dot separator in the processed field name do it again. -->
				<xsl:choose>
					<xsl:when test="contains($processedFieldName, '.')">
						<xsl:value-of select="substring-after($processedFieldName, '.')"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$processedFieldName"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$longFieldName"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>
