Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mike 80 posts 101 karma points
    Feb 06, 2010 @ 10:56
    Mike
    0

    Fill META information from xsltSearch results?

    Hi,

    I do not know if this is possible within XSLT so here's the question, i hope I can make it clear...

    When using the XsltSearch package, which works great, i would like the meta information in the head section to be randomly filled with text from the search results.

    this would be the template for the search page.

    <head>
    <title><umbraco:Item runat="server" field="siteName" recursive="true"/> - <umbraco:Item runat="server" field="pageName"/></title>
    <meta name="Keywords" content="<umbraco:Item runat='server' field='metaKeywords' recursive='true'/>" />
    <meta name="Description" content="<umbraco:Item runat='server' field='metaDescription' recursive='true'/>" />
    </head>
    <body>
    <h1 class="first"><umbraco:Item runat="server" field="pageName"/></h1>
    <div>
    <umbraco:Macro runat="server" Alias="XSLTsearch"
    macroAlias="XSLTsearch"
    source="-1"
    searchFields="@nodeName,metaKeywords,metaDescription,bodyText"
    previewFields="bodyText,metaDescription"
    previewType="beginning"
    searchBoxLocation="bottom"
    resultsPerPage="5"
    previewChars="255"
    showPageRange="1"
    showOrdinals="0"
    showScores="0"
    showStats="1">
    </umbraco:Macro>
    </div>
    </body>

    When the search returns some results, I would like the Page's Title, and Meta Description to be filled with:

    title: Title of the first search result.
    Description: random text from all results..

    I believe I can mange to get the required texts in the xsltsearch.xslt file, but how would I fill the meta texts in this template... if this is at all possible.

    If this is not possible, would there be another solution to getting this done?

    thanks!

  • dandrayne 1138 posts 2262 karma points
    Feb 06, 2010 @ 16:07
    dandrayne
    0

    Interesting question. Doug would probably be the best person to get an answer from re xslt search, but I wonder if it's a great idea to have the page title as the same as the first result?  Duplicate page titles could lead to indexing issues, but this is a small point.

    Easier to achieve, and more commonly used would be using something like "Search results for X" in the page title and description.  For this you could just have a macro that renders the meta tags, grabbing querystring paramaters with umbraco.library:Request and inserting them in the tags.

    Would be interested in hearing if there is a way to achieve what you're after though.

    Dan

  • Mike 80 posts 101 karma points
    Feb 06, 2010 @ 16:39
    Mike
    0

    hmm, yes good point about the duplicate.. that is not what i would want of course.. one reason for me to implement this is that i now have duplicates...

    another option for the title might be:

    [searchword] - [title of first hit]

    or

    [searchword] - [string randomized from all result titles]

    that way duplicate titles would be avoided...

    thanks

     

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Feb 06, 2010 @ 17:43
    Morten Bock
    0

    I would make a copy of the xslt search and modify the part of the file that outputs html. This would cause the serach to be performed twice, and thus would degrade performance, if you have a lot of pages.

  • rasb 162 posts 218 karma points
    Feb 06, 2010 @ 18:09
    rasb
    0

    A thing that search engines do not like is a meta or title tag filled with random words. You should seriously avoid doing that.

    What they really like is a meta tag containing a humanly readable sentence, and best of all a unique one for each page at that.

    So I am not sure that just picking a number of words from the resulting pages is a good thing. The best bet would be to use the meta tag from the first result or even better to construct some logic that creates a meta tag based on the first result.

    You should definitely only use the first result, as this is at the top of the page and is thus perceived as the most important by the Search Engine. That way you will make a much better case to the Search Engine.

    /RasB

  • Mike 80 posts 101 karma points
    Feb 07, 2010 @ 14:45
    Mike
    0

    Apart from the "what will actually be in the tags", let's focus on the "how".

    Thus, the question is:

    Is it possible to fill the meta tags from the xsltsearch macro (or any other macro) further down the page, or should indeed the macro output the entire html structure?

     

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Feb 07, 2010 @ 14:56
    Morten Bock
    0

    The xslt macro's are rendered in the order they appear in the html, so the macro rendering your title/meta can not be an xslt macro, if you don't want to run the search twice.

    One thing that might be possible is to modify the xslt search to save the text that you want into the HttpContext items, and the have a .Net control in your head section that sets the title/meta values as late as possible, like teh prerender event or similar, using the context items you save earlier.

    Try it with a proof of concept first using a simple xslt macro that just sets a couple of values, and then see if you can output them in the header section.

  • Mike 80 posts 101 karma points
    Feb 08, 2010 @ 09:56
    Mike
    0

    Hi,

    i tried the HttpContext trick from Morten. Setting the HTTP context in a XSLT Macro works. In the head-section I can read the context variable I set in the XSLT.

    Now I only need to add this to the meta tags...

    I tried this from a .net usercontrol (in a PreRender override):

                HtmlMeta mt = new HtmlMeta();

    mt.Name = "description";
    mt.Content = "Some words listed here";
    this.Page.Header.Controls.Add(mt);

    But this gives a Object not set error on the this.Page.Header command.

    Any other option to add this?

     

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Feb 08, 2010 @ 10:05
    Dirk De Grave
    0

    Do you have a <head runat="server"> set in your template?

     

    Cheers,

    /Dirk

  • Mike 80 posts 101 karma points
    Feb 08, 2010 @ 10:38
    Mike
    0

    when I add the runat=server to the head tag, it gives the error:

    The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.

    maybe there is another option?

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Feb 08, 2010 @ 17:46
    Morten Bock
    0

    Maybe you can just use literals and just set the text property on prerender instead of adding a new control to the control set?

  • Mike 80 posts 101 karma points
    Feb 08, 2010 @ 18:09
    Mike
    0

    ah of course... using the literal control it is now working... thanks!

     

  • Mike 80 posts 101 karma points
    Feb 10, 2010 @ 17:08
    Mike
    0

    Allright, i have now a working proof of concept using a modified XsltSearch. I do not know however if this is the best method as I do not really know how to do XSLT.

    Did you know that an xsl:variable not really is a variable... (lol)....

    So here are my modifications to xsltSearch:

    <!-- display a portion of the page's text -->
    <!-- display first words of the text -->
    <xsl:if test="$previewType = 'BEGINNING' and string-length($escapedData &gt; 0)">
    <p class="xsltsearch_result_description">
    <span class="xsltsearch_description">

    <xsl:variable name="mdesc" select="umbraco.library:StripHtml(ps:surround(substring($escapedData, 1, $previewChars), $search, $before, $after))"/>
    <xsl:variable name="title_url" select="@nodeName"/>
    <xsl:value-of select="ps:fillIt($title_url, $mdesc)" />

    <xsl:value-of select="ps:surround(substring($escapedData, 1, $previewChars), $search, $before, $after)" disable-output-escaping="yes"/>
    <!-- add an elipsis if there is more text than we are showing on the search results page -->
    <xsl:if test="string-length($escapedData) &gt; $previewChars">...</xsl:if>
    </span>
    </p>
    </xsl:if>

    And added a c# function to set the HttpContext variables...

       <msxsl:script language="C#" implements-prefix="ps">
    <msxml:using namespace="System.IO" />
    <msxml:assembly name="System.Web" />
    <msxml:using namespace="System.Web" />
    <![CDATA[

    public void fillIt(string title_url, string mdesc )
    {
    try
    {
    string keyword = string.Empty;
    if(!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.QueryString["search"]))
    {
    keyword = System.Web.HttpContext.Current.Request.QueryString["search"];
    }

    // Test...
    if(System.Web.HttpContext.Current.Items["metadesc"]!=null)
    {
    System.Web.HttpContext.Current.Items["metadesc"] += mdesc;
    }else
    {
    System.Web.HttpContext.Current.Items.Add("metadesc", mdesc);
    }

    if(System.Web.HttpContext.Current.Items["metatit"]!=null)
    {
    System.Web.HttpContext.Current.Items["metatit"] += title_url;
    }else
    {
    System.Web.HttpContext.Current.Items.Add("metatit", title_url);
    }

    if(System.Web.HttpContext.Current.Items["metakeyw"]!=null)
    {
    System.Web.HttpContext.Current.Items["metakeyw"] = keyword;
    }else
    {
    System.Web.HttpContext.Current.Items.Add("metakeyw", keyword);
    }
    }
    catch (Exception)
    {
    }
    }

     

    These I can now read in the PreRender in another .net Macro...

    I also tried to create a struct and then add these to an Arraylist and put the arraylist in the httpcontext, but in the usercontrol I couldn't cast this back to the struct. I then got this error: System.Xml.Xsl.CompiledQuery.Script6+pagedata cast invalid. (pagedata was my struct).

    So if anyone knows if there is more efficient code... please let me know

    thanks!

     

     

     

     

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Feb 10, 2010 @ 17:41
    Douglas Robar
    0

    This is a good modification to XSLTsearch for your needs... nice and clean.

    I will echo others' comments that simply because you can do this it may not be wise to do it; but that's your decision.

    cheers,
    doug.

  • Mike 80 posts 101 karma points
    Feb 11, 2010 @ 07:41
    Mike
    0

    Well,

    I will only use this to set a title: "Your search for <keyword> returned <number> results..."

    But i think there might be situations where something like this, using things from a macro further down the page that is, might come in handy. Also it was a nice learning experience...

    I was thinking, Google doesn't really use a search form, does it? So these pages would not be indexed at all.

     

  • dandrayne 1138 posts 2262 karma points
    Feb 11, 2010 @ 12:40
    dandrayne
    0

    Google does indeed submit forms, and has since early 2008.  Scary stuff actually!

    Dan

Please Sign in or register to post replies

Write your reply to:

Draft