Copied to clipboard

Flag this post as spam?

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


  • Sajid Riaz 142 posts 165 karma points
    Jul 17, 2010 @ 21:12
    Sajid Riaz
    0

    How to get count of images in subfolder in media section ver4.5

    Hi Everyone.

    I have a sub folder specified in media section containing images.

    this folder is the source for a media picker property in a in a content page

    I declare a variable as follows to point at this property

    <xsl:variable name="imageRoot" select="$currentPage/gallery"/>

    first I want to examine the xml beneath this node:

         when i do <xsl:copy-of select="$imageRoot"/> I just get the ID i.e 1052

    second I need to know if this property contains any  images.

         when i do <xsl: value-of select="count($imageRoot)"/> i get 1 but I have 3 images under the folder.


    Many Thanks for any help.

    >sajid

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Jul 17, 2010 @ 23:14
    Chriztian Steinmeier
    1

    Hi Sajid,

    When you set your variable $ImageRoot - use the umbraco.library:GetMedia() function, to get the media XML for that node, and then you can just check for any Image (or 'node' if using Umbraco 4.0) below that (or you can use count() - but you only need to do that if you need to know how many):

    <xsl:variable name="ImageRoot" select="umbraco.library:GetMedia($currentPage/gallery, true())" />
    
    <!-- Check for images in folder -->
    <xsl:if test="$ImageRoot/*[self::node or self::Image]">
        ...
    </xsl:if>
    
    

     

    /Chriztian

  • Sajid Riaz 142 posts 165 karma points
    Jul 17, 2010 @ 23:25
    Sajid Riaz
    0

    Hi Chriztian, thanks 4 yr reply.

    here's what i have:

    <xsl:variable name="imageRoot" select="$currentPage/gallery"/>
    <xsl:variable name="mediaItems" select="umbraco.library:GetMedia($imageRoot, true())"/> 
     <xsl:value-of select="count($mediaItems)"/>

    but the count returns 1, when i have 3 images declared/

    the reason why i'm cgecking the count is because if the user has not uploaded any images i will get an error indicating xslt will not parse.

    >sajid


  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Jul 17, 2010 @ 23:32
    Chriztian Steinmeier
    1

    Hi Sajid,

    OK, you're doing almost what I said - your $mediaItems variable returns the folder node, so you will have to count the number of children it has:

    <xsl:value-of select="count($mediaItems/*[self::node or self::Image])" />

    But that's what the if statement I gave you before did too - the contents of the if will only be executed if the folder contains images.

    /Chriztian

  • Sajid Riaz 142 posts 165 karma points
    Jul 17, 2010 @ 23:43
    Sajid Riaz
    0

    Hi Chriztian.

     

    <xsl:variable name="mediaItems" select="umbraco.library:GetMedia($imageRoot, true())"/>

      <xsl:value-of select="count($mediaItems/*[self::node or self::Image])" />

     

    th value of returns 0.  when it should return 3 as there are 3 images.

    am not doing something correctly.  here my 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:variable name="imageRoot" select="$currentPage/gallery"/>
        
    <xsl:template match="/">

    <!-- start writing XSLT -->
    <xsl:variable name="mediaItems" select="umbraco.library:GetMedia($imageRoot, true())"/>
    <xsl:value-of select="count($mediaItems/*[self::node or self::Image])" />
    <xsl:if test="count($mediaItems/*[self::node or self::Image])">
     <xsl:for-each select="$mediaItems/Folder/Image">
        <xsl:variable name="picFile" select="umbracoFile"/>
        <xsl:variable name="picW" select="umbracoWidth"/>
        <xsl:variable name="picH" select="umbracoHeight"/>
          <img>
              <xsl:attribute name="src"><xsl:value-of select="$picFile"/></xsl:attribute>  
          </img>
      </xsl:for-each>
    </xsl:if>
    </xsl:template>
    </xsl:stylesheet>

  • Sajid Riaz 142 posts 165 karma points
    Jul 17, 2010 @ 23:49
    Sajid Riaz
    0

    Hi Chriztian,

     

    this seems to return the correct result i.e. 3 images:

    <xsl:value-of select="count($mediaItems/Folder/*[self::node or self::Image])">

     

    >sajid

  • Sajid Riaz 142 posts 165 karma points
    Jul 18, 2010 @ 00:01
    Sajid Riaz
    0

    fixed heres the 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:if test="string($currentPage/gallery ) != '' ">
      <xsl:variable name="imageRoot" select="$currentPage/gallery"/>
      <xsl:variable name="mediaItems" select="umbraco.library:GetMedia($imageRoot, true())"/>
      <xsl:value-of select="count($mediaItems/Folder/*[self::node or self::Image])" />
      <xsl:if test="count($mediaItems/Folder/*[self::node or self::Image])">
        <xsl:for-each select="$mediaItems/Folder/Image">
         <xsl:variable name="picFile" select="umbracoFile"/>
         <xsl:variable name="picW" select="umbracoWidth"/>
         <xsl:variable name="picH" select="umbracoHeight"/>
          <img>
              <xsl:attribute name="src"><xsl:value-of select="$picFile"/></xsl:attribute>  
          </img>
       </xsl:for-each>
     </xsl:if>
    </xsl:if>
    </xsl:template>
    </xsl:stylesheet>

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Jul 18, 2010 @ 00:21
    Chriztian Steinmeier
    1

    Hi Sajid,

    I did some testing between 4.0 and 4.5 and it seems there's a subtle difference: In 4.0 the GetMedia() method returns the actual <folder> item, whereas in 4.5 it returns an XML document containing the <Folder> item as its first child. So - as you found out, you'll need the /Folder/ in there...

    Just to try and get you a little teaser into the world XSLT, here's how I'd do something like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [
        <!ENTITY nbsp "&#x00A0;">
    ]>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxml="urn:schemas-microsoft-com:xslt"
        xmlns:umbraco.library="urn:umbraco.library"
        version="1.0"
        exclude-result-prefixes="msxml umbraco.library">
    
        <xsl:output method="xml" omit-xml-declaration="yes"/>
        <xsl:param name="currentPage"/>
    
        <xsl:template match="/">
    
            <!-- Get the folder Id -->
            <xsl:variable name="imageRoot" select="$currentPage/gallery"/>
    
            <!-- Safeguard against non-picked folder -->
            <xsl:if test="number($imageRoot)">
    
                <!-- Get the Folder XML -->
                <xsl:variable name="mediaItems" select="umbraco.library:GetMedia($imageRoot, true())"/>
    
                <!-- Render the images in that folder (if any) -->
                <xsl:apply-templates select="$mediaItems/Folder/Image" />
    
            </xsl:if>
        </xsl:template>
    
        <!-- Template for images -->
        <xsl:template match="Image">
            <img src="{$umbracoFile}" />
        </xsl:template>
    
    </xsl:stylesheet>

    /Chriztian

  • Sajid Riaz 142 posts 165 karma points
    Jul 18, 2010 @ 01:41
    Sajid Riaz
    0

    Thanks Chriztian,

    How can i get to see the nodes of what i'm searching for?

     

    >sajid

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies