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 17, 2009 @ 15:55
    trfletch
    0

    Check whether any parent or ancestors contain a property

    Hi,

    I have an Umbraco V4 website and I have the following XSLT which checks if the parent page has a property (an image in this case) and if so it displays this property. Currently the code I have works but if I was to add a sub page then I would have modify the XSLT to make it check the parent of the parent of the parent page to see if it has that property. I was thinking there must be a way to just check in any of the ancestor pages contains the property and if so then show it. Can anyone help me with how I might need to modify my code?

    <xsl:choose>
    <xsl:when test="$currentPage/data [@alias = 'middlebannerimage'] !='' or $currentPage/parent::node/data [@alias = 'middlebannerimage'] !=''or $currentPage/parent::node/parent::node/data [@alias = 'middlebannerimage'] !=''">
    <xsl:if test="$currentPage/data [@alias = 'middlebannerimage'] !=''">
    <img alt="">
        <xsl:attribute name="src">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias='middlebannerimage'], 'false')/data [@alias='umbracoFile']" />
        </xsl:attribute>
        <xsl:attribute name="width">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias='middlebannerimage'], 'false')/data [@alias='umbracoWidth']" />
        </xsl:attribute>
        <xsl:attribute name="height">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias='middlebannerimage'], 'false')/data [@alias='umbracoHeight']" />
        </xsl:attribute>
        </img>
    </xsl:if>
    <xsl:if test="$currentPage/parent::node/data  [@alias = 'middlebannerimage'] !=''">
    <img alt="">
        <xsl:attribute name="src">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/parent::node/data [@alias='middlebannerimage'], 'false')/data [@alias='umbracoFile']" />
        </xsl:attribute>
        <xsl:attribute name="width">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/parent::node/data [@alias='middlebannerimage'], 'false')/data [@alias='umbracoWidth']" />
        </xsl:attribute>
        <xsl:attribute name="height">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/parent::node/data [@alias='middlebannerimage'], 'false')/data [@alias='umbracoHeight']" />
        </xsl:attribute>
        </img>
    </xsl:if>
    <xsl:if test="$currentPage/parent::node/parent::node/data  [@alias = 'middlebannerimage'] !=''">
    <img alt="">
        <xsl:attribute name="src">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/parent::node/parent::node/data [@alias='middlebannerimage'], 'false')/data [@alias='umbracoFile']" />
        </xsl:attribute>
        <xsl:attribute name="width">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/parent::node/parent::node/data [@alias='middlebannerimage'], 'false')/data [@alias='umbracoWidth']" />
        </xsl:attribute>
        <xsl:attribute name="height">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/parent::node/parent::node/data [@alias='middlebannerimage'], 'false')/data [@alias='umbracoHeight']" />
        </xsl:attribute>
        </img>
    </xsl:if>
    </xsl:when>
    <xsl:otherwise>
    Do something else!!!!
    </xsl:otherwise>
    </xsl:choose>
  • dandrayne 1138 posts 2262 karma points
    Dec 17, 2009 @ 16:00
    dandrayne
    0

    Hows about instead of

    parent::node

    using

    anscestor::node

    Dan

  • trfletch 598 posts 604 karma points
    Dec 17, 2009 @ 16:11
    trfletch
    0

    Hi Dan,

    I tried that with the correct spelling :-)

    But it did not work :-(

    I got an XSLT error on the child pages.

  • dandrayne 1138 posts 2262 karma points
    Dec 17, 2009 @ 17:55
    dandrayne
    0

    Boo typos.

    Hows about this instead of all the chooses

    <xsl:variable name="imageNode" select="$currentPage/ancestor-or-self::node/data [@alias='middlebannerimage' != '']" />               
    <xsl:variable name="getImage" select="umbraco.library:GetMedia($imageNode, 'false')" />

    <img alt="">
    <xsl:attribute name="src">
    <xsl:value-of select="$getImage/data [@alias='umbracoFile']" />
    </xsl:attribute>
    <xsl:attribute name="width">
    <xsl:value-of select="$getImage/data [@alias='umbracoWidth']" />
    </xsl:attribute>
    <xsl:attribute name="height">
    <xsl:value-of select="$getImage/data [@alias='umbracoHeight']" />
    </xsl:attribute>
    </img>

    I must admit to not being sure it will work, and not having enough time to test it

    Dan

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

    I do need the when statement in there though because the XSLT is supposed to display the image "middlebannerimage" if there is one specified in the current page or any of its ancestors but if not it will show a flash file that I am calling in "otherwise" statement (I left that part out of the XSLT I put in my original XSLT to try and reduce the size of it). The full XSLT I have is as follows:

    <?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:cc2="urn:CAdRotator: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"
     exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">

    <xsl:output method="html" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>
    <xsl:template match="/">
    <xsl:choose>
    <xsl:when test="$currentPage/data [@alias = 'middlebannerimage'] !='' or $currentPage/parent::node/data [@alias = 'middlebannerimage'] !=''or $currentPage/parent::node/parent::node/data [@alias = 'middlebannerimage'] !=''">
    <xsl:if test="$currentPage/data [@alias = 'middlebannerimage'] !=''">
    <img alt="">
        <xsl:attribute name="src">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias='middlebannerimage'], 'false')/data [@alias='umbracoFile']" />
        </xsl:attribute>
        <xsl:attribute name="width">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias='middlebannerimage'], 'false')/data [@alias='umbracoWidth']" />
        </xsl:attribute>
        <xsl:attribute name="height">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias='middlebannerimage'], 'false')/data [@alias='umbracoHeight']" />
        </xsl:attribute>
        </img>
    </xsl:if>
    <xsl:if test="$currentPage/parent::node/data  [@alias = 'middlebannerimage'] !=''">
    <img alt="">
        <xsl:attribute name="src">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/parent::node/data [@alias='middlebannerimage'], 'false')/data [@alias='umbracoFile']" />
        </xsl:attribute>
        <xsl:attribute name="width">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/parent::node/data [@alias='middlebannerimage'], 'false')/data [@alias='umbracoWidth']" />
        </xsl:attribute>
        <xsl:attribute name="height">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/parent::node/data [@alias='middlebannerimage'], 'false')/data [@alias='umbracoHeight']" />
        </xsl:attribute>
        </img>
    </xsl:if>
    <xsl:if test="$currentPage/parent::node/parent::node/data  [@alias = 'middlebannerimage'] !=''">
    <img alt="">
        <xsl:attribute name="src">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/parent::node/parent::node/data [@alias='middlebannerimage'], 'false')/data [@alias='umbracoFile']" />
        </xsl:attribute>
        <xsl:attribute name="width">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/parent::node/parent::node/data [@alias='middlebannerimage'], 'false')/data [@alias='umbracoWidth']" />
        </xsl:attribute>
        <xsl:attribute name="height">
            <xsl:value-of select="umbraco.library:GetMedia($currentPage/parent::node/parent::node/data [@alias='middlebannerimage'], 'false')/data [@alias='umbracoHeight']" />
        </xsl:attribute>
        </img>
    </xsl:if>
    </xsl:when>
    <xsl:otherwise>
    <div id="flashbanner1">
    <img src="/images/noflashfallback.jpg" />
    </div>
    <script type="text/javascript">
    <![CDATA[
    // Rest of JavaScript code
    var s1 = new SWFObject("/images/mainflash.swf","flashbanner1","794","154","9");
    s1.addParam("allowfullscreen","true");
    s1.addParam("wmode","transparent");
    s1.addParam("allowscriptaccess","always");
    s1.write("flashbanner1");
    ]]>
    </script>
    <div id="goldline">
    <!-- -->
    </div>
    <div id="blueline">
    <!-- -->
    </div>
    </xsl:otherwise>
    </xsl:choose>

    </xsl:template>
    </xsl:stylesheet>
  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Dec 17, 2009 @ 23:38
    Chriztian Steinmeier
    0

    Hi,

    You ask for help modifying your code - here's a couple of pointers:

    1. Get rid of all the repetition (you'll hate it a month from now when you need to change something) - create a separate template for the stuff you do multiple times  
    2. Cache the result from umbraco.library:GetMedia() in a variable and use that to access the data
    Applying these to your code, I've shrunk the XSLT to this:
    <?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:cc2="urn:CAdRotator: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"
    exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">
    <xsl:output method="html" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>
    <xsl:template match="/">
    <!-- Grab the 'middlebannerimage' from currentPage or an ancestor -->
    <xsl:variable name="middleBannerImage" select="$currentPage/ancestor-or-self::node[data[@alias = 'middlebannerimage']]" />
    <xsl:choose>
    <!-- If there's an ID in there... -->
    <xsl:when test="number($middleBannerImage)">
    <!-- Call the 'img' template with the node returned from GetMedia() -->
    <xsl:call-template name="img">
    <xsl:with-param name="mediaNode" select="umbraco.library:GetMedia($middleBannerImage, 'false')" />
    </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
    <div id="flashbanner1">
    <img src="/images/noflashfallback.jpg" />
    </div>
    <script type="text/javascript"><![CDATA[
    // Rest of JavaScript code
    var s1 = new SWFObject("/images/mainflash.swf","flashbanner1","794","154","9");
    s1.addParam("allowfullscreen","true");
    s1.addParam("wmode","transparent");
    s1.addParam("allowscriptaccess","always");
    s1.write("flashbanner1");
    ]]></script>
    <div id="goldline">
    <xsl:comment /><!-- Use this to generate a comment instead of getting <div id="goldline" />  -->
    </div>
    <div id="blueline">
    <xsl:comment /><!-- Ditto -->
    </div>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:template>
    <!-- Named template for generating an <img> tag from a media node -->
    <xsl:template name="img">
    <xsl:param name="mediaNode" />
    <xsl:if test="$mediaNode"><!-- Just to be really safe -->
    <img alt=""
    src="{$mediaNode/data[@alias = 'umbracoFile']}"
    width="{$mediaNode/data[@alias = 'umbracoWidth']}"
    height="{$mediaNode/data[@alias = 'umbracoHeight']}"
    />
    </xsl:if>
    </xsl:template>
    </xsl:stylesheet>
    /Chriztian

    PS: Don't know if the two empty <div>s after the <script> are supposed to be empty (could be stripped code to avoid cluttering), but if they are indeed intended to be empty, you'll have to use <xsl:comment> instead of a regular XML comment, because regular comments in the stylesheet are just that, and won't make it through the tunnel to the output...  

Please Sign in or register to post replies

Write your reply to:

Draft