Advertisement
Guest User

XSLTSearch with title highlighting

a guest
Aug 4th, 2012
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 41.21 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE xsl:stylesheet [
  3. <!ENTITY nbsp "&#x00A0;">
  4. ]>
  5. <xsl:stylesheet version="1.0"
  6. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  7. xmlns:msxml="urn:schemas-microsoft-com:xslt"
  8. xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  9. xmlns:umbraco.library="urn:umbraco.library"
  10. xmlns:PS.XSLTsearch="urn:PS.XSLTsearch"
  11. exclude-result-prefixes="msxml umbraco.library PS.XSLTsearch">
  12.  
  13. <xsl:output method="xml" omit-xml-declaration="yes" />
  14.  
  15. <!--
  16. ======================================================================
  17. XSLTsearch.xslt
  18. ======================================================================
  19. Copyright 2006-2011 Percipient Studios. All rights reserved.
  20. MIT License (http://www.opensource.org/licenses/mit-license.php)
  21.  
  22. Version 3.0.1 - For umbraco 4.5+ and new XML schema
  23. Fixed xslt error when a previewed field had fewer than six characters
  24. Changed default behavior to only search within current site if source node id is not specified (better for multiple sites in one installation)
  25. Multi-site searching made easier. Now searches only within the current site if the source= parameter is not specified (better for multiple sites in one installation)
  26.  
  27. Version 3.0.2 - Fixed issue of PreviewMode="CONTEXT" in which the search term would not be highlighted if it were the last word in the content being searched
  28.  
  29. Version 3.0.3 - Removed extraneous whitespace and empty quotes from search term
  30.  
  31. Version 3.0.4 - Additional fix for PreviewMode="CONTEXT" in which the search term would not be displayed if it were the last word in the content being search and the search term were more than $maxChars from the beginning of the search field
  32. Removed errant 'xmp' debug statement that appeared in v3.0.3
  33.  
  34. www.percipientstudios.com
  35. ======================================================================
  36. -->
  37.  
  38. <xsl:param name="currentPage"/>
  39.  
  40. <xsl:variable name="startTime" select="PS.XSLTsearch:getTime()"/>
  41. <xsl:variable name="currentID" select="$currentPage/@id"/>
  42. <xsl:variable name="XSLTsearchVersion" select="'3.0.4'"/>
  43.  
  44. <!-- DICTIONARY parameters for localization. Use default (English) text if dictionary item does not exist or has no value -->
  45. <xsl:variable name="dictionaryButton-Search" select="PS.XSLTsearch:getDictionaryParameter(string(umbraco.library:GetDictionaryItem('[XSLTsearch]Button-Search')), 'Search')"/>
  46. <xsl:variable name="dictionaryDescription-Context" select="PS.XSLTsearch:getDictionaryParameter(string(umbraco.library:GetDictionaryItem('[XSLTsearch]Description-Context')), 'Context')"/>
  47. <xsl:variable name="dictionaryDescription-ContextUnavailable" select="PS.XSLTsearch:getDictionaryParameter(string(umbraco.library:GetDictionaryItem('[XSLTsearch]Description-ContextUnavailable')), 'unavailable')"/>
  48. <xsl:variable name="dictionaryHeading-SearchResults" select="PS.XSLTsearch:getDictionaryParameter(string(umbraco.library:GetDictionaryItem('[XSLTsearch]Heading-SearchResults')), 'Search Results')"/>
  49. <xsl:variable name="dictionaryNavigation-Next" select="PS.XSLTsearch:getDictionaryParameter(string(umbraco.library:GetDictionaryItem('[XSLTsearch]Navigation-Next')), 'Next')"/>
  50. <xsl:variable name="dictionaryNavigation-Previous" select="PS.XSLTsearch:getDictionaryParameter(string(umbraco.library:GetDictionaryItem('[XSLTsearch]Navigation-Previous')), 'Previous')"/>
  51. <xsl:variable name="dictionaryPageRange-ShowingResults" select="PS.XSLTsearch:getDictionaryParameter(string(umbraco.library:GetDictionaryItem('[XSLTsearch]PageRange-ShowingResults')), 'Showing results')"/>
  52. <xsl:variable name="dictionaryPageRange-To" select="PS.XSLTsearch:getDictionaryParameter(string(umbraco.library:GetDictionaryItem('[XSLTsearch]PageRange-To')), 'to')"/>
  53. <xsl:variable name="dictionaryScore-Score" select="PS.XSLTsearch:getDictionaryParameter(string(umbraco.library:GetDictionaryItem('[XSLTsearch]Score-Score')), 'score')"/>
  54. <xsl:variable name="dictionaryStats-PagesIn" select="PS.XSLTsearch:getDictionaryParameter(string(umbraco.library:GetDictionaryItem('[XSLTsearch]Stats-PagesIn')), 'pages in')"/>
  55. <xsl:variable name="dictionaryStats-Searched" select="PS.XSLTsearch:getDictionaryParameter(string(umbraco.library:GetDictionaryItem('[XSLTsearch]Stats-Searched')), 'Searched')"/>
  56. <xsl:variable name="dictionaryStats-Seconds" select="PS.XSLTsearch:getDictionaryParameter(string(umbraco.library:GetDictionaryItem('[XSLTsearch]Stats-Seconds')), 'seconds')"/>
  57. <xsl:variable name="dictionarySummary-Matches" select="PS.XSLTsearch:getDictionaryParameter(string(umbraco.library:GetDictionaryItem('[XSLTsearch]Summary-Matches')), 'matches')"/>
  58. <xsl:variable name="dictionarySummary-NoMatchesWereFoundFor" select="PS.XSLTsearch:getDictionaryParameter(string(umbraco.library:GetDictionaryItem('[XSLTsearch]Summary-NoMatchesWereFoundFor')), 'No matches were found for')"/>
  59. <xsl:variable name="dictionarySummary-Page" select="PS.XSLTsearch:getDictionaryParameter(string(umbraco.library:GetDictionaryItem('[XSLTsearch]Summary-Page')), 'page')"/>
  60. <xsl:variable name="dictionarySummary-Pages" select="PS.XSLTsearch:getDictionaryParameter(string(umbraco.library:GetDictionaryItem('[XSLTsearch]Summary-Pages')), 'pages')"/>
  61. <xsl:variable name="dictionarySummary-YourSearchFor" select="PS.XSLTsearch:getDictionaryParameter(string(umbraco.library:GetDictionaryItem('[XSLTsearch]Summary-YourSearchFor')), 'Your search for')"/>
  62.  
  63. <!-- MACRO parameters get default values if not passed in from the macro -->
  64. <xsl:variable name="source" select="string(PS.XSLTsearch:getParameter(string(/macro/source), '-1'))"/>
  65. <xsl:variable name="resultsPerPage" select="string(PS.XSLTsearch:getParameter(string(/macro/resultsPerPage), '5'))"/>
  66. <xsl:variable name="previewChars" select="string(PS.XSLTsearch:getParameter(string(/macro/previewChars), '255'))"/>
  67. <xsl:variable name="searchBoxLocation" select="PS.XSLTsearch:uppercase(PS.XSLTsearch:getParameter(string(/macro/searchBoxLocation), 'bottom'))"/>
  68. <!-- valid choices are: 'bottom' or 'top' or 'both' or 'none' -->
  69. <xsl:variable name="previewType" select="PS.XSLTsearch:uppercase(PS.XSLTsearch:getParameter(string(/macro/previewType), 'beginning'))"/>
  70. <!-- valid choices are: 'beginning' and 'context' -->
  71. <xsl:variable name="showPageRange" select="PS.XSLTsearch:uppercase(PS.XSLTsearch:getParameter(string(/macro/showPageRange), '0'))"/>
  72. <xsl:variable name="showOrdinals" select="PS.XSLTsearch:uppercase(PS.XSLTsearch:getParameter(string(/macro/showOrdinals), '0'))"/>
  73. <xsl:variable name="showScores" select="PS.XSLTsearch:uppercase(PS.XSLTsearch:getParameter(string(/macro/showScores), '0'))"/>
  74. <xsl:variable name="showStats" select="PS.XSLTsearch:uppercase(PS.XSLTsearch:getParameter(string(/macro/showStats), '0'))"/>
  75. <xsl:variable name="showDebug" select="PS.XSLTsearch:uppercase(PS.XSLTsearch:getParameter(string(umbraco.library:RequestQueryString('umbDebugShowTrace')), '0'))"/>
  76.  
  77.  
  78. <!-- prepare for highlighting the search term within the search results by surrounding it with 'strong' tags -->
  79. <xsl:variable name="before">&lt;strong&gt;</xsl:variable>
  80. <xsl:variable name="after">&lt;/strong&gt;</xsl:variable>
  81.  
  82.  
  83. <!-- which umbraco fields to search -->
  84. <!-- Note: Comma-separated list of fields. The order of the search fields affects the search score and
  85. order of the search results! Place the more important fields first, with bodyText last.
  86. The reason is that if a search term appears in the page's title, there is a greater likelihood
  87. that page discusses the search term at length, than it simply being mentioned in the bodyText in passing.
  88. -->
  89. <xsl:variable name="searchFields" select="PS.XSLTsearch:getListParameter(string(/macro/searchFields), '@nodeName,metaKeywords,metaDescription,bodyText')"/>
  90.  
  91. <!-- which umbraco field to display for a found entry -->
  92. <!-- Note: Comma-separated list of fields. The order of the preview fields is from most preferred
  93. to least preferred. Put the most appropriate fields first (typically, bodyText).
  94. Note: ONLY works for properties, not attributes
  95. -->
  96. <xsl:variable name="previewFields" select="PS.XSLTsearch:getListParameter(string(/macro/previewFields), 'bodyText,metaDescription')"/>
  97.  
  98. <!-- the search term to look for -->
  99. <xsl:variable name="search">
  100. <xsl:choose>
  101. <!-- form field value, if present -->
  102. <xsl:when test="string(umbraco.library:RequestForm('search')) != ''">
  103. <xsl:value-of select="PS.XSLTsearch:cleanSearchTerm(PS.XSLTsearch:escapeString(string(umbraco.library:RequestForm('search'))))" />
  104. </xsl:when>
  105. <!-- querystring value, if present -->
  106. <xsl:when test="string(umbraco.library:RequestQueryString('search')) != ''">
  107. <xsl:value-of select="PS.XSLTsearch:cleanSearchTerm(PS.XSLTsearch:escapeString(string(umbraco.library:RequestQueryString('search'))))" />
  108. </xsl:when>
  109. <!-- no value -->
  110. <xsl:otherwise>
  111. <xsl:value-of select="''"/>
  112. </xsl:otherwise>
  113. </xsl:choose>
  114. </xsl:variable>
  115. <xsl:variable name="unescapedSearch" select="PS.XSLTsearch:unescapeString($search)"/>
  116.  
  117. <!-- We have to calculate matching nodes before we can finish calculating the rest of the variables/parameters... -->
  118. <!-- uppercase the search string for case-insensitive searching -->
  119. <xsl:variable name="searchUpper" select="PS.XSLTsearch:uppercase(string($search))"/>
  120.  
  121.  
  122. <!-- ============================================================= -->
  123.  
  124.  
  125. <xsl:template match="/">
  126. <!-- determine which nodeset to search through, based on the value (or absence) of the SOURCE parameter in the macro -->
  127. <xsl:choose>
  128. <!-- short-circuit the whole searching if no search-text were passed in -->
  129. <xsl:when test="$search = ''">
  130. <!-- using NO nodes; only calling the template for the form -->
  131. <xsl:call-template name="search">
  132. <xsl:with-param name="items" select="./node[1=2]"/>
  133. </xsl:call-template>
  134. </xsl:when>
  135.  
  136. <xsl:when test="number($source)= -1 or number($source)= 0">
  137. <!-- using ALL nodes -->
  138. <xsl:call-template name="search">
  139. <!-- searches absolutely all pages (useful if you want to search multiple sites at once, or if you do not have all your content pages below a common homepage node in the content tree)
  140. <xsl:with-param name="items" select="umbraco.library:GetXmlAll()/*"/>
  141. -->
  142. <!-- searches all pages within a specific site (useful if you have multiple sites in one umbraco installation) -->
  143. <xsl:with-param name="items" select="$currentPage/ancestor-or-self::* [@level = '1']"/>
  144. </xsl:call-template>
  145. </xsl:when>
  146. <xsl:when test="number($source)=$currentID">
  147. <!-- using only nodes within the search page's family tree, from top to bottom -->
  148. <xsl:call-template name="search">
  149. <xsl:with-param name="items" select="$currentPage/ancestor-or-self::* [@isDoc and @level = 1]"/>
  150. </xsl:call-template>
  151. </xsl:when>
  152. <xsl:otherwise>
  153. <!-- search only within the SOURCE node specified in the macro and all of its children -->
  154. <xsl:call-template name="search">
  155. <xsl:with-param name="items" select="./descendant-or-self::*[@isDoc]"/>
  156. </xsl:call-template>
  157. </xsl:otherwise>
  158. </xsl:choose>
  159. </xsl:template>
  160.  
  161.  
  162. <!-- ============================================================= -->
  163.  
  164.  
  165. <xsl:template name="search">
  166. <!-- Perform the search on the appropriate nodeset and display the output -->
  167. <xsl:param name="items"/>
  168.  
  169. <!-- reduce the number of nodes for applying all the functions in the next step -->
  170. <xsl:variable name="possibleNodes" select="$items/descendant-or-self::*[
  171. @isDoc
  172. and string(umbracoNaviHide) != '1'
  173. and count(attribute::id)=1
  174. and (umbraco.library:IsProtected(@id, @path) = false()
  175. or umbraco.library:HasAccess(@id, @path) = true())
  176. ]"/>
  177.  
  178. <!-- generate a string of a semicolon-delimited list of all @id's of the matching nodes -->
  179. <xsl:variable name="matchedNodesIdList">
  180. <xsl:call-template name="booleanAndMatchedNodes">
  181. <xsl:with-param name="yetPossibleNodes" select="$possibleNodes"/>
  182. <xsl:with-param name="searchTermList" select="concat($searchUpper, ' ')"/>
  183. </xsl:call-template>
  184. </xsl:variable>
  185.  
  186. <!-- get the actual matching nodes as a nodeset -->
  187. <xsl:variable name="matchedNodes" select="$possibleNodes[contains($matchedNodesIdList, concat(';', concat(@id, ';')))]" />
  188.  
  189. <!-- the current page -->
  190. <xsl:variable name="page">
  191. <xsl:choose>
  192. <!-- first page -->
  193. <xsl:when test="number(umbraco.library:RequestQueryString('page')) &lt;=1
  194. or string(umbraco.library:RequestQueryString('page')) = ''
  195. or string(number(umbraco.library:RequestQueryString('page'))) = 'NaN'
  196. or (
  197. string(umbraco.library:RequestForm('search')) != ''
  198. and string(umbraco.library:RequestForm('search')) != string(umbraco.library:RequestQueryString('search'))
  199. )
  200. ">
  201. <xsl:text>1</xsl:text>
  202. </xsl:when>
  203. <!-- last page -->
  204. <xsl:when test="number(umbraco.library:RequestQueryString('page')) &gt; count($matchedNodes) div $resultsPerPage">
  205. <xsl:value-of select="ceiling(count($matchedNodes) div $resultsPerPage)"/>
  206. </xsl:when>
  207. <!-- the value specified in the querystring -->
  208. <xsl:otherwise>
  209. <xsl:value-of select="number(umbraco.library:RequestQueryString('page'))"/>
  210. </xsl:otherwise>
  211. </xsl:choose>
  212. </xsl:variable>
  213.  
  214. <!-- calculate a few handy variables now, for easy access later -->
  215. <xsl:variable name="startMatch" select="($page - 1) * $resultsPerPage + 1"/>
  216. <xsl:variable name="endMatch">
  217. <xsl:choose>
  218. <!-- all the rest (on the last page) -->
  219. <xsl:when test="($page * $resultsPerPage) &gt; count($matchedNodes)">
  220. <xsl:value-of select="count($matchedNodes)"/>
  221. </xsl:when>
  222. <!-- only the appropriate number for this page -->
  223. <xsl:otherwise>
  224. <xsl:value-of select="$page * $resultsPerPage"/>
  225. </xsl:otherwise>
  226. </xsl:choose>
  227. </xsl:variable>
  228.  
  229. <!-- display results header information to the screen, if a search has been run; otherwise just show the search form -->
  230. <div id="xsltsearch">
  231. <xsl:if test="$unescapedSearch !=''">
  232. <!-- most pages have a search header specified in the template. If you wish XSLTsearch to display its own header, uncomment the <h2> ... </h2> lines below -->
  233. <!--
  234. <h2>
  235. <xsl:value-of select="$dictionaryHeading-SearchResults"/>
  236. </h2>
  237. -->
  238.  
  239. <!-- display search box at the top of the page (the search box is always present even if no search was attempted) -->
  240. <xsl:if test="$searchBoxLocation='TOP' or $searchBoxLocation='BOTH'">
  241. <div class="xsltsearch_form">
  242. <input name="search" type="text" class="input">
  243. <xsl:attribute name="value">
  244. <xsl:value-of select="$unescapedSearch"/>
  245. </xsl:attribute>
  246. </input>
  247. <xsl:text>&nbsp;</xsl:text>
  248. <input type="submit" class="submit">
  249. <xsl:attribute name="value">
  250. <xsl:value-of select="$dictionaryButton-Search"/>
  251. </xsl:attribute>
  252. </input>
  253. </div>
  254. </xsl:if>
  255.  
  256. <p id="xsltsearch_summary">
  257. <xsl:choose>
  258. <xsl:when test="count($matchedNodes) = 0">
  259. <xsl:value-of select="$dictionarySummary-NoMatchesWereFoundFor"/>
  260. <xsl:text> </xsl:text>
  261. <strong>
  262. <xsl:value-of select="$unescapedSearch"/>
  263. </strong>
  264. </xsl:when>
  265. <xsl:when test="count($matchedNodes) = 1">
  266. <xsl:value-of select="$dictionarySummary-YourSearchFor"/>
  267. <xsl:text> </xsl:text>
  268. <strong>
  269. <xsl:value-of select="$unescapedSearch"/>
  270. </strong>
  271. <xsl:text> </xsl:text>
  272. <xsl:value-of select="$dictionarySummary-Matches"/>
  273. <xsl:text> </xsl:text>
  274. <strong>1</strong>
  275. <xsl:text> </xsl:text>
  276. <xsl:value-of select="$dictionarySummary-Page"/>
  277. </xsl:when>
  278. <xsl:otherwise>
  279. <xsl:value-of select="$dictionarySummary-YourSearchFor"/>
  280. <xsl:text> </xsl:text>
  281. <strong>
  282. <xsl:value-of select="$unescapedSearch"/>
  283. </strong>
  284. <xsl:text> </xsl:text>
  285. <xsl:value-of select="$dictionarySummary-Matches"/>
  286. <xsl:text> </xsl:text>
  287. <strong>
  288. <xsl:value-of select="count($matchedNodes)"/>
  289. </strong>
  290. <xsl:text> </xsl:text>
  291. <xsl:value-of select="$dictionarySummary-Pages"/>
  292. </xsl:otherwise>
  293. </xsl:choose>
  294.  
  295. <!-- show the page number range. Useful if you don't show the ordinal for the result -->
  296. <xsl:if test="$showPageRange != '0'">
  297. <xsl:if test="count($matchedNodes) &gt; 0">
  298. <br />
  299. <span id="xsltsearch_pagerange">
  300. <xsl:value-of select="$dictionaryPageRange-ShowingResults"/>
  301. <xsl:text> </xsl:text>
  302. <xsl:value-of select="$startMatch"/>
  303. <xsl:if test="$startMatch != $endMatch">
  304. <xsl:text> </xsl:text>
  305. <xsl:value-of select="$dictionaryPageRange-To"/>
  306. <xsl:text> </xsl:text>
  307. <xsl:value-of select="$endMatch"/>
  308. </xsl:if>
  309. </span>
  310. </xsl:if>
  311. </xsl:if>
  312. </p>
  313.  
  314. <!-- Now we need to sort the pages by score/relevance before sending them to the screen.
  315. We'll cycle through matched nodes once to save their pageScore in a variable -->
  316. <xsl:variable name="pageScoreList">
  317. <xsl:text>;</xsl:text>
  318. <xsl:for-each select="$matchedNodes">
  319. <!-- unique id for this node -->
  320. <xsl:value-of select="generate-id(.)"/>
  321. <xsl:text>=</xsl:text>
  322. <!-- weighted score for the matches -->
  323. <xsl:call-template name="pageScore">
  324. <xsl:with-param name="item" select="."/>
  325. </xsl:call-template>
  326. <xsl:text>;</xsl:text>
  327. </xsl:for-each>
  328. </xsl:variable>
  329.  
  330. <!-- display search results to the screen -->
  331. <div id="xsltsearch_results">
  332. <xsl:if test="count($matchedNodes) = 0">
  333. <xsl:text disable-output-escaping="yes"> </xsl:text>
  334. </xsl:if>
  335.  
  336. <xsl:for-each select="$matchedNodes">
  337. <!-- sort on the page score of the node, within ALL the nodes to return (the sorting must happen before choosing the nodes for this page) -->
  338. <xsl:sort select="substring-before(substring-after($pageScoreList, concat(';',generate-id(.),'=')),';')" data-type="number" order="descending"/>
  339.  
  340. <!-- only the nodes for this page -->
  341. <xsl:if test="position() &gt;= $startMatch and position() &lt;= $endMatch">
  342.  
  343. <div class="xsltsearch_result">
  344. <p class="xsltsearch_result_title">
  345. <!-- show the ordinal number for this result -->
  346. <xsl:if test="$showOrdinals != '0'">
  347. <span class="xsltsearch_ordinal">
  348. <xsl:value-of select="position()"/>
  349. <xsl:text>.&nbsp;</xsl:text>
  350. </span>
  351. </xsl:if>
  352.  
  353.  
  354. <!-- page name and url -->
  355. <a href="{umbraco.library:NiceUrl(@id)}" class="xsltsearch_title">
  356. <xsl:value-of select="PS.XSLTsearch:surround(@nodeName, $search, $before, $after)" disable-output-escaping="yes" />
  357. </a>
  358.  
  359. <!-- show the pageScore/relevance rating -->
  360. <xsl:if test="$showScores != '0'">
  361. <span class="xsltsearch_score">
  362. <xsl:text> (</xsl:text>
  363. <xsl:value-of select="$dictionaryScore-Score"/>
  364. <xsl:text>: </xsl:text>
  365. <xsl:value-of select="substring-before(substring-after($pageScoreList, concat(';',generate-id(.),'=')),';')"/>
  366. <xsl:text>)</xsl:text>
  367. </span>
  368. </xsl:if>
  369. </p>
  370.  
  371. <xsl:variable name="displayField">
  372. <xsl:call-template name="displayFieldText">
  373. <xsl:with-param name="item" select="."/>
  374. <xsl:with-param name="fieldList" select="$previewFields"/>
  375. </xsl:call-template>
  376. </xsl:variable>
  377.  
  378. <!-- contents of search result -->
  379. <xsl:variable name="strippedHTML" select="umbraco.library:StripHtml($displayField)"/>
  380.  
  381. <xsl:variable name="escapedData">
  382. <xsl:choose>
  383. <!-- display content of the search result, if available -->
  384. <xsl:when test="string($strippedHTML) != ''">
  385. <xsl:value-of select="$strippedHTML"/>
  386. </xsl:when>
  387. </xsl:choose>
  388. </xsl:variable>
  389.  
  390.  
  391. <!-- display a portion of the page's text -->
  392. <!-- display first words of the text -->
  393. <xsl:if test="$previewType = 'BEGINNING' and string-length($escapedData &gt; 0)">
  394. <p class="xsltsearch_result_description">
  395. <span class="xsltsearch_description">
  396. <xsl:value-of select="PS.XSLTsearch:surround(PS.XSLTsearch:escapingIntelligentSubstring($escapedData, 0, $previewChars), $search, $before, $after)" disable-output-escaping="yes"/>
  397. <!-- add an elipsis if there is more text than we are showing on the search results page -->
  398. <xsl:if test="string-length($escapedData) &gt; $previewChars">...</xsl:if>
  399. </span>
  400. </p>
  401. </xsl:if>
  402.  
  403. <!-- or, display the actual place(s) where the search term was found and highlight them in context
  404. providing a few words on either side of the search term. -->
  405. <xsl:if test="$previewType = 'CONTEXT' and string-length($escapedData &gt; 0)">
  406. <p class="xsltsearch_result_description">
  407. <span class="xsltsearch_description">
  408. <i>
  409. <xsl:value-of select="$dictionaryDescription-Context"/>
  410. <xsl:text>: </xsl:text>
  411. </i>
  412. <xsl:variable name="context" select="PS.XSLTsearch:contextOfFind(string($escapedData), $search, 5, 5, $previewChars)"/>
  413. <xsl:choose>
  414. <xsl:when test="string($context) != ''">
  415. <xsl:value-of select="PS.XSLTsearch:surround($context, $search, $before, $after)" disable-output-escaping="yes"/>
  416. </xsl:when>
  417. <xsl:otherwise>
  418. <i>
  419. <xsl:value-of select="$dictionaryDescription-ContextUnavailable"/>
  420. </i>
  421. </xsl:otherwise>
  422. </xsl:choose>
  423. </span>
  424. </p>
  425. </xsl:if>
  426.  
  427. <!-- OPTIONAL: include any additional information regarding the search result you want to display, such as createDate, updateDate, author, etc. -->
  428.  
  429. </div>
  430. </xsl:if>
  431.  
  432. </xsl:for-each>
  433. </div>
  434.  
  435. <!-- display paging navigation links, if needed -->
  436. <xsl:if test="$resultsPerPage &lt; count($matchedNodes)">
  437. <xsl:call-template name="searchNavigation">
  438. <xsl:with-param name="page" select="$page"/>
  439. <xsl:with-param name="matchedNodes" select="$matchedNodes"/>
  440. </xsl:call-template>
  441. </xsl:if>
  442.  
  443. </xsl:if>
  444.  
  445. <!-- display search box at the bottom of the page (the search box is always present even if no search was attempted) -->
  446. <xsl:if test="$searchBoxLocation='BOTTOM' or $searchBoxLocation='BOTH' or ($searchBoxLocation!='NONE' and $unescapedSearch ='')">
  447. <div class="xsltsearch_form">
  448. <input name="search" type="text" class="input">
  449. <xsl:attribute name="value">
  450. <xsl:value-of select="$unescapedSearch"/>
  451. </xsl:attribute>
  452. </input>
  453. <xsl:text>&nbsp;</xsl:text>
  454. <input type="submit" class="submit">
  455. <xsl:attribute name="value">
  456. <xsl:value-of select="$dictionaryButton-Search"/>
  457. </xsl:attribute>
  458. </input>
  459. </div>
  460. </xsl:if>
  461.  
  462. <!-- display search execution time and stats -->
  463. <xsl:if test="$showStats != '0'">
  464. <xsl:if test="$search !=''">
  465. <p id="xsltsearch_stats">
  466. <xsl:value-of select="$dictionaryStats-Searched"/>
  467. <xsl:text> </xsl:text>
  468. <xsl:value-of select="count($possibleNodes)"/>
  469. <xsl:text> </xsl:text>
  470. <xsl:value-of select="$dictionaryStats-PagesIn"/>
  471. <xsl:text> </xsl:text>
  472. <xsl:value-of select="round(PS.XSLTsearch:getTimeSpan($startTime, PS.XSLTsearch:getTime())) div 1000"/>
  473. <xsl:text> </xsl:text>
  474. <xsl:value-of select="$dictionaryStats-Seconds"/>
  475. </p>
  476. </xsl:if>
  477. </xsl:if>
  478.  
  479. <!-- display XSLTsearch version information if in debug mode (that is, if the querystring contains umbDebugShowTrace=true) -->
  480. <xsl:if test="$showDebug != '0'">
  481. <p id="xsltsearch_debug">
  482. <xsl:text>XSLTsearch version </xsl:text>
  483. <xsl:value-of select="$XSLTsearchVersion"/>
  484. </p>
  485. </xsl:if>
  486.  
  487. <!-- don't let a collapsed div tag break the page layout -->
  488. <xsl:if test="$unescapedSearch='' and $searchBoxLocation='NONE'">
  489. <xsl:text>&nbsp;</xsl:text>
  490. </xsl:if>
  491. </div>
  492. </xsl:template>
  493.  
  494.  
  495. <!-- ============================================================= -->
  496.  
  497.  
  498. <xsl:template name="searchNavigation">
  499. <!-- navigation template (Note: we're just using hrefs with querystrings) -->
  500. <xsl:param name="page"/>
  501. <xsl:param name="matchedNodes"/>
  502.  
  503. <p id="xsltsearch_navigation">
  504. <!-- previous page -->
  505. <a id="previous">
  506. <xsl:choose>
  507. <xsl:when test="$page &lt;= 1">
  508. <xsl:attribute name="class">disabled</xsl:attribute>
  509. </xsl:when>
  510. <xsl:otherwise>
  511. <xsl:attribute name="href">
  512. <xsl:text>?search=</xsl:text>
  513. <xsl:value-of select="$search"/>
  514. <xsl:text>&amp;page=</xsl:text>
  515. <xsl:value-of select="$page - 1"/>
  516. </xsl:attribute>
  517. </xsl:otherwise>
  518. </xsl:choose>
  519. <xsl:text>&lt; </xsl:text>
  520. <xsl:value-of select="$dictionaryNavigation-Previous"/>
  521. </a>
  522. <xsl:text>&nbsp;&nbsp;</xsl:text>
  523.  
  524. <!-- each paged set of results is listed, with a link to that page set -->
  525. <xsl:call-template name="pageNumbers">
  526. <xsl:with-param name="pageIndex" select="1"/>
  527. <xsl:with-param name="page" select="$page"/>
  528. <xsl:with-param name="matchedNodes" select="$matchedNodes"/>
  529. </xsl:call-template>
  530.  
  531. <!-- next page -->
  532. <xsl:text>&nbsp;</xsl:text>
  533. <a id="next">
  534. <xsl:choose>
  535. <xsl:when test="$page * $resultsPerPage &gt;= count($matchedNodes)">
  536. <xsl:attribute name="class">disabled</xsl:attribute>
  537. </xsl:when>
  538. <xsl:otherwise>
  539. <xsl:attribute name="href">
  540. <xsl:text>?search=</xsl:text>
  541. <xsl:value-of select="$search"/>
  542. <xsl:text>&amp;page=</xsl:text>
  543. <xsl:value-of select="$page + 1"/>
  544. </xsl:attribute>
  545. </xsl:otherwise>
  546. </xsl:choose>
  547. <xsl:value-of select="$dictionaryNavigation-Next"/>
  548. <xsl:text> &gt;</xsl:text>
  549. </a>
  550. </p>
  551. </xsl:template>
  552.  
  553.  
  554. <!-- ============================================================= -->
  555.  
  556.  
  557. <xsl:template name="pageNumbers">
  558. <!-- paged results template -->
  559. <xsl:param name="page"/>
  560. <xsl:param name="pageIndex"/>
  561. <xsl:param name="matchedNodes"/>
  562.  
  563. <xsl:variable name="distanceFromCurrent" select="$pageIndex - $page"/>
  564.  
  565. <xsl:choose>
  566. <xsl:when test="$pageIndex = $page">
  567. <!-- current paged set -->
  568. <strong>
  569. <xsl:value-of select="$pageIndex"/>
  570. </strong>
  571. <xsl:text>&nbsp;</xsl:text>
  572. </xsl:when>
  573.  
  574. <!-- show a maximum of nine paged sets on either side of the current paged set; just like Google does it -->
  575. <xsl:when test="($distanceFromCurrent &gt; -10 and $distanceFromCurrent &lt; 10)">
  576. <a>
  577. <xsl:attribute name="href">
  578. <xsl:text>?search=</xsl:text>
  579. <xsl:value-of select="$search"/>
  580. <xsl:text>&amp;page=</xsl:text>
  581. <xsl:value-of select="$pageIndex"/>
  582. </xsl:attribute>
  583. <xsl:value-of select="$pageIndex"/>
  584. </a>
  585. <xsl:text>&nbsp;</xsl:text>
  586. </xsl:when>
  587. </xsl:choose>
  588.  
  589. <!-- recursively call the template for all the paged sets -->
  590. <xsl:if test="$pageIndex * $resultsPerPage &lt; count($matchedNodes)">
  591. <xsl:call-template name="pageNumbers">
  592. <xsl:with-param name="pageIndex" select="$pageIndex + 1"/>
  593. <xsl:with-param name="page" select="$page"/>
  594. <xsl:with-param name="matchedNodes" select="$matchedNodes"/>
  595. </xsl:call-template>
  596. </xsl:if>
  597. </xsl:template>
  598.  
  599.  
  600. <!-- ============================================================= -->
  601.  
  602.  
  603. <xsl:template name="pageScore">
  604. <xsl:param name="item"/>
  605.  
  606. <!-- sum up scores for each of the attributes -->
  607. <xsl:variable name="scoreA">
  608. <xsl:call-template name="scoreAttributes">
  609. <xsl:with-param name="item" select="$item"/>
  610. </xsl:call-template>
  611. </xsl:variable>
  612.  
  613. <!-- sum up scores for each of the data nodes -->
  614. <xsl:variable name="scoreD">
  615. <xsl:call-template name="scoreDataNodes">
  616. <xsl:with-param name="item" select="$item[@isDoc]/*[not(@isDoc) and contains($searchFields, concat(',',name(),','))]"/>
  617. </xsl:call-template>
  618. </xsl:variable>
  619.  
  620. <xsl:value-of select="number($scoreA) + number($scoreD)"/>
  621. </xsl:template>
  622.  
  623.  
  624. <!-- ============================================================= -->
  625.  
  626.  
  627. <xsl:template name="scoreAttributes">
  628. <xsl:param name="item"/>
  629. <xsl:param name="index" select="1"/>
  630. <xsl:param name="score" select="0"/>
  631.  
  632. <!-- name of the attribute on which we're searching -->
  633. <xsl:variable name="attributeName" select="name($item/attribute::*[position()=$index])"/>
  634.  
  635. <xsl:variable name="weighting">
  636. <xsl:if test="contains($searchFields, concat(',@',$attributeName,','))">
  637. <xsl:value-of select="PS.XSLTsearch:power(2, number(PS.XSLTsearch:hitCount(substring-after($searchFields,$attributeName), ','))-1)"/>
  638. </xsl:if>
  639. </xsl:variable>
  640.  
  641. <!-- calculate the final, cumulative, weighted score for this field -->
  642. <xsl:variable name="thisScore">
  643. <xsl:choose>
  644. <xsl:when test="contains($searchFields, concat(',@',$attributeName,','))">
  645. <!-- only calculate when this is a field actually being searched -->
  646. <xsl:call-template name="scoreForBooleanSearch">
  647. <xsl:with-param name="weighting" select="$weighting"/>
  648. <xsl:with-param name="toSearch" select="umbraco.library:StripHtml(string($item/attribute::*[name()=$attributeName]))"/>
  649. <xsl:with-param name="searchTermList" select="concat($search, ' ')"/>
  650. </xsl:call-template>
  651. </xsl:when>
  652. <xsl:otherwise>0</xsl:otherwise>
  653. </xsl:choose>
  654. </xsl:variable>
  655.  
  656. <xsl:choose>
  657. <xsl:when test="count(./attribute::*)=$index">
  658. <!-- all done; print out total weight score -->
  659. <xsl:value-of select="$score + $thisScore"/>
  660. </xsl:when>
  661. <xsl:otherwise>
  662. <!-- continue recursion for other fields -->
  663. <xsl:call-template name="scoreAttributes">
  664. <xsl:with-param name="item" select="$item"/>
  665. <xsl:with-param name="index" select="$index + 1"/>
  666. <xsl:with-param name="score" select="$score + $thisScore"/>
  667. </xsl:call-template>
  668. </xsl:otherwise>
  669. </xsl:choose>
  670. </xsl:template>
  671.  
  672.  
  673. <!-- ============================================================= -->
  674.  
  675.  
  676. <xsl:template name="scoreForBooleanSearch">
  677. <xsl:param name="weighting" select="1"/>
  678. <xsl:param name="toSearch"/>
  679. <xsl:param name="searchTermList"/>
  680. <xsl:param name="currentHitCount" select="0"/>
  681.  
  682. <!-- next search term -->
  683. <xsl:variable name="searchTerm">
  684. <xsl:value-of select="PS.XSLTsearch:getFirstElement($searchTermList, ' ')"/>
  685. </xsl:variable>
  686.  
  687. <!-- remaining search terms -->
  688. <xsl:variable name="remainingSearchTermList">
  689. <xsl:value-of select="PS.XSLTsearch:removeFirstElement($searchTermList, ' ')"/>
  690. </xsl:variable>
  691.  
  692. <!-- hit count for this search term -->
  693. <xsl:variable name="thisHitCount">
  694. <xsl:value-of select="PS.XSLTsearch:hitCount($toSearch, string($searchTerm))"/>
  695. </xsl:variable>
  696.  
  697. <xsl:choose>
  698. <xsl:when test="string-length($remainingSearchTermList) &gt; 1">
  699. <!-- continue to search the rest of the terms -->
  700. <xsl:call-template name="scoreForBooleanSearch">
  701. <xsl:with-param name="weighting" select="$weighting"/>
  702. <xsl:with-param name="toSearch" select="$toSearch"/>
  703. <xsl:with-param name="searchTermList" select="$remainingSearchTermList"/>
  704. <xsl:with-param name="currentHitCount" select="$currentHitCount + $thisHitCount"/>
  705. </xsl:call-template>
  706. </xsl:when>
  707.  
  708. <xsl:otherwise>
  709. <!-- finished searching: return the total hit count * weighting -->
  710. <xsl:value-of select="number($currentHitCount + $thisHitCount) * $weighting"/>
  711. </xsl:otherwise>
  712. </xsl:choose>
  713. </xsl:template>
  714.  
  715.  
  716. <!-- ============================================================= -->
  717.  
  718.  
  719. <xsl:template name="scoreDataNodes">
  720. <xsl:param name="item"/>
  721. <xsl:param name="score" select="0"/>
  722.  
  723. <!-- the weighting to apply to hits in this search field -->
  724. <xsl:variable name="weighting" select="PS.XSLTsearch:power(2, number(PS.XSLTsearch:hitCount(substring-after($searchFields,string(name($item))), ','))-1)"/>
  725.  
  726. <!-- calculate the final, cumulative, weighted score for this field -->
  727. <xsl:variable name="thisScore">
  728. <xsl:choose>
  729. <xsl:when test="contains($searchFields, concat(',',name($item),','))">
  730. <!-- only calculate when this is a field actually being searched -->
  731. <xsl:call-template name="scoreForBooleanSearch">
  732. <xsl:with-param name="weighting" select="$weighting"/>
  733. <xsl:with-param name="toSearch" select="umbraco.library:StripHtml(string($item))"/>
  734. <xsl:with-param name="searchTermList" select="concat($search, ' ')"/>
  735. </xsl:call-template>
  736. </xsl:when>
  737. <xsl:otherwise>0</xsl:otherwise>
  738. </xsl:choose>
  739. </xsl:variable>
  740.  
  741. <!-- remaining data nodes whose hitCounts we need to tally; for recursion use -->
  742. <xsl:variable name="remaining" select="$item/following-sibling::*[not(@isDoc) and contains($searchFields, concat(',',name(),','))]"/>
  743. <xsl:choose>
  744. <xsl:when test="count($remaining) = 0">
  745. <!-- all done; return the final score -->
  746. <xsl:value-of select="number($thisScore) + number($score)"/>
  747. </xsl:when>
  748. <xsl:otherwise>
  749. <!-- keep summing the hitCounts for all the fields for this page by calling the pageScore template recursively -->
  750. <xsl:call-template name="scoreDataNodes">
  751. <xsl:with-param name="item" select="$remaining[position()=1]"/>
  752. <xsl:with-param name="score" select="number($thisScore) + number($score)"/>
  753. </xsl:call-template>
  754. </xsl:otherwise>
  755. </xsl:choose>
  756. </xsl:template>
  757.  
  758.  
  759. <!-- ============================================================= -->
  760.  
  761.  
  762. <xsl:template name="displayFieldText">
  763. <xsl:param name="item"/>
  764. <xsl:param name="fieldList"/>
  765.  
  766. <xsl:variable name="fieldName">
  767. <xsl:value-of select="PS.XSLTsearch:getFirstElement($fieldList, ',')"/>
  768. </xsl:variable>
  769. <xsl:variable name="remainingFields">
  770. <xsl:value-of select="PS.XSLTsearch:removeFirstElement($fieldList, ',')"/>
  771. </xsl:variable>
  772.  
  773. <xsl:choose>
  774. <!-- actually print out field, if it exists and has content -->
  775. <xsl:when test="count($item/*[not(@isDoc) and name()=string($fieldName)]) = 1 and string($item/*[not(@isDoc) and name()=string($fieldName)]) != ''">
  776. <xsl:value-of select="string($item/*[not(@isDoc) and name()=string($fieldName)])"/>
  777. </xsl:when>
  778.  
  779. <xsl:when test="$remainingFields != ''">
  780. <!-- if this element does not exist, go on to the next one -->
  781. <xsl:call-template name="displayFieldText">
  782. <xsl:with-param name="item" select="$item"/>
  783. <xsl:with-param name="fieldList" select="$remainingFields"/>
  784. </xsl:call-template>
  785. </xsl:when>
  786. </xsl:choose>
  787. </xsl:template>
  788.  
  789.  
  790. <!-- ============================================================= -->
  791.  
  792.  
  793. <xsl:template name="booleanAndMatchedNodes">
  794. <xsl:param name="yetPossibleNodes"/>
  795. <xsl:param name="searchTermList"/>
  796.  
  797. <xsl:variable name="searchTerm">
  798. <xsl:value-of select="PS.XSLTsearch:unescapeString(PS.XSLTsearch:getFirstElement($searchTermList, ' '))"/>
  799. </xsl:variable>
  800. <xsl:variable name="remainingSearchTermList">
  801. <xsl:value-of select="PS.XSLTsearch:removeFirstElement($searchTermList, ' ')"/>
  802. </xsl:variable>
  803.  
  804. <xsl:variable name="searchFieldsAttribsOnly">
  805. <xsl:variable name="sf" select="umbraco.library:Split($searchFields, ',')" />
  806. <xsl:for-each select="$sf//value">
  807. <xsl:if test="substring(., 1, 1) = '@'">
  808. <xsl:text>,</xsl:text>
  809. <xsl:value-of select="substring-after(., '@') "/>
  810. <xsl:text>,</xsl:text>
  811. </xsl:if>
  812. </xsl:for-each>
  813. </xsl:variable>
  814.  
  815. <xsl:variable name="attribNodes" select="$yetPossibleNodes[@isDoc
  816. and attribute::*[
  817. contains($searchFieldsAttribsOnly,concat(',', name(), ','))
  818. and contains(PS.XSLTsearch:uppercase(PS.XSLTsearch:unescapeString(umbraco.library:StripHtml(string(.)))), $searchTerm)
  819. ]
  820. ]" />
  821.  
  822. <xsl:variable name="propertyNodes" select="$yetPossibleNodes/*[count(@isDoc) = 0
  823. and contains($searchFields,concat(',', name(), ','))
  824. and contains(PS.XSLTsearch:uppercase(PS.XSLTsearch:unescapeString(umbraco.library:StripHtml(string(.)))), $searchTerm)
  825. ]/.." />
  826.  
  827. <xsl:variable name="evenYetPossibleNodes" select="$attribNodes | $propertyNodes" />
  828.  
  829. <xsl:choose>
  830. <xsl:when test="string-length($remainingSearchTermList) &gt; 1">
  831. <!-- continue to search the rest of the terms -->
  832. <xsl:call-template name="booleanAndMatchedNodes">
  833. <xsl:with-param name="yetPossibleNodes" select="$evenYetPossibleNodes"/>
  834. <xsl:with-param name="searchTermList" select="$remainingSearchTermList"/>
  835. </xsl:call-template>
  836. </xsl:when>
  837.  
  838. <xsl:otherwise>
  839. <!-- finished searching: return a list of the attribute @id's of the currently possible nodes as the final set of matched nodes -->
  840. <xsl:variable name="nodeIDList">
  841. <xsl:text>;</xsl:text>
  842. <xsl:for-each select="$evenYetPossibleNodes">
  843. <!-- @id for this node -->
  844. <xsl:choose>
  845. <xsl:when test="@isDoc">
  846. <xsl:value-of select="@id"/>
  847. </xsl:when>
  848. <xsl:otherwise>
  849. <xsl:value-of select="../@id"/>
  850. </xsl:otherwise>
  851. </xsl:choose>
  852. <xsl:text>;</xsl:text>
  853. </xsl:for-each>
  854. </xsl:variable>
  855.  
  856. <!-- return the actual list of id's -->
  857. <xsl:value-of select="$nodeIDList"/>
  858. </xsl:otherwise>
  859. </xsl:choose>
  860. </xsl:template>
  861.  
  862.  
  863. <!-- ============================================================= -->
  864.  
  865.  
  866. </xsl:stylesheet>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement