Copied to clipboard

Flag this post as spam?

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


  • Yael Manshary 17 posts 57 karma points
    Aug 27, 2012 @ 10:17
    Yael Manshary
    0

    Making an image recursive in Xslt macro

     

    Hi,

    I am trying to understand how to make an image recursive in Xslt macro. I've searched the forum for hours, tryng to find what I need, but no luck so far. I would apriciate any help. I am not a programmer, and I have a fairly small understanding of Xslt/Xpath. I mainly use open code and change it to my (limited) understanding.

    I'm using Umbraco 4.7

    So basicly, I have an Xslt macro who makes a list of books the site sells. Every book should have an image, but sometimes it doesn't, and when it doesn't I'd like to show a default image which is in the parent node.

    this is the full code I have now:

    <?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="/">



    <!-- The fun starts here -->


       <div class="products_bg">
        
                <ul class="products_list">
                    <xsl:for-each select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1']">
                  <xsl:call-template name="strip"></xsl:call-template>
                      </xsl:for-each>
                  <div class="clearfix">&nbsp;</div>
                </ul>
     </div

                         
        </xsl:template>              
                      
    <xsl:template name="strip">
      
       <li>        
                        <div class="product_head">
                            <href="{umbraco.library:NiceUrl(@id)}"><h2>
                              <xsl:value-of select="title" disable-output-escaping="yes"/></h2></a>
                            <h3 class="right">
                                <xsl:value-of select="autor" disable-output-escaping="yes"/>&nbsp;</h3>
                            <h3 class="left">
                                <xsl:value-of select="publishing" disable-output-escaping="yes"/>&nbsp;</h3>
                            <div class="clearfix">&nbsp;</div>
                        </div>
         
                        <div class="product_info">
                            <div class="product_description">
                               &nbsp;<xsl:value-of select="shortDescription" disable-output-escaping="yes"/>
                            </div>
                            <div class="price">
                                <h4>
                                   &nbsp; <xsl:value-of select="price" disable-output-escaping="yes"/> $</h4>
                            </div>
                          
                            <href="{umbraco.library:NiceUrl(@id)}" class="more_info">more info</a
                           <xsl:value-of select="payPalLink" disable-output-escaping="yes"/>
                        
                        </div>
         
                        <div class="product_image_holder">
                         <href="{umbraco.library:NiceUrl(@id)}">
                           <img src="{img}" alt='{imageAlt}' title='{imgAlt}' />

         </a>

                        </div>
         
        <div class="clearfix">&nbsp;</div>
                 </li>
     
     </xsl:template>
    </xsl:stylesheet>

    The code worked perfectly so far, but now the client asked me to put this default image. 

    The specific image code is this line:

    <img src="{img}" alt='{imageAlt}' title='{imgAlt}' />

     

    Thank you so much to anyone who can help.


     

     

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 27, 2012 @ 11:12
    Chriztian Steinmeier
    0

    Hi Yael,

    Here's a quick "fix" for your current code...

    Add a template for the image before the final </xsl:stylesheet>:

    <xsl:template match="img">
    <img src="{.}" alt="{../imageAlt}" title="{../imgAlt}" />
    </xsl:template>

    And where you output it, use a recursive select:

    <xsl:apply-templates select="ancestor-or-self::*[normalize-space(img)][1]/img" />

    /Chriztian

  • Yael Manshary 17 posts 57 karma points
    Aug 27, 2012 @ 11:23
    Yael Manshary
    0

    Hi Chriztian,

    Your solution works great! And thank you so much for your quick response.

    Have a great day!

    Yael

Please Sign in or register to post replies

Write your reply to:

Draft