Index: /main/trunk/greenstone3/web/interfaces/default/transform/expand-gsf.xsl
===================================================================
--- /main/trunk/greenstone3/web/interfaces/default/transform/expand-gsf.xsl	(revision 36879)
+++ /main/trunk/greenstone3/web/interfaces/default/transform/expand-gsf.xsl	(revision 36880)
@@ -140,5 +140,33 @@
 	    did need the character to be entity-mapped -->
 	<xsl:template match="gsf:squote"><xslt:text disable-output-escaping="yes">'</xslt:text></xsl:template>	
-	
+
+        <xsl:template name="gsf:assocfilepathVariable">
+          <xslt:variable name="thisAssocfilepath">					
+	    <xslt:choose>
+	      <xslt:when test="/page/pageResponse/document">
+		<xslt:choose>
+		  <xslt:when test="/page/pageResponse/document/metadataList/metadata[@name='assocfilepath']">
+		    <xslt:value-of disable-output-escaping="yes" select="/page/pageResponse/document/metadataList/metadata[@name='assocfilepath']" />
+		  </xslt:when>
+		  <xslt:otherwise>
+		    <xslt:value-of disable-output-escaping="yes" select="/page/pageResponse/document/documentNode[@nodeType='root']/metadataList/metadata[@name='assocfilepath']" />
+		  </xslt:otherwise>
+		</xslt:choose>
+	      </xslt:when>
+	      <xslt:otherwise>
+		<xslt:choose>
+		  <xslt:when test="metadataList/metadata[@name='assocfilepath']">
+		    <xslt:value-of disable-output-escaping="yes" select="metadataList/metadata[@name='assocfilepath']" />
+		  </xslt:when>
+		  <xslt:otherwise>
+		    <xslt:value-of disable-output-escaping="yes" select="metadataList/metadata[@name='root_assocfilepath']" />
+		  </xslt:otherwise>
+		</xslt:choose>
+	      </xslt:otherwise>
+	    </xslt:choose>						
+	  </xslt:variable>
+          
+        </xsl:template>
+        
 	<xsl:template match="gsf:image">
 		<xslt:variable name="metaName">
@@ -150,28 +178,6 @@
 			</xsl:choose>
 		</xslt:variable>
-		<xslt:variable name="thisAssocfilepath">					
-		  <xslt:choose>
-		    <xslt:when test="/page/pageResponse/document">
-		      <xslt:choose>
-			<xslt:when test="/page/pageResponse/document/metadataList/metadata[@name='assocfilepath']">
-			  <xslt:value-of disable-output-escaping="yes" select="/page/pageResponse/document/metadataList/metadata[@name='assocfilepath']" />
-			</xslt:when>
-			<xslt:otherwise>
-			  <xslt:value-of disable-output-escaping="yes" select="/page/pageResponse/document/documentNode[@nodeType='root']/metadataList/metadata[@name='assocfilepath']" />
-			</xslt:otherwise>
-		      </xslt:choose>
-		    </xslt:when>
-		    <xslt:otherwise>
-		      <xslt:choose>
-			<xslt:when test="(.//metadataList)[last()]/metadata[@name='assocfilepath']">
-			  <xslt:value-of disable-output-escaping="yes" select="(.//metadataList)[last()]/metadata[@name='assocfilepath']" />
-			</xslt:when>
-			<xslt:otherwise>
-			  <xslt:value-of disable-output-escaping="yes" select="(.//metadataList)[last()]/metadata[@name='root_assocfilepath']" />
-			</xslt:otherwise>
-		      </xslt:choose>
-		    </xslt:otherwise>
-		  </xslt:choose>						
-		</xslt:variable>
+                <!-- output the $thisAssocfilepath variable definition-->
+                <xsl:call-template name="gsf:assocfilepathVariable"/>
 		  <xslt:if test="./metadataList/metadata[@name = $metaName]">
 			<img>
@@ -268,21 +274,5 @@
 			</xsl:when>
 			<xsl:when test="@type='source'">
-			  <xslt:variable name="thisAssocfilepath">					
-			    <xslt:choose>
-			      <xslt:when test="/page/pageResponse/document">
-				<xslt:choose>
-				  <xslt:when test="/page/pageResponse/document/metadataList/metadata[@name='assocfilepath']">
-				    <xslt:value-of disable-output-escaping="yes" select="/page/pageResponse/document/metadataList/metadata[@name='assocfilepath']" />
-				  </xslt:when>
-				  <xslt:otherwise>
-				    <xslt:value-of disable-output-escaping="yes" select="/page/pageResponse/document/documentNode[@nodeType='root']/metadataList/metadata[@name='assocfilepath']" />
-				  </xslt:otherwise>
-				</xslt:choose>
-			      </xslt:when>
-			      <xslt:otherwise>
-				<xslt:value-of disable-output-escaping="yes" select="(.//metadataList)[last()]/metadata[@name='root_assocfilepath']" />
-			      </xslt:otherwise>
-			    </xslt:choose>						
-				</xslt:variable>
+                          <xsl:call-template name="gsf:assocfilepathVariable"/>
 				<xsl:variable name="image_meta">
 				  <xsl:choose>
@@ -307,5 +297,5 @@
 				   disable-output-escaping="yes" select="/page/pageResponse/collection/metadataList/metadata[@name='httpPath']" />/index/assoc/<xslt:value-of 
 				   disable-output-escaping="yes" select="$thisAssocfilepath" />/<xslt:value-of 
-				   disable-output-escaping="yes" select="(.//metadataList)[last()]/metadata[@name='{$image_meta}']" />
+				   disable-output-escaping="yes" select="metadataList/metadata[@name='{$image_meta}']" />
 					</xslt:attribute>
 					<xsl:copy-of select="$opt-title"/>
@@ -377,5 +367,5 @@
 							<xsl:when test="@OIDmetadata">
 								<xsl:variable name="OIDmeta" select="@OIDmetadata"/>
-								<xslt:value-of select="(.//metadataList)[last()]/metadata[@name='{$OIDmeta}']"/>
+								<xslt:value-of select="metadataList/metadata[@name='{$OIDmeta}']"/>
 							</xsl:when>
 							<xsl:otherwise>
@@ -617,5 +607,5 @@
 			<xslt:for-each>
 				<xsl:attribute name="select">
-				  (<xsl:if test="@type='collection'">/page/pageResponse/collection/</xsl:if>.//metadataList)[last()]/metadata[<xsl:value-of select="$meta_test"/><xsl:if test="@lang">
+				 <xsl:if test="@type='collection'">/page/pageResponse/collection/</xsl:if>metadataList/metadata[<xsl:value-of select="$meta_test"/><xsl:if test="@lang">
 						<xsl:text> and @lang=</xsl:text>
 						<xsl:value-of select="@lang"/>
@@ -648,5 +638,5 @@
     <xslt:for-each>
       <xsl:attribute name="select">
-	(<xsl:if test="@type='collection'">/page/pageResponse/collection/</xsl:if>.//metadataList)[last()]/metadata[@name='<xsl:value-of select="$meta_name"/>'<xsl:if test="@lang"><xsl:text> and @lang=</xsl:text><xsl:value-of select="@lang"/></xsl:if><xsl:text>]</xsl:text>
+	(<xsl:if test="@type='collection'">/page/pageResponse/collection/</xsl:if>metadataList/metadata[@name='<xsl:value-of select="$meta_name"/>'<xsl:if test="@lang"><xsl:text> and @lang=</xsl:text><xsl:value-of select="@lang"/></xsl:if><xsl:text>]</xsl:text>
       </xsl:attribute><xsl:choose><xsl:when test='@sort'><xslt:sort/></xsl:when><xsl:when test="gsf:sort"><xslt:sort><xsl:copy-of select="gsf:sort/@*"/></xslt:sort></xsl:when></xsl:choose><xsl:if test='@separator'><xslt:if test='position()>1'><xsl:value-of select='@separator'/></xslt:if></xsl:if>
       <xsl:apply-templates/>
@@ -742,5 +732,5 @@
 			<xsl:for-each select="gsf:metadata">
 				<xslt:when>
-					<xsl:attribute name="test">(.//metadataList)[last()]/metadata[@name='<xsl:call-template name="getMetadataName"/>']</xsl:attribute>
+					<xsl:attribute name="test">metadataList/metadata[@name='<xsl:call-template name="getMetadataName"/>']</xsl:attribute>
 					<xsl:apply-templates select="."/>
 				</xslt:when>
@@ -763,8 +753,8 @@
 			<xsl:choose>
 				<xsl:when test="@preprocess">
-					<xslt:value-of select="util:{@preprocess}((.//metadataList)[last()]/metadata[@name='{$meta-name}'], /page/@lang )"/>
+					<xslt:value-of select="util:{@preprocess}(metadataList/metadata[@name='{$meta-name}'], /page/@lang )"/>
 				</xsl:when>
 				<xsl:otherwise>
-					<xslt:value-of select="(.//metadataList)[last()]/metadata[@name='{$meta-name}']"/>
+					<xslt:value-of select="metadataList/metadata[@name='{$meta-name}']"/>
 				</xsl:otherwise>
 			</xsl:choose>
