Copied to clipboard

Flag this post as spam?

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


  • Brian Olsen 143 posts 424 karma points
    Aug 09, 2013 @ 11:02
    Brian Olsen
    0

    Xslt if empty then go to parent

    umbraco 4.11.5

    here is my Xslt where I make a check on "$currentPage/testBackgroundBillede/widgets/widget/umbracofile" is empty. 

    if it is not empty then it displays the images. 

    but if it is empty. 

    I would like to check on the parent if it has images and if it has then it displays the images

     

    from umbraco.config

    empty

            <testBackgroundBillede>
              <widgets>
                <widget isCollapsed="false">
                  <umbracofile />
                  <billedetekst />
                </widget>
              </widgets>
            </testBackgroundBillede>

    the two images in

        <testBackgroundBillede>
          <widgets>
            <widget isCollapsed="false">
              <umbracofile>1096</umbracofile>
              <billedetekst>test</billedetekst>
            </widget>
          </widgets>         <widget isCollapsed="false">           <umbracofile>1096</umbracofile>           <billedetekst>test</billedetekst>         </widget>
          </widgets>     </testBackgroundBillede>

     

    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" 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="xml" omit-xml-declaration="yes"/>
     
    <xsl:param name="currentPage"/>
     
    <xsl:template match="/">
     
    <xsl:variable name="Billede" select="$currentPage/testBackgroundBillede/widgets/widget/umbracofile"/>
     
    <xsl:choose>
    <xsl:when test="$Billede !=''">
     
    <xsl:for-each select="$currentPage/testBackgroundBillede/widgets/widget">
     
    <xsl:variable name="backgroundbillede" select="umbraco.library:GetMedia(.//umbracofile, false)"/>
     
    <xsl:variable name="backgroundbilledetext" select=".//billedetekst"/>
     
    <img src="{$backgroundbillede/umbracoFile}" alt="{$backgroundbilledetext}" class="bgM"/>
     
    </xsl:for-each>
     
    </xsl:when>
     
    <xsl:otherwise>
     /* How do I check whether parent have some images */
    </xsl:otherwise>
    </xsl:choose>
    </xsl:template>
    </xsl:stylesheet>

     

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 09, 2013 @ 11:34
    Chriztian Steinmeier
    100

    Hi Brian,

    You should be able to select the image(s) recursively and then just iterate over those - something like this:

    <xsl:template match="/">
        <!-- Recursively grab the widget nodes that have a file assigned -->
        <xsl:variable name="Billede" select="$currentPage/ancestor-or-self::*[normalize-space(testBackgroundBillede//umbracofile)][1]/testBackgroundBillede/widgets/widget[normalize-space(umbracofile)]" />
    
        <xsl:for-each select="$Billede">
            <!-- In here, you're inside a <widget> element -->
            <xsl:variable name="backgroundbillede" select="umbraco.library:GetMedia(umbracofile, false())" />
            <xsl:variable name="backgroundbilledetext" select="billedetekst" />
            <img src="{$backgroundbillede/umbracoFile}" alt="{$backgroundbilledetext}" class="bgM" />
        </xsl:for-each>
    </xsl:template>
    

    /Chriztian

  • Brian Olsen 143 posts 424 karma points
    Aug 09, 2013 @ 11:46
    Brian Olsen
    0

    Hi Chriztian 

    just what I was looking for

    if it is not too much to ask would I know if you could cut it out for me

    <xsl:variablename="Billede"select="$currentPage/ancestor-or-self::*[normalize-space(testBackgroundBillede//umbracofile)][1]/testBackgroundBillede/widgets/widget[normalize-space(umbracofile)]"/>

    thanks in advance

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 09, 2013 @ 12:45
    Chriztian Steinmeier
    0

    Of course :-) - here goes:

    Breaking it up:

    <xsl:variable name="Billede"
    select="
        $currentPage
        /ancestor-or-self::
        *[normalize-space(testBackgroundBillede//umbracofile)]
        [1]
        /testBackgroundBillede/widgets/widget
        [normalize-space(umbracofile)]
    "/>
    

    Here's what the individual lines of the select does:

    1. Start from $currentPage and
    2. go up the tree through its ancestors and collect all those
    3. that does not have an empty umbracofile element below a testBackgroundBillede element.
    4. Grab only the first from that set
    5. and take all the testBackgroundBillede/widgets/widget nodes from that one
    6. if they don't have an empty umbracofile element

    Hope that helps you understand it better.

    For the various axes you can try the XPath Axes Visualizer tool to test them.

    The normalize-space() function I've found to be the best way to test for content in an element/attribute.

    /Chriztian

  • Brian Olsen 143 posts 424 karma points
    Aug 09, 2013 @ 12:49
    Brian Olsen
    0

    Hi Chriztian 

    thank you so much

    /Brian

Please Sign in or register to post replies

Write your reply to:

Draft