Copied to clipboard

Flag this post as spam?

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


  • Daniel 10 posts 30 karma points
    Nov 08, 2010 @ 05:40
    Daniel
    0

    Search results linking to parent page

    Hi,

    Umbraco 4.5.1. XSLTsearch 2.8.1.

    First thanks for make this great package available to us.

    I have site where I have a content item such as a Q&A page set up with child items such as each of the Q/A pairs. The page itself has very little content - it's all in the child items.

    When I use XSLT Search it reports matches on the child items. What I require is that the link in the search results goes to the parent item. It's fine for the description to be the child node.

    A condition such as "if DocType=QAPair" needs to be defined for these types of results then the parent node identified then the URL changed.

    Can you please assist in telling me how to change your source code to accomplish this?

    My XSLT is terrible and I am new to Umbraco. I hope I have set up my pages in the right way (i.e. using child items).

    Regards

    -Daniel.

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Nov 08, 2010 @ 16:49
    Douglas Robar
    0

    Yes, I do this kind of thing on websites all the time, so you're not doing anything unusual.

    What I do in these cases is build on the best practice of ensuring that every page (or bit of content) is responsible for its own presentation. In other words, for your child items I would set a template that redirects to the parent page. That way, no matter how someone gets to that child node it will always redirect to the parent, which knows how to display it properly and in context.

    That means you don't need to modify XSLTsearch at all. Nor your sitemap. Nor any other macros that might list the child page.

    Hope that helps.

    cheers,
    doug.

  • Daniel 10 posts 30 karma points
    Nov 10, 2010 @ 23:46
    Daniel
    0

    Thanks Doug. This works nicely. I used the umbracoRedirect feature. Ideally I would like a couple of my doc types to automatically redirect to their parent so I don't need to set the redirect manually for all of these nodes. Is this possible? I had a quick look at a couple of the redirect packages but they didn't mention this functionality.

    Regards

    -Daniel.

  • Rich Green 2246 posts 4008 karma points
    Nov 10, 2010 @ 23:55
    Rich Green
    0

    Hi Daniel,

    You can create a new template for your child doc type, name it something like 'RedirectToParent' then on this template add a macro where the xslt redirects to the parent node (you will need a little xslt extension for this but I don't have the code here).

    Your child nodes will then all call this macro as they will default to this template. 

    Hope that makes sense.

    Rich

     

     

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Nov 11, 2010 @ 12:28
    Douglas Robar
    0

    The idea is to set the 'redirect to parent' template as the default template for QAPair document type in your example. Any document type that should default to displaying its parent instead of itself would work this way. Then editors won't need to select a template or do anything special... it will just work.

     

    Here's how I've done the 'redirect to parent' option in the past. This uses a bit of c# in an xslt macro but you could (and probably should) use a .net macro or an xslt extension instead.

     

    TEMPLATE

    <%@ Master Language="C#" MasterPageFile="/umbraco/masterpages/default.master" AutoEventWireup="true" %>

    <asp:Content ContentPlaceHolderID="ContentPlaceHolderDefault" runat="server">
      <umbraco:Macro Alias="RedirecttoParent" runat="server"></umbraco:Macro>
    </asp:Content>

     

    XSLT MACRO

    <xsl:param name="currentPage"/>
    <xsl:variable name="parentId" select="$currentPage/@parentID "/>

    <!-- =========================================================== -->

    <xsl:template match="/">
      <xsl:value-of select="ps:redirect(umbraco.library:NiceUrl($parentId))" />
    </xsl:template>

    <!-- =========================================================== -->

    <msxml:script language="CSharp" implements-prefix="ps">
    <msxml:assembly name="System.Web" />
    <msxml:using namespace="System.Web" />
     
    <![CDATA[
    public void redirect(String url) {
      HttpContext.Current.Response.Status = "301 Moved Permanently";
      HttpContext.Current.Response.AddHeader("Location", url);
    }
    ]]>

    </msxml:script>

    cheers,
    doug.

  • Daniel 10 posts 30 karma points
    Nov 16, 2010 @ 00:18
    Daniel
    0

    Thanks Doug.

    I'm getting:

    System.Xml.Xsl.XslLoadException: Prefix 'ps' is not defined. An error occurred at C:\Users\Public\Documents\wwwroot\cms\v4.5.1\build\xslt\634254993016626971_temp.xslt(18,3). 
    at System.Xml.Xsl.XslCompiledTransform.LoadInternal(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver) 
    at umbraco.presentation.webservices.codeEditorSave.SaveXslt(String fileName, String oldName, String fileContents, Boolean ignoreDebugging)

    So do I need something like this in the header?:

    xmlns:ps="???"

    -Daniel.

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Nov 16, 2010 @ 08:29
    Douglas Robar
    0

    Sorry about that, Daniel, I didn't put two and two together when I posted my example from another project and you ended up with two "ps" extensions - one for XSLTsearch and one for the redirect script block. You can't have two different things with the same name.

    All you need to do is pick a new name for the redirection code (how about 'Daniel' :) ) and update the reference in three places:

    <msxml:script language="CSharp" implements-prefix="Daniel">
      <xsl:value-of select="Daniel:redirect(umbraco.library:NiceUrl($parentId))" />

    And at the top of the redirect xslt file you'll need to reference this new name. I don't think we mentioned that before. You need to declare the 'Daniel' block like so. Just add your 'Daniel' entry to the list of xmlns's and also to the exclude-result-prefixes list. It will look something like this:

    <xsl:stylesheet
    .
    .          
    xmlns:umbraco.library="urn:umbraco.library"
    xmlns:Daniel="urn:Daniel:xslt"
        

    exclude-result-prefixes
    ="umbraco.library . . . Daniel">

    cheers,
    doug.

  • Daniel 10 posts 30 karma points
    Nov 16, 2010 @ 10:08
    Daniel
    0

    Thanks for continuing to help. This now compiles but when I save Umbraco tells me "XSLT file could not be saved".

    When I removed <xsl:value-of select="Daniel:redirect(umbraco.library:NiceUrl($parentId))" /> it could be saved.

    Looking at http://forum.umbraco.org/yaf_postst8720_Xslt-file-could-not-be-saved-anymore.aspx, I removed the redirects that I had set up but that didn't help. I then removed the umbracoRedirect  from doc type and republished the site to be sure. That didn't help.

    The 'skip testing' checkbox does of course work.

    And once I set up the template, the redirection macro worked too. So I'm not sure why the macro didn't save, but the desired redirection result has been achieved.

    Thank you Doug once again for you help.

    Regards

    -Daniel.

     

     

     

     

  • Rich Green 2246 posts 4008 karma points
    Nov 16, 2010 @ 10:10
    Rich Green
    0

    Daniel,

    Add this test around your line of code to this and it will save fine

    <xsl:if test="$parentId !=''">
            <xsl:value-of select="Daniel:redirect(umbraco.library:NiceUrl($parentId))" />
    </xsl:if>

    Rich

  • Daniel 10 posts 30 karma points
    Nov 16, 2010 @ 10:19
    Daniel
    0

    Thanks Rich, but it didn't work. Here is my code:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet 
      version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:msxml="urn:schemas-microsoft-com:xslt"
      xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
      xmlns:turtle="urn:turtle:xslt"
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets turtle">

    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>
    <xsl:variable name="parentId" select="$currentPage/@parentID "/>


    <xsl:template match="/">
      <xsl:if test="$parentId !=''">
        <xsl:value-of select="turtle:redirect(umbraco.library:NiceUrl($parentId))" />
      </xsl:if>
    </xsl:template>

       
    <msxml:script language="CSharp" implements-prefix="turtle">
    <msxml:assembly name="System.Web" />
    <msxml:using namespace="System.Web" />
    <![CDATA[
    public void redirect(String url) {
      HttpContext.Current.Response.Status = "301 Moved Permanently";
      HttpContext.Current.Response.AddHeader("Location", url);
    }
    ]]>
    </msxml:script>
        
    </xsl:stylesheet>

    Note that I have changed the Daniel namespace to turtle.

    Regards

    -Daniel.

  • Rich Green 2246 posts 4008 karma points
    Nov 16, 2010 @ 10:29
    Rich Green
    0

    Hi Daniel,

    Hmm, doesn't save for me either actually.

    Hopefully someone will reply with the reason, otherwise just tick the box and like you say it'll work.

    Rich

  • Rich Green 2246 posts 4008 karma points
    Nov 16, 2010 @ 10:32
    Rich Green
    0

    I know you've only just got this working but it might be worth reading Dougs great post on moving the extension into the app code directory http://blog.percipientstudios.com/2010/11/12/make-an-app_code-xslt-extension-for-umbraco.aspx#

    Depending on your set up it may not be a problem but well worth a read.

    Rich

Please Sign in or register to post replies

Write your reply to:

Draft