Copied to clipboard

Flag this post as spam?

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


  • trfletch 598 posts 604 karma points
    Dec 18, 2009 @ 17:22
    trfletch
    0

    Search engine optimisation page title

    Hi,

    I have the following XSLT in my Umbraco V4 website that allows me to put in a search engine optimised page title if I enter one but shows the umbraco page title if I don't enter one, this works fine if the page has the property seopagetitle but if I have a page that doesn't have that property or a page that hasn't been published since I added the seopagetitle to that document type then it doesn't show the Umbraco page title which is what I want. Basically I need the XSLT to check if the node has got nothing specified for the seopagetitle or doesn't even have the seopagetitle property. Can someone tell me how or if I can do this, this is what I have so far:

    <?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"
     exclude-result-prefixes="msxml umbraco.library">

    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>
    <xsl:template match="/">
    <!-- The fun starts here -->
    <xsl:if test="$currentPage/data [@alias='seopagetitle'] != ''">
    <xsl:value-of select="$currentPage/data [@alias = 'seopagetitle']"/>
    </xsl:if>
    <xsl:if test="$currentPage/data [@alias='seopagetitle'] = ''">
    <xsl:value-of select="$currentPage/@nodeName"/>
    </xsl:if>

    </xsl:template>
    </xsl:stylesheet>
  • Chad Rosenthal 272 posts 474 karma points
    Dec 18, 2009 @ 18:04
    Chad Rosenthal
    0

    I do something similar. I have a the following fields in my generic properties doctype: Page Title and Remove Sitename from Page Title. My home page then has a field name sitename. Basically it uses either Page Title (if it exists) otherwise it uses Node Name. If the Remove Sitename from Page Title is not checked, then it will add the sitename afterwards. Otherwise, it will just display the page title.

    Hope this makes sense.

    <?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"
    exclude-result-prefixes="msxml 
    umbraco.library">
    
      <xsl:output method="xml" omit-xml-declaration="yes"/>
    
      <xsl:param name="currentPage"/>
    
      <xsl:variable name="minLevel" select="1"/>
    
      <xsl:template match="/">
        <xsl:choose>
          <xsl:when test="$currentPage/data [@alias='pageTitle'] != ''">
            <xsl:value-of select="$currentPage/data [@alias='pageTitle']" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$currentPage/@nodeName"/>
          </xsl:otherwise>
        </xsl:choose>
        <xsl:if test="$currentPage/data [@alias='removeSitename'] != 1">
          <xsl:text> - </xsl:text>
          <xsl:value-of select="$currentPage/ancestor-or-self::node [@level=1]/data [@alias='siteName']"/>
        </xsl:if>
      </xsl:template>
    </xsl:stylesheet> 
  • trfletch 598 posts 604 karma points
    Dec 18, 2009 @ 18:27
    trfletch
    0

    Thanks for the reply Chad the problem I have is with the pages that do not have the extra property field "seopagetitle" or ones that "seopagetitle" has recently been added to their doctype but the page has not been republished since (hence in the database they still don't have a property called seopagetitle).

    Because these pages don't have seopagetitle equally nothing (they just dont have seopagetitle full stop) they do not instead use the Umbraco page title.

    Therefore I need a way to check if the pages have "seopagetitle" equally nothing OR they do not have the property "seopagetitle" what so ever.

  • Chad Rosenthal 272 posts 474 karma points
    Dec 18, 2009 @ 18:39
    Chad Rosenthal
    0

    ahh....sorry about that. So if I'm clear, the XSLT is not working if SEOpagetitle doesn't exist at all on the page. 

    If this is the case, then your if if statement won't work because they're checking the same field that fails.

    Did you try a choose statement?

    <xsl:choose>
      <xsl:when test="$currentPage/data [@alias='seopagetitle'] != ''">
    <xsl:value-of select="$currentPage/data [@alias = 'seopagetitle']"/>
    </xsl:when>
      <xsl:else>
    <xsl:value-of select="$currentPage/@nodeName"/>
    </xsl:else>
    </xsl:choose>

    Didn't test, but this should work....(famous last words).

     

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Dec 18, 2009 @ 20:19
    Douglas Robar
    0

    If your page might not have the property you're checking against you just need to handle that. The easy way is to use the string() function, which will return an empty string if the property has no value or if the property doesn't even exist. The ultimate error handler :)

    I'd use the following in xslt...

     <xsl:variable name="title">
    <xsl:choose>

         
    <xsl:when test="string($currentPage/data [@alias='seopagetitle']) != ''">
           
    <xsl:value-of select="$currentPage/data [@alias='seopagetitle']" />
         
    </xsl:when>
         
    <xsl:otherwise>
           
    <xsl:value-of select="$currentPage/@nodeName"/>
         
    </xsl:otherwise>
       
    </xsl:choose>
    </xsl:variable>

    <xsl:value-of select="$title" />

     

    But if this is all the macro is doing you could just as easily handle it with the "Insert umbraco page field" button in the template editor's toolbar. You would simply select the following parameters:

    Choose field = seopagetitle

    Alternative field = @pageName

    This will create the following line in your template:

    <umbraco:Item field="seopagetitle" useIfEmpty="pageName" runat="server"></umbraco:Item>

    Hope this helps.

    cheers,
    doug.

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Dec 21, 2009 @ 00:31
    Stefan Kip
    0

    @Douglas

    I wanted to use the umbraco Item's useIfEmpty property before for the page title, but found out it doesn't really work... I'm not able to explain more about the problem I had, because I forgot. But I think the problem was, that if the pageTitle property was empty (''), it still uses the pageTitle property instead of the pageName...

  • trfletch 598 posts 604 karma points
    Jan 11, 2010 @ 12:27
    trfletch
    0

    Hi,

    Sorry for not responding sooner on this, been busy with Christmas etc, I managed to get it to work with Chad's suggestion but I changed <xsl:else> to <xsl:otherwise> as shown below, thank you all for your help on this problem and happy new year to you all.

    <xsl:with-param name="parent" select="$currentPage/ancestor-or-self::node [@level=1]"/>  
    <xsl:choose>
            <xsl:when test="$currentPage/data [@alias='seopagetitle'] != ''">
               <xsl:value-of select="$currentPage/data [@alias = 'seopagetitle']"/>
            </xsl:when>
          
    <xsl:otherwise>
                   <xsl:value-of select="$currentPage/@nodeName"/
    </xsl:otherwise>
    </xsl:choose>
Please Sign in or register to post replies

Write your reply to:

Draft