Copied to clipboard

Flag this post as spam?

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


  • syn-rg 282 posts 425 karma points
    Sep 07, 2012 @ 09:20
    syn-rg
    0

    Redirect to external URL

    I've added a textstring property "externalURL" to my Doctype. I want to enter in the external URL in the textstring and for that link to redirect to the external URL. However the following isn't working:

    <a>
                 <xsl:attribute name="href">
                   <xsl:choose>
                     <xsl:when test="string(externalUrl) != ''">
                       <xsl:value-of select="externalUrl" />
                     </xsl:when>
                     <xsl:otherwise>
                       <xsl:value-of select="umbraco.library:NiceUrl(@id)" />
                     </xsl:otherwise>
                   </xsl:choose>
                 </xsl:attribute>
                 <xsl:if test="descendant-or-self::*[@id = $currentPage/@id]">
                   <xsl:attribute name="class">current</xsl:attribute>
                 </xsl:if>
                 <xsl:value-of select="@nodeName" />
               </a>

    Any suggestions?

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Sep 07, 2012 @ 09:25
    Chriztian Steinmeier
    0

    Hi JV,

    How does it not work?

    What is rendered in the href attribute?

    /Chriztian

  • Drew 165 posts 340 karma points
    Sep 07, 2012 @ 09:29
    Drew
    0

    If you do a:

    <xsl:copy-of selct="$currentPage"/>

    on your page and view the source, can you find the "externalURL" property and does it have a value?
    (first step, check it's definitely there!)

     

    Cheers,

    Drew 

  • syn-rg 282 posts 425 karma points
    Sep 10, 2012 @ 02:36
    syn-rg
    0

    The "Current career opportunites" page should be redirecting to the "externalURL" however the result is as follows:

    <div class="subnav"><a href="/careers.aspx">Careers</a>
      <ul>
        <li class="current"><span><a href="/careers/working-with-us.aspx">Working with us</a></span></li>
        <li class=""><a href="/careers/current-career-opportunities.aspx">Current career opportunities</a></li>
      </ul>
    </div> 

    Here's my entire XSLT

    <?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="/">
    <xsl:variable name="level1" select="$currentPage/ancestor-or-self::* [@level = 2 and string(umbracoNaviHide) != '1' and string(hideFromSideNavigation) != '1'][not(self::OverviewSection)]" />  
    <xsl:variable name="level" select="2"/>

    <!-- The fun starts here -->
    <div class="subnav">
      <!-- LEVEL 1 -->
      <xsl:if test="$level1 != ''">
        <href="{umbraco.library:NiceUrl($level1/@id)}">
          <xsl:if test="$level1/@id = $currentPage/@id">
            <xsl:attribute name="class">current</xsl:attribute>
          </xsl:if>
          <xsl:value-of select="$level1/@nodeName"/>
        </a>
      </xsl:if>
      <!-- LEVEL 2 -->  
      <ul>
        <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1' and string(@template) != '0'][not(self::JobApplicationPage)]">
          <xsl:element name="li">
            <xsl:attribute name="class">
              <xsl:if test="$currentPage/ancestor-or-self::*[@isDoc]/@id = current()/@id">
                <xsl:text>current</xsl:text>
              </xsl:if>
            </xsl:attribute>
            <a>
            <xsl:attribute name="href">
              <xsl:choose>
                <xsl:when test="string(externalUrl) != ''">
                  <xsl:value-of select="externalUrl" />
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="umbraco.library:NiceUrl(@id)" />
                </xsl:otherwise>
              </xsl:choose>
            </xsl:attribute>
            <xsl:value-of select="@nodeName" />
          </a
            <!-- LEVEL 3 if any more nodes are Visible -->
            <xsl:if test="*[@isDoc][not(umbracoNaviHide = 1)][not(self::Chart)][not(self::NewsItem)][not(self::EmployeeTestimonial)][not(self::JobDescription)][not(self::PreviousCareerOpportunites)]">
              <ul>
                <xsl:for-each select="./* [@isDoc and string(umbracoNaviHide) != '1' and string(@template) != '0']">
                  <xsl:element name="li">
                    <xsl:attribute name="class">
                      <xsl:if test="$currentPage/ancestor-or-self::*[@isDoc]/@id = current()/@id">
                        <xsl:text>current</xsl:text>
                      </xsl:if>
                    </xsl:attribute>
                    
                    <a>
                      <xsl:attribute name="href">
                        <xsl:choose>
                          <xsl:when test="string(externalUrl) != ''">
                            <xsl:value-of select="externalUrl" />
                          </xsl:when>
                          <xsl:otherwise>
                            <xsl:value-of select="umbraco.library:NiceUrl(@id)" />
                          </xsl:otherwise>
                        </xsl:choose>
                      </xsl:attribute>
                      <xsl:value-of select="@nodeName" />
                    </a
                    
                    <!-- LEVEL 4 if any more nodes are Visible -->
                    <xsl:if test="*[@isDoc][not(umbracoNaviHide = 1)][not(self::Chart)][not(self::NewsItem)][not(self::EmployeeTestimonial)][not(self::JobDescription)][not(self::PreviousCareerOpportunites)]">
                      <ul>
                        <xsl:for-each select="./* [@isDoc and string(umbracoNaviHide) != '1' and string(@template) != '0']">
                          <xsl:element name="li">
                            <xsl:attribute name="class">
                              <xsl:if test="$currentPage/ancestor-or-self::*[@isDoc]/@id = current()/@id">
                                <xsl:text>current</xsl:text>
                              </xsl:if>
                            </xsl:attribute>
                            <!--<a href="{umbraco.library:NiceUrl(@id)}">
                              <xsl:value-of select="@nodeName" />
                            </a>-->
                            
                            <a>
                              <xsl:attribute name="href">
                                <xsl:choose>
                                  <xsl:when test="string(externalUrl) != ''">
                                    <xsl:value-of select="externalUrl" />
                                  </xsl:when>
                                  <xsl:otherwise>
                                    <xsl:value-of select="umbraco.library:NiceUrl(@id)" />
                                  </xsl:otherwise>
                                </xsl:choose>
                              </xsl:attribute>
                              <xsl:value-of select="@nodeName" />
                            </a
                            
                          </xsl:element>
                        </xsl:for-each>
                      </ul>
                    </xsl:if>
                  </xsl:element>
                </xsl:for-each>
              </ul>
            </xsl:if>
          </xsl:element>
        </xsl:for-each>
      </ul>
    </div>
    </xsl:template>

    </xsl:stylesheet>

     

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Sep 10, 2012 @ 09:16
    Chriztian Steinmeier
    1

    Hi JV,

    My top guesses as to what is wrong:

    1. The property is not called externalUrl (XML is case sensitive - maybe it's externalURL?)

    2. The property has (for some reason) not been published for the node in question (unlikely, but I've seen it happen)

    3.  The property contains one or more spaces/tabs - use normalize-space(externalUrl) instead of externalUrl != '' to make sure that's not the case

     

    For navigations and similar structures you can really benefit from XSLT's template approach, e.g. - the following is another way to do what you're doing, but instead of repeating yourself at every level, there's a single template responsible for the link output:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet
           
    version="1.0"
           
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
           
    xmlns:umb="urn:umbraco.library"
           
    exclude-result-prefixes="umb"
    >

           
    <xsl:outputmethod="xml"indent="yes"omit-xml-declaration="yes"/>

           
    <xsl:paramname="currentPage"/>

           
    <xsl:variablename="minLevel"select="2"/>
           
    <xsl:variablename="maxLevel"select="10"/>
           
           
    <xsl:variablename="navRoot"select="$currentPage/ancestor-or-self::*[@level = $minLevel][not(self::OverviewSection)][not(umbracoNaviHide = 1 or hideFromSiteNavigation = 1)]"/>

           
    <!-- These are the Document Types (aliases) to exclude from navigation -->
           
    <xsl:variablename="excludeDocTypes"select="'Chart | NewsItem | EmployeeTestimonial | JobDescription | PreviousCareerOpportunites'"/>
           
           
    <xsl:templatematch="/">
                   
    <divclass="subnav">
                           
    <!-- Top level -->
                           
    <xsl:apply-templatesselect="$navRoot"mode="link"/>

                           
    <!-- Sub levels -->
                           
    <ul>
                                   
    <xsl:apply-templatesselect="$navRoot/*[@isDoc][not(umbracoNaviHide = 1)]"mode="link"/>
                           
    </ul>
                   
    </div>
           
    </xsl:template>

           
    <!-- Template for all links -->
           
    <xsl:templatematch="*[@isDoc]"mode="link">
                   
    <!-- Grab any subpages -->
                   
    <xsl:variablename="subPages"select="*[@isDoc][not(umbracoNaviHide = 1)][not(contains($excludeDocTypes, name()))][not(@template = 0)]"/>
                   
                   
    <ahref="{umb:NiceUrl(@id)}">
                           
    <!-- Set CSS class if necessary -->
                           
    <xsl:iftest="@id = $currentPage/@id"><xsl:attributename="class">current</xsl:attribute></xsl:if>
                           
    <!-- Override href attribute if an external URL was set -->
                           
    <xsl:iftest="normalize-space(externalUrl)"><xsl:attributename="href"><xsl:value-ofselect="externalUrl"/></xsl:attribute></xsl:if>
                           
                           
    <xsl:value-ofselect="@nodeName"/>
                   
    </a>

                   
    <!-- Any more levels to show? -->
                   
    <xsl:iftest="$subPages and @level &lt;= $maxLevel">
                           
    <ul>
                                   
    <xsl:apply-templatesselect="$subPages"mode="link"/>
                           
    </ul>
                   
    </xsl:if>
           
    </xsl:template>

    </xsl:stylesheet>

    /Chriztian

  • syn-rg 282 posts 425 karma points
    Sep 11, 2012 @ 03:36
    syn-rg
    0

    Thanks Chriztian, it was the case sensitive issue! I needed to change it to "externalURL".

    Cheers!

Please Sign in or register to post replies

Write your reply to:

Draft