<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:msxsl="urn:schemas-microsoft-com:xslt">

<xsl:output method="html"/>

<xsl:variable name="sects">

<xsl:for-each select="/SafariSearchResults/book/section">

<xsl:variable name="weight">
<xsl:value-of select="substring-before(@weight,'%')"/>
</xsl:variable>

<xsl:variable name="ancestorWeight">
<xsl:value-of select="substring-before(ancestor::book/@weight,'%')"/>
</xsl:variable>

<xsl:variable name="finalWeight">
<xsl:choose>
<xsl:when test="not( starts-with($weight,'&lt;')) and not(starts-with($ancestorWeight,'&lt;'))">
<xsl:value-of select="$ancestorWeight * $weight"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="0"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<xsl:element name="sect">
  <xsl:attribute name="weight">
  <xsl:value-of select="$finalWeight"/>
  </xsl:attribute>
  <xsl:attribute name="bookname">
  <xsl:value-of select="ancestor::book/@bookname"/>
  </xsl:attribute>
  <xsl:attribute name="snode">
  <xsl:value-of select="@snode"/>
  </xsl:attribute>
  <xsl:attribute name="stitle">
  <xsl:value-of select="title"/>
  </xsl:attribute>

<xsl:for-each select="./sentence[position()=1 or position()=2]">
  <xsl:element name="sentence">
  <xsl:copy-of select="."/>
  </xsl:element>
</xsl:for-each>

</xsl:element>


</xsl:for-each>
</xsl:variable>

<xsl:variable name="indexterms">

<xsl:for-each select="/SafariSearchResults/book/section//indexterm[0]">

<xsl:element name="idxterm">
  <xsl:attribute name="idxprimary">
  <xsl:value-of select="primary"/>
  </xsl:attribute>
  <xsl:attribute name="idxsecondary">
  <xsl:value-of select="secondary"/>
  </xsl:attribute>
  <xsl:attribute name="idxtertiary">
  <xsl:value-of select="tertiary"/>
  </xsl:attribute>
  <xsl:attribute name="snode">
  <xsl:value-of select="../../@snode"/>
  </xsl:attribute>

  <xsl:copy-of select="."/>

</xsl:element>

</xsl:for-each>
</xsl:variable>

	
<xsl:template match="/SafariSearchResults">
<html>
<head>
<style>
body,td {	font-family:Verdana; font-size: 9pt;}
.hit {	font-family:Verdana; font-size: 8pt;}
.hitword {	font-weight: bold;}
.indexterm { font-size: 7pt; }
.secweight { color: red; font-weight: bold; font-size: 8pt}
.bookweight { font-weight: bold; font-size: 12pt}
</style>
</head>
<body><blockquote>

<p><a href="ftsearch.html">search again</a></p>

<table cellpadding="8" cellspacing="0"><tr><td valign="top" width="50%">

<xsl:apply-templates/>

</td><td valign="top">

<xsl:for-each select="msxsl:node-set($sects)/sect">
<xsl:sort data-type="number" order="descending" select="./@weight"/> 
<xsl:if test="@weight > 0">
<p>(<span class="secweight"><xsl:value-of select="./@weight"/></span>)
<A>
<xsl:attribute name="HREF">
http://safari.oreilly.com/main.asp?bookname=<xsl:value-of select='./@bookname'/>&amp;snode=<xsl:value-of select='./@snode'/>
</xsl:attribute>
<xsl:value-of select="@stitle"/>
</A>
(<A>
<xsl:attribute name="HREF">
http://safari.oreilly.com/main.asp?bookname=<xsl:value-of select='./@bookname'/>
</xsl:attribute>
<xsl:value-of select="./@bookname"/>
</A>)
<xsl:apply-templates select="."/>

<xsl:for-each select="msxsl:node-set($indexterms)">
<xsl:if test="../@snode = ../../@snode">
<xsl:value-of select="."/>
<xsl:apply-templates />
</xsl:if>
</xsl:for-each>

</p>
</xsl:if>


</xsl:for-each>

</td></tr></table>

</blockquote></body>
</html>
</xsl:template>

<xsl:template match="book">

<xsl:variable name="weight">
<xsl:value-of select="substring-before(@weight,'%')"/>
</xsl:variable>

<xsl:if test="not(starts-with($weight,'&lt;'))">
<p>
<A>
<xsl:attribute name="HREF">
http://safari.oreilly.com/main.asp?bookname=<xsl:value-of select='@bookname'/>
</xsl:attribute>
<xsl:value-of select="title"/>
</A>
(<span class="bookweight"><xsl:value-of select="$weight"/></span>)
<xsl:apply-templates select="section"/>
</p>
</xsl:if>

</xsl:template>

<xsl:template match="section">

<xsl:variable name="weight">
<xsl:value-of select="substring-before(@weight,'%')"/>
</xsl:variable>
	
<xsl:if test="not(starts-with($weight,'&lt;'))">
<div>
<xsl:value-of select="title"/>
<xsl:variable name="ancestorWeight">
<xsl:value-of select="substring-before(ancestor::book/@weight,'%')"/>
</xsl:variable>
(<span class="secweight"><xsl:value-of select=" ($weight * $ancestorWeight)"/></span>)
</div>
</xsl:if>

</xsl:template>

<xsl:template match="sentence">
<p class="hit">
<xsl:apply-templates/>
</p>
</xsl:template>


<xsl:template match="indexterm|primary|secondary|tertiary"/>

<xsl:template match="idxterm">
<xsl:value-of select="."/>
<span class="indexterm">
<div>[indexed as: <xsl:apply-templates />]</div>
</span>
</xsl:template>

<xsl:template match="hlhit">
<span class="hitword"><xsl:apply-templates/></span>
</xsl:template>

<xsl:template match="idxprimary">
(primary)
<xsl:apply-templates /> 
<xsl:if test="../secondary">
 ;
</xsl:if>
</xsl:template>

<xsl:template match="idxsecondary">
(secondary)
<xsl:apply-templates /> 
<xsl:if test="../tertiary">
 ;
</xsl:if>
</xsl:template>

<xsl:template match="idxtertiary">
(tertiary)
<xsl:apply-templates /> 
</xsl:template>

</xsl:stylesheet>
