Copied to clipboard

Flag this post as spam?

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


  • Robin 37 posts 109 karma points
    Oct 22, 2014 @ 17:57
    Robin
    0

    List images in a folder fails

    Hello,

    I'm trying to list all images in a folder and it is really driving me crazy. Hope someone of you can help.

    I want to list all images in a folder, the folder should be chosen when inserting the Macro (so not defined in a document type, that's something that I got working but not what I'm looking for). So far so good: I created a macro with an xslt and added a parameter 'mediaFolder' with type 'mediaCurrent'. When inserting the Macro I can select the folder. Nice.

    In my XSLT I have the following:

    <?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"/>
    <!-- This is our Macro Paramater with the alias of mediaFolder -->
    <xsl:param name="mediaFolder" select="/macro/mediaFolder" />

    <!-- The value of mediaFolder is an XML snippet, where we need to select the id attribute from the XML node <node> -->
    <xsl:param name="mediaFolderID" select="$mediaFolder/node/@id" />

    <xsl:template match="/">
    <!-- start writing XSLT -->
        <xsl:if test="$mediaFolder &gt; 0">
            <p>Yipee</p>
        </xsl:if>

       
    </xsl:template>

    </xsl:stylesheet>

     

    But in never get the 'Yipee' ! Somebody who can explain this?

    Is use Umbraco V4.11.3
    I found this tutorial which I try to follow: http://our.umbraco.org/wiki/reference/code-snippets/listfilesfrommediafolderxslt

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Oct 22, 2014 @ 18:00
    Jan Skovgaard
    0

    Hi Robin

    What do you see if you write out the content of your $mediaFolder variable? Does it contain anything?

    /Jan

  • Robin 37 posts 109 karma points
    Oct 22, 2014 @ 18:13
    Robin
    0

    Hi Jan,

    $mediaFolder does give a value, but $mediaFolderID doesn't

    The check should be:

     <xsl:if test="$mediaFolder &gt; 0">
    <p>Yipee</p>
    </xsl:if>

    and if I add:

     <xsl:value-of select="$mediaFolder"/>

    I get a value, but if I add:

     <xsl:value-of select="$mediaFolderID"/>

    Nothing returns

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Oct 22, 2014 @ 18:18
    Jan Skovgaard
    0

    Hi Robin

    Ok, well that's probably because you're looking for a /node element, which does not exist in the current schema and is not what you get returned anyway :)

    You need to feed the id to the GetMedia extension - Have a look at this http://our.umbraco.org/wiki/reference/umbracolibrary/getmedia

    Hope this helps

    /Jan

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Oct 22, 2014 @ 18:24
    Dennis Aaen
    0

    Hi Robin,

    It looks to me that you have found some old XSLT that match the old XML schema, from version Umbraco 4.5, and later on there were implemented a new XML schema, which also have influence on how you should write your XSLT.

    What if you do something like 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: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"/>
    <!-- This is our Macro Paramater with the alias of mediaFolder -->
    <xsl:param name="mediaFolder" select="/macro/mediaFolder" />

    <!-- The value of mediaFolder is an XML snippet, where we need to select the id attribute from the XML node <node> -->
    <xsl:param name="mediaFolderID" select="$mediaFolder/@id" />

    <xsl:template match="/">
    <!-- start writing XSLT -->
        <xsl:if test="$mediaFolderID &gt; 0">
            <p>Yipee</p>
        </xsl:if>
    </xsl:template>
    </xsl:stylesheet>

    Here a some documentation on the difference between the new and the old XML schema.

    http://our.umbraco.org/wiki/reference/xslt/45-xml-schema

    http://our.umbraco.org/wiki/reference/xslt/45-xml-schema/xslt-examples-updated-to-new-schema

    http://our.umbraco.org/wiki/reference/xslt/45-xml-schema/no-more-@nodetypealias

    Hope this helps,

    /Dennis

  • Robin 37 posts 109 karma points
    Oct 22, 2014 @ 18:34
    Robin
    0

    Hi,

    I just discovered I get nothing returned when I insert the macro in my template (where it should be). In my post above I inserted the Macro in a rich text editor in the 'content' section of the site. (The information I got was something like /media/22323/nameoftjepicture.jp1513211331..4556151jpg so it looked strange to me).

    Which I understand from your post above, I should add

    <xsl:value-of select="umbraco.library:GetMedia($mediaFolderID,0)/umbracoFile" />

    but when saving the XSLT I get 'Error occured System.OverflowException: Value was either too large or too small for an Int32.' When I replace $mediaFolder with a number, I can save. So I think the variable is still empty.

    I saw when I insert the macro in the template, a property mediaFolder="1327" is added, 1327 is the ID of the selected folder. Maybe this is what I need to get?

     

    Edit: I saw I didn't paste the code well, so insterted it.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Oct 22, 2014 @ 18:40
    Jan Skovgaard
    0

    Hi Robin

    Yeah, that sounds right :)

    Have a look at the link I posted and the example Dennis posted above as well.

    The error you got is because the value is not known before runtime so to avoid seeing it (if you're editing the XSLT inside Umbraco) and in general you need to make sure that the variable has a value before passing it the GetMedia extension.

    Hope this helps.

    /Jan

  • Robin 37 posts 109 karma points
    Oct 22, 2014 @ 18:56
    Robin
    0

    Hi,

    It still doesn't work.

    I deleted the /node in my code, but nothing changed. Also when I just copy/paste the code from Dennis it gives nothing. The $mediaFolderID stays empty

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Oct 22, 2014 @ 19:01
    Jan Skovgaard
    0

    Hi Robin

    Aaah, well that's because he's doing something that won't work either it seems.

    The below is copy pasted from the page I linked to.

    <xsl:variable name="media" select="umbraco.library:GetMedia($currentPage/bannerImage, 0)" />
    
     <xsl:if test="$media">
      <xsl:variable name="url" select="$media/umbracoFile" />
        <xsl:variable name="width" select="$media/umbracoWidth" />
         <xsl:variable name="height" select="$media/umbracoHeight" />
       <img src="{$url}" width="{$width}" height="{$height}" />
     </xsl:if>
    

    In the $media variable the GetMedia extension is being used.

    It returns the stuff you need to succeed - in your case it would look like this currently

    <xsl:variable name="mediaFolder" select="umbraco.library:GetMedia($currentPage/bannerImage, 0)" />
    
     <xsl:if test="$mediaFolder">
    <p>Yipeee</p>
     </xsl:if>
    

    Hope this works :)

    /Jan

  • Robin 37 posts 109 karma points
    Oct 22, 2014 @ 19:07
    Robin
    0

    :(

    I still got the same error, but if I change $currentpage/bannerImage to 7777 I can save.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Oct 22, 2014 @ 19:09
    Jan Skovgaard
    0

    Argh, sorry!

    You need to place the variable inside the if statement. My bad!

     <xsl:if test="$mediaFolder">
    <xsl:variable name="mediaFolderId" select="umbraco.library:GetMedia($currentPage/bannerImage, 0)" />
    <p>Yipeee</p>
     </xsl:if>
    

    Try this.

    /Jan

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Oct 22, 2014 @ 19:14
    Dennis Aaen
    101

    Hi Robin,

    With this snippet of code you should get the images from the folder that you have choosen when you added the macro into your template.

    <?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:Exslt.ExsltStrings="urn:Exslt.ExsltStrings"
        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:param name="mediafolder" select="/macro/mediaFolder/Folder/@id" />

       <xsl:template match="/">
           
          <xsl:if test="$mediafolder !=''">
               <xsl:variable name="files" select="umbraco.library:GetMedia($mediafolder, true())" />

           
              <xsl:if test="$files != ''">
                    <ul>
                        <!-- For each XML node <node> where the nodeTypeAlias attribute = File -->
                        <xsl:for-each select="$files/Image">
                            <li>
                                <!-- On the current XML node <node> find the XML node <data> where the alias atribute = umbracoFile -->
                             <!-- This is where the filepath to the file is stored -->
                               <a href="{./umbracoFile}" class="external">
                                   <img src="{./umbracoFile}" />
                             </a>
                            </li>
                       </xsl:for-each>
                 </ul>
               </xsl:if>
           </xsl:if>
       </xsl:template>
    </xsl:stylesheet>

    Hope this helps,

    /Dennis

  • Robin 37 posts 109 karma points
    Oct 22, 2014 @ 19:15
    Robin
    0

    No problem,

    I can save now (and now I understand what you ment with you have to check if it exists, but didn't know xslt was going to check if I checked it). But still I get no 'Yipee'.

  • Robin 37 posts 109 karma points
    Oct 22, 2014 @ 19:20
    Robin
    0

    Hi Dennis,

    Fantastic!! It works like a charm. Thanks a lot. I have a look now why it works :)

     

    Also thanks to Jan for his time and effort!

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Oct 22, 2014 @ 19:25
    Dennis Aaen
    0

    Hi Robin,

    You are welcome, can you please mark this as solved, so other people that came across the post can go directly to the solution that works for you.

    /Dennis

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Oct 22, 2014 @ 19:29
    Jan Skovgaard
    0

    Hi Robin

    You're welcome :)

    /Jan

Please Sign in or register to post replies

Write your reply to:

Draft