<xsl:stylesheet version = '1.0' 
    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> 

<xsl:output method="html"/>

<xsl:variable name="prev">
	<xsl:text>&lt;&lt;PREV</xsl:text>
</xsl:variable>
<xsl:variable name="next">
	<xsl:text> NEXT>></xsl:text>
</xsl:variable>
<!-- number of results per page -->
<xsl:variable name="page-size" select="10"/>
<!-- max number of navigation links (pages) -->
<xsl:variable name="maxLinks" select="20"/>


<xsl:template match="return">
<!-- search summary -->
	<b>Search time: </b><xsl:value-of select="./searchTime"/>
	<b> Total Results:</b><xsl:value-of select="./estimatedTotalResultsCount"/><br/>
	<xsl:apply-templates select="directoryCategories"/>
	<xsl:apply-templates select="resultElements"/>
	<!-- format result page links -->
	<b>Result Page: </b>
	<xsl:call-template name="format-link">
		<xsl:with-param name="from" select="1"/>
		<xsl:with-param name="maxResults" select="./estimatedTotalResultsCount"/>
	</xsl:call-template>

</xsl:template>

<xsl:template match="directoryCategories"> 
	<!-- transform directory items -->
	<xsl:if test="./item">
		<h4>Directory Matches:</h4>
	</xsl:if>
	<xsl:for-each select="item">
		<ul>
		<xsl:element name="a">
			<xsl:attribute name="href">
				http://directory.google.com/<xsl:value-of select="./fullViewableName"/>
			</xsl:attribute>
			<xsl:value-of select="./fullViewableName" disable-output-escaping="yes"/>
		</xsl:element>
		</ul>
	</xsl:for-each>
</xsl:template>

<xsl:template match="resultElements"> 
	<!-- transform main results -->
	<xsl:for-each select="item">
		<p>
		<xsl:element name="a">
			<xsl:attribute name="href">
				<xsl:value-of select="./URL"/>
			</xsl:attribute>
			<em><xsl:value-of select="./title" disable-output-escaping="yes"/></em> <br/>
		</xsl:element>

		<xsl:value-of select="./snippet" disable-output-escaping="yes"/><br/>
		<xsl:value-of select="./URL"/><xsl:text> -- </xsl:text>
		<em>size:</em><xsl:value-of select="./cachedSize"/>
		<br/>
		</p>
	</xsl:for-each>
</xsl:template>

<xsl:template match="*"> 
	 <xsl:apply-templates/>
</xsl:template>

<!-- function to format page links -->
<xsl:template name="format-link">
<xsl:param name="from"/>
<xsl:param name="maxResults"/>
<xsl:text> </xsl:text>
<xsl:if test="($from &lt; $maxResults) and ($from div $page-size &lt; $maxLinks)">
	<!-- add PREVIOUS  button -->
	<xsl:if test="$from = 1 and ./startIndex!= 1">
		<xsl:call-template name="add-href">
			<xsl:with-param name="from" select="./startIndex - $page-size"/>
			<xsl:with-param name="href-value" select="$prev"/>
		</xsl:call-template>
	</xsl:if>

	<!-- add numeric page link -->
	<xsl:call-template name="add-href">
		<xsl:with-param name="from" select="$from"/>
		<xsl:with-param name="href-value" select="ceiling($from div $page-size)"/>
	</xsl:call-template>
	<!-- add NEXT  -->
	<xsl:if test="($from > $maxResults - $page-size) or ($from div $page-size >= $maxLinks - 1)">
		<xsl:call-template name="add-href">
			<xsl:with-param name="from" select="./startIndex + $page-size"/>
			<xsl:with-param name="href-value" select="$next"/>
		</xsl:call-template>
	</xsl:if>
	<!-- recursive until all links are formatted. -->
	<xsl:call-template name="format-link">
		<xsl:with-param name="from" select="$from + $page-size"/>
		<xsl:with-param name="maxResults" select="$maxResults"/>
	</xsl:call-template>

</xsl:if>
</xsl:template>

<xsl:template name="add-href">
<xsl:param name="from"/>
<xsl:param name="href-value"/>
	<xsl:choose>
	<xsl:when test="$from != ./startIndex">
	<xsl:element name="a">
		<xsl:attribute name="href">
		<xsl:text>/soapclient?SoapWSDL=/xml/googlesearch.wsdl&amp;MethodName=doGoogleSearch&amp;template=/googleResult.html&amp;xsltFile=/xml/google.xsl&amp;key=firp7ewYCi3NrIIRyuT9Gr6bycyt7AHV&amp;filter=0&amp;restrict=0&amp;safesearch=1&amp;lr=lang_en&amp;start=</xsl:text>
		<xsl:value-of select="number($from)-1"/>
		<xsl:text>&amp;q=</xsl:text>
		<xsl:value-of select="./searchQuery"/>
		<xsl:text>&amp;maxResults=</xsl:text>
		<xsl:value-of select="$page-size"/>
		</xsl:attribute>
	<xsl:value-of select="$href-value"/>
	</xsl:element>
	</xsl:when>
	<xsl:otherwise>
		<xsl:value-of select="$href-value"/>
	</xsl:otherwise>
	</xsl:choose>
</xsl:template>

</xsl:stylesheet> 
