Percipient Studios logo

Customizing XSLTsearch

Tuesday, April 07, 2009
Filed under: xslt, xsltsearch - by Douglas Robar

Most of the modifications people want to make to XSLTsearch have to do with limiting or filtering the nodes being searched.

As an example, suppose you have a blog and want to exclude blog comments from the search results. This is easily done with a one-line addition to the xsltsearch.xslt macro file.

Search for umbracoNaviHide and you'll find the possibleNodes variable in the search template. This is where most of the modifications you might ever want to make would be made.

The search template looks like this (XSLTsearch 2.8):

<xsl:template name="search">
    <!-- Perform the search on the appropriate nodeset and display the output -->
    <xsl:param name="items"/>
    <!-- reduce the number of nodes -->
    <xsl:variable name="possibleNodes" select="$items/descendant-or-self::node[
         string(data [@alias='umbracoNaviHide']) != '1'
         and count(attribute::id)=1 
         and (umbraco.library:IsProtected(@id, @path) = false()
         or umbraco.library:HasAccess(@id, @path) = true())
        ]"/>
    ...

 

As you can see, the search template that gets all the possible nodes that will be searched. In our example, we want to remove from the search any BlogPostComment nodes. We'll do that by checking the @nodeTypeAlias.

The modified xsltsearch.xslt file that excludes blog post comments would look like:

    <!-- reduce the number of nodes -->
    <xsl:variable name="possibleNodes" select="$items/descendant-or-self::node[
         string(data [@alias='umbracoNaviHide']) != '1'
         and @nodeTypeAlias != 'BlogPostComment'
         and count(attribute::id)=1 
         and (umbraco.library:IsProtected(@id, @path) = false()
         or umbraco.library:HasAccess(@id, @path) = true())
        ]"/>
    ...

This same technique could be used to search within certain document types (rather than to exclude certain document types as we did above). To search only within umbraco content nodes of homepage and webpage document types we would modify the possibleNodes variable like this:

    <!-- reduce the number of nodes -->
    <xsl:variable name="possibleNodes" select="$items/descendant-or-self::node[
         string(data [@alias='umbracoNaviHide']) != '1'
         and (
            @nodeTypeAlias = 'homepage'
            or @nodeTypeAlias = 'webpage'
            )
         and count(attribute::id)=1 
         and (umbraco.library:IsProtected(@id, @path) = false()
         or umbraco.library:HasAccess(@id, @path) = true())
        ]"/>
    ...

Or perhaps you have created your own document type property called  'searchHide' rather than assuming that any page that does not appear in the navigation should not appear in the search results either. If so, you would simply change the umbracoNaviHide alias to that of your property:

    <!-- reduce the number of nodes -->
    <xsl:variable name="possibleNodes" select="$items/descendant-or-self::node[
         string(data [@alias='searchHide']) != '1'
         and count(attribute::id)=1 
         and (umbraco.library:IsProtected(@id, @path) = false()
         or umbraco.library:HasAccess(@id, @path) = true())
        ]"/>
    ...

 

Though these examples are simple, more advanced modifications can be handled in the same way.

For instance, it would be a small matter to create a custom search form that included checkboxes for the areas or docTypes of your site to search (such as FAQs, News, Support, Products, etc.). You could simply add a macro parameter and pass in the nodeTypeAlias(es) according to the user's selection rather than hard-coding them as we've done here.

Many websites will not need to modify XSLTsearch because of the many built-in options it provides; but it is nice to know that it is so easy to customize XSLTsearch to meet your site's unique needs if you need to. 

15 comments for “Customizing XSLTsearch”

  1. Gravatar of bob baty-barr
    bob baty-barr says:
    oh, man... this blog is gonna be AWESOME!!!!
  2. Gravatar of Johan
    Johan says:
    Congrats and good luck with your new blog. Looking forward to read your posts.
  3. Gravatar of Dirk
    Dirk says:
    Hi Doug,

    Congrats on your blog! It has been worth waiting so long for it ;)

    Also love how you've handled the code stuff? Do you have to format the code yourself to make it span multiple lines?

    Anyway, I'm hoping to find much more valuable tips and trick of the most friendly and talented umbraco community member.

    Cheers,
    Dirk
  4. Gravatar of Thomas Höhler
    Thomas Höhler says:
    Hi Doug,

    Congratulations to your blog. I added it immediatly to my rssreader. Keep on with your great work.

    I am looking foreward to see you at codegarden.

    Cheers,
    Thomas
  5. Gravatar of Warren Buckley
    Warren Buckley says:
    Congrats Doug on getting this Blog off the ground. I look forward to reading many more posts from you.

    The examples above will be a useful point of reference for me for when I need to use XSLTSearch next.

    Warren :)
  6. Gravatar of Tim Geyssens
    Tim Geyssens says:
    Congrats on the launch Doug, looking forward the your posts.
  7. Gravatar of Kenneth Solberg
    Kenneth Solberg says:
    Congratulations Doug, this looks really good!
  8. Gravatar of Jesper Ordrup
    Jesper Ordrup says:
    Great work as always. Congratulations with your new blog.

  9. Gravatar of PeterD
    PeterD says:
    Good to see you are going to pick up blogging!
    Even better to see you are using compression :)

    Cheers,
    PeterD
  10. Gravatar of Hendy
    Hendy says:
    Excellent stuff - judging from your posts on the forum this blog will no doubt become an essential Umbraco resource :)
  11. Gravatar of Matt Perry
    Matt Perry says:
    Thanks Doug.

    Just in time for a new project I'm working on that will need this exact modification.
  12. Gravatar of Hjalmar Guerra
    Hjalmar Guerra says:
    I find it very usefull, but I have a problem. How can I display de result in another place instead of display it under the search box? Right now it's braking my layout. Thanks a lot!
  13. Gravatar of matostoolla
    matostoolla says:
    Sorry for being Off-Topic but what WP template do you use? It looks great!
  14. Gravatar of Kim Andersen
    Kim Andersen says:
    This is a great search-package Doug. And beside that the search works perfectly, it's very flexible and customizable.
    Thanks.
  15. Gravatar of Gerald Diva
    Gerald Diva says:
    Doug, is there a way to customize the XSLTSearch so that the search term must equal the word it finds? By the way great work!

    Thanks
 
powered by <XSLTsearch>

Categories

Follow Us

RSS Feed
Follow us on Twitter
Follow us on Flickr