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 16, 2010 @ 19:28
    Sajid Riaz
    0

    version 4.5 xslt for getting all images from a folder in media

    Hi everyone,

    I'm converting an xslt 4 to 4.5 file which is used to get all images from a sub folder in the media section.

    here's the xslt currently, its very simple as i want to understand whats going on:

    I declare a property in doctType  named frontPageImages which is set to the actual folder containing the images:

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

    heres the rest of the xslt:

    <xsl:template match="/">

    <xsl:variable name="mediaItems" select="umbraco.library:GetMedia($imageRoot, true())"/>
     <xsl:for-each select="$mediaItems">
          <xsl:variable name="picFile" select="/Image/umbracoFile"/>
          <xsl:variable name="picW" select="/Image/umbracoWidth"/>
          <xsl:variable name="picH" select="/Image/umbracoHeight"/>
          <img>
              <xsl:attribute name="src"><xsl:value-of select="$picFile"/></xsl:attribute>  
          </img>

    </xsl:for-each>

    </xsl:template>
    </xsl:stylesheet>

    from my very little understanding the for loop is looping thru all utems in $mediaItems.

    but it does not work.  can any one shed any light on my cave.

    >sajid


  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 16, 2010 @ 20:16
    Lee Kelleher
    1

    Hi Sajid,

    Try changing the for-each to this...

    <xsl:for-each select="$mediaItems/Image">
        <xsl:variable name="picFile" select="umbracoFile"/>
        <xsl:variable name="picW" select="umbracoWidth"/>
        <xsl:variable name="picH" select="umbracoHeight"/>
        ...
    </xsl:for-each>

    ...so you are loop through each Image.

    Cheers, Lee.

  • Sajid Riaz 142 posts 165 karma points
    Jul 16, 2010 @ 20:29
    Sajid Riaz
    0

    Hi Lee, thanks for yr response:

    still not working.

    heres my xslt: slightly different from above but same scenario:

    <?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="ID" select="$currentPage/frontPageImages"/>
    <xsl:template match="/">

    <!-- start writing XSLT -->
    <xsl:if test="$ID &gt; 0">
    <xsl:variable name="mediaItems" select="umbraco.library:GetMedia($ID, true())"/>
      <xsl:for-each select="$mediaItems/Image">       
    <img>
        <xsl:attribute name="src">
        <xsl:text>/umbraco/imagegen.ashx?image=</xsl:text>
          <xsl:value-of select="umbracoFile"/>
        <xsl:text>&amp;width=300</xsl:text>
        </xsl:attribute>
    </img>
    </xsl:for-each>
    </xsl:if>

    </xsl:template>

    </xsl:stylesheet>

  • Hendrik Jan 71 posts 137 karma points
    Jul 16, 2010 @ 22:04
    Hendrik Jan
    2

    Hey, Tried this?

    <xsl:for-each select="$mediaItems/Folder/Image">       
     <!-- ..code here -->
    </xsl:for-each>


    or if u want images in subfolders to

    <xsl:for-each select="$mediaItems//Image">       
     <!-- ..code here -->
    </xsl:for-each>


  • Sajid Riaz 142 posts 165 karma points
    Jul 16, 2010 @ 22:08
    Sajid Riaz
    1

    Neils, you are a star...it worked...thank you!!!!

    here is xslt working:


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

    <xsl:template match="/">
      <xsl:variable name="mediaItems" select="umbraco.library:GetMedia($imageRoot, true())"/>
      <xsl:copy-of select="$mediaItems/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>

     

    how can i get the xml for the doc so i can see the nodes generated.

    again many many thanks

    >sajid

  • Sajid Riaz 142 posts 165 karma points
    Jul 16, 2010 @ 22:14
    Sajid Riaz
    0

    if i set doc property to an image in a folder both these work.

     <xsl:value-of select="umbraco.library:GetMedia($currentPage/yourImage, false())/Image/umbracoFile"/>

     <xsl:value-of select="umbraco.library:GetMedia($currentPage/yourImage, false())/Folder/Image/umbracoFile"/>

     

    so in a for loop we need to specify Folder in your post.  is that correct.

     

    >sajid

     

  • Sajid Riaz 142 posts 165 karma points
    Jul 16, 2010 @ 22:24
    Sajid Riaz
    0

    oops

    bold one does not work.  so if you want to get all images in a folder you need specify a for loop with Folder before Image as in Neils post above. 

    <xsl:value-of select="umbraco.library:GetMedia($currentPage/yourImage, false())/Image/umbracoFile"/>

     <xsl:value-of select="umbraco.library:GetMedia($currentPage/yourImage, false())/Folder/Image/umbracoFile"/>

     

    >sajid

  • Hendrik Jan 71 posts 137 karma points
    Jul 16, 2010 @ 22:41
    Hendrik Jan
    1

    mmm... i don't really know what you want to achieve but i think you want something like this?

    <xsl:choose>
      <xsl:when test="umbraco.library:GetMedia($currentPage/yourImage, 1)/node()[1]/@nodeTypeAlias = 'Image'">
        <!-- parse as image -->
      </xsl:when>
      <xsl:otherwise>
        <!-- parse as folder -->
      </xsl:otherwise>
    </xsl:choose>

     

  • Hendrik Jan 71 posts 137 karma points
    Jul 16, 2010 @ 23:01
    Hendrik Jan
    1

    Yes you need an for loop with a folder. This is because the folder has the images as children. So we need to loop through his children if u want to acces them.

    What is posible th,o is selecting an image from its index like this:

    <xsl:value-of select="umbraco.library:GetMedia($currentPage/yourImage, false())/Folder/Image[1]/umbracoFile"/>

  • Sajid Riaz 142 posts 165 karma points
    Jul 16, 2010 @ 23:04
    Sajid Riaz
    0

    Sorry Neils for confusing you,

    What I have is a a front page with one logo.  so i set a document as media picker and set to the company logo,  this is where i use:

    xsl:value-of select="umbraco.library:GetMedia($currentPage/yourImage, false())/Image/umbracoFile"/>

    so $currentPage/yourImage is the media picker property pointing at company logo.  this workd with xslt just specified in line above.

    There is also an image gallery page wher i use the for loop you gave me above.  This too now works after adding /Folder/Image.

    so all now works A ok,

    my query was that when i get the logo as in xslt line above i dont specif /Folder/Image/umbracoFileI just need to specify /Image/umbracoFile.

    where as in the for loop when i traverse thru a folder in the media sections to get all images in that folder i need to specify /Folder/Image


    So what i understand is that in a for loop when getting images from a folder we use /Folder/Image, but when we are getting just one image there is no need to specify /Folder/Image/umbracoFile, we can just specify /Image/umbracoFile.


    So i'm trying to get a better understanding of the syntax.

     

    >sajid

  • Hendrik Jan 71 posts 137 karma points
    Jul 16, 2010 @ 23:14
    Hendrik Jan
    0

    Ah ok :) thanks for clearing out. And yes you are correct!

    This is because when umbraco.library:GetMedia gives us a folder back, we acces it by /Folder.
    If it gives us an image back, we acces it by
    /Image.

    And we use a
    for-each loop if we want to loop through the children of an node. like when we loop trough the children nodes of the folder in /Folder/Image

  • Sajid Riaz 142 posts 165 karma points
    Jul 16, 2010 @ 23:19
    Sajid Riaz
    0

    thanks 4 all yr help.

     

    when i get into problem like this is there a way to get all the xml that is generated  so i can debug it?

     

    >sajid

  • Hendrik Jan 71 posts 137 karma points
    Jul 16, 2010 @ 23:30
    Hendrik Jan
    1

    No problem, i'm still a xslt noob myself. I learned from this to.

    Great question, getting the xml your working on ( <xsl:copy-of select=' '/> )

    Like this:
    <xsl:copy-of select="umbraco.library:GetMedia($currentPage/yourImage, false())"/>

  • Sajid Riaz 142 posts 165 karma points
    Jul 16, 2010 @ 23:38
    Sajid Riaz
    0

    thanx Neils...yr a great dude!!

    I'm also converting another xslt version 4 to 4.5.  It basically pulls back a random banner image when user comes back to frontpage or refreshes it.

    here's the xslt.  still not working in ver4.5:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
      version="1.0"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"  
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxml="urn:schemas-microsoft-com:xslt"
      xmlns:umbraco.library="urn:umbraco.library"
      xmlns:myFuncs="urn:my-scripts"
      exclude-result-prefixes="msxml myFuncs umbraco.library">

    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>
    <!--<xsl:variable name="imageRoot" select="$currentPage/frontPageImages"/>-->
    <xsl:variable name="imageRoot"   select="$currentPage/ancestor-or-self::* [@level = 1]/* [@isDoc and string(frontPageImages) !='-1']"/>

    <msxsl:script implements-prefix="myFuncs" language="JavaScript">
    <![CDATA[

    var myArray = new Array();
    var index;

    //generate random number
    function generateRandom(){     
     return index= Math.floor(Math.random() * myArray.length);    
    }

    //get image url from array
    function getUrl(){
      return myArray[index].url;
    }

    //get caption from array

    function getCaption(){
      return myArray[index].caption;
    }

    function addtoArray(itemUrl, itemCaption){
        var item = {url: itemUrl, caption: itemCaption};
        myArray.push(item);
    }


    ]]>

    </msxsl:script>     

        
    <xsl:template match="/">


    <xsl:call-template name="buildArray">  
      <xsl:with-param name="arrayNodes" select="umbraco.library:GetMedia($imageRoot, true())/Folder/Image"/>
      </xsl:call-template>


      <xsl:value-of select="myFuncs:generateRandom()" />

    <img>
    <xsl:attribute name="src">
    <xsl:text>/umbraco/imagegen.ashx?image=</xsl:text>
       <xsl:value-of select="myFuncs:getUrl()"/>
    <xsl:text>&amp;width=400</xsl:text>
    </xsl:attribute>
    <xsl:attribute name="style">
    <xsl:text>width:400px;</xsl:text>
    </xsl:attribute>
    </img>
    <p id="caption">
    <xsl:value-of select="myFuncs:getCaption()"/>
    </p>

      
    </xsl:template>


    <xsl:template name="buildArray" >
    <xsl:param name="arrayNodes"/>
        <xsl:for-each select="$arrayNodes">
          <xsl:value-of select="myFuncs:addtoArray(string(umbracoFile),string(Image/caption))"/>
        </xsl:for-each>
      
    </xsl:template>

    </xsl:stylesheet>

     

     

  • Hendrik Jan 71 posts 137 karma points
    Jul 17, 2010 @ 00:12
    Hendrik Jan
    0


    I dont know if this variable imageRoot is works.
    <xsl:variable name="imageRoot"   select="$currentPage/ancestor-or-self::* [@level = 1]/* [@isDoc and string(frontPageImages) !='-1']"/>

    But i would go for something like this :)

    <xsl:param name="currentPage"/>
    <xsl:variable name="imageRoot" select="$currentPage/ancestor-or-self::* [@level = 1]/* [@isDoc and string(frontPageImages) !='-1']"/>

    <xsl:template match="/">
      
      <xsl:variable name="randomImageNumber" select="ceiling( Exslt.ExsltMath:random() * count(umbraco.library:GetMedia($imageRoot, 1)/Folder/Image) )"/>
      <xsl:variable name="theImage" select="umbraco.library:GetMedia($imageRoot, 1)/Folder/Image[$randomImageNumber]"/>
        
      <img>
        <xsl:attribute name="src">
          <xsl:text>/umbraco/imagegen.ashx?image=</xsl:text>
             <xsl:value-of select="$theImage/umbracoFile"/>
          <xsl:text>&amp;width=400</xsl:text>
        </xsl:attribute>
        <xsl:attribute name="style">
          <xsl:text>width:400px;</xsl:text>
        </xsl:attribute>
      </img>
      
      <id="caption">
        <xsl:value-of select="$theImage/caption"/>
      </p>

    </xsl:template>

  • Sajid Riaz 142 posts 165 karma points
    Jul 17, 2010 @ 00:29
    Sajid Riaz
    0

    Neils amazing...

    heres what i did and it works,  only problem is tries to bring back 3 images when the folder only contains 2 so one of the images says hello world.

    The xslt is below and it works:

    i think i need to change something so it only brings back image nodes not sure what yet:  your xslt seems far better let me try it out.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
      version="1.0"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"  
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxml="urn:schemas-microsoft-com:xslt"
      xmlns:umbraco.library="urn:umbraco.library"
      xmlns:myFuncs="urn:my-scripts"
      exclude-result-prefixes="msxml myFuncs umbraco.library">

    <xsl:output method="xml" omit-xml-declaration="yes"/>

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

        <msxsl:script implements-prefix="myFuncs" language="JavaScript">
    <![CDATA[

    var myArray = new Array();
    var index;


    //generate random number
    function generateRandom(){     
     index= Math.floor(Math.random() * myArray.length);    
    }

    function addtoArray(itemUrl, itemCaption){
        var item = {url: itemUrl, caption: itemCaption};
        myArray.push(item);
    }
    //get image url from array
    function getUrl(){
        return myArray[index].url;
    }

    //get caption from array
    function getCaption(){
        return myArray[index].caption;
    }



    ]]>

    </msxsl:script>    
    <xsl:template match="/">
    <xsl:call-template name="buildArray">  
      <xsl:with-param name="arrayNodes" select="umbraco.library:GetMedia($imageRoot, true())/Folder/*"/>
      </xsl:call-template>

    <xsl:value-of select="myFuncs:generateRandom()" />
    <xsl:element name="img">
    <xsl:attribute name="src">
    <xsl:text>/umbraco/imagegen.ashx?image=</xsl:text>
    <xsl:value-of select="myFuncs:getUrl()"/>
    <xsl:text>&amp;width=200</xsl:text>
    </xsl:attribute>
    <xsl:attribute name="style">
    <xsl:text>width:400px;</xsl:text>
    </xsl:attribute>
    </xsl:element>
    <p id="caption">
    <xsl:value-of select="myFuncs:getCaption()"/>
    </p>
    </xsl:template>
    <xsl:template name="buildArray" >
    <xsl:param name="arrayNodes"/>
    <xsl:for-each select="$arrayNodes">  
    <xsl:value-of select="myFuncs:addtoArray(string(umbracoFile),string(caption))"/>
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

  • Hendrik Jan 71 posts 137 karma points
    Jul 17, 2010 @ 00:34
    Hendrik Jan
    0

    thx :)

    maybe try this

    //generate random number
    function generateRandom(){     
     index= Math.floor(Math.random() * myArray.length);    // change 
    myArray.length to  (myArray.length-1)
    }


    so you would get
    //generate random number
    function generateRandom(){     
     index= Math.floor(Math.random() * (myArray.length-1)); 

    }

    necessary since in your code Math.Random generates between 0-1. you multiply it with 2 wich means you get  0,1,2 = 3 images,
    changing it to multiply with 1 less gets you 0,1 = 2 images

    ::edit::

    Sorry i was wrong, this is needed when u use xstl indexes. (xslt begins on 1, javascript on 0)


  • Sajid Riaz 142 posts 165 karma points
    Jul 17, 2010 @ 00:53
    Sajid Riaz
    0

    Hey Neils thanx for all yr help in this...its getting late on friday but you still helping...U r kool.

     

    i've posted my original ver4 xslt below which exact same job but no hello world image:

    I'm wondering if its anything to do with the imageRoot selction.  I mean could it be trying to convert the frontPageImages folder into a image and hence the helloworld image. 

    here how i define imageRoot:

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

    and heres how i traverse thru its contents:

    <xsl:call-template name="buildArray">  
      <xsl:with-param name="arrayNodes" select="umbraco.library:GetMedia($imageRoot, true())/Folder/*"/>
      </xsl:call-template>

    I did try /Folder/Image but error was javascript getUrl undefined

    but myarray,length - 1 does not work as it brings back only one of the images and the helloworld image.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>

    <xsl:stylesheet
        version="1.0"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxml="urn:schemas-microsoft-com:xslt"
        xmlns:umbraco.library="urn:umbraco.library"
        xmlns:myFuncs="urn:my-scripts"
        exclude-result-prefixes="msxml myFuncs umbraco.library">


    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

    <xsl:param name="currentPage"/>

    <xsl:variable name="imageRoot"     select="$currentPage/ancestor-or-self:: node[@level = 1]/data [@alias = 'frontPageImages']"/>

    <msxsl:script implements-prefix="myFuncs" language="JavaScript">
    <![CDATA[

    var myArray = new Array();
    var index;

    //generate random number
    function generateRandom(){    
     index= Math.floor(Math.random() * myArray.length);   
    }

    //get image url from array
    function getUrl(){
        return myArray[index].url;

    }

    //get caption from array

    function getCaption(){
        return myArray[index].caption;
    }

    function addtoArray(itemUrl, itemCaption){
        var item = {url: itemUrl, caption: itemCaption};
        myArray.push(item);
    }

    ]]>

    </msxsl:script>

    <xsl:template match="/">


    <xsl:call-template name="buildArray">
    <xsl:with-param name="arrayNodes" select="umbraco.library:GetMedia($imageRoot, 'true')/node"/>

    </xsl:call-template>


    <xsl:value-of select="myFuncs:generateRandom()" />
    <img>
    <xsl:attribute name="src">
    <xsl:text>/umbraco/imagegen.ashx?image=</xsl:text>
       <xsl:value-of select="myFuncs:getUrl()"/>
    <xsl:text>&amp;width=400</xsl:text>
    </xsl:attribute>
    <xsl:attribute name="style">
    <xsl:text>width:400px;</xsl:text>
    </xsl:attribute>
    </img>
    <p id="caption">
    <xsl:value-of select="myFuncs:getCaption()"/>
    </p>


    </xsl:template>


    <xsl:template name="buildArray" >
    <xsl:param name="arrayNodes"/>
        <xsl:for-each select="$arrayNodes">

            <xsl:value-of select="myFuncs:addtoArray(string(./data[@alias='umbracoFile']),string(./data[@alias='caption']))"/>

        </xsl:for-each>

    </xsl:template>
    </xsl:stylesheet>


     

     

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

    Hi Neils,

    In the original v4 xslt I had:

    <xsl:variable name="imageRoot"     select="$currentPage/ancestor-or-self:: node[@level = 1]/data [@alias = 'frontPageImages']"/>

    in the v4.5 ver i have translated above to:

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

     

    not sure may be i should also have the level?

     

    >sajid

  • Sajid Riaz 142 posts 165 karma points
    Jul 17, 2010 @ 01:08
    Sajid Riaz
    0

    ok a temprary fix:

    //generate random number
    function generateRandom(){     
      index= Math.floor(Math.random() * myArray.length);
       
      if (index == 0){
        index = 1;
      }

    }

     

    when index == 0 is when hello world is shown.

     

    if i obly have 2 images myarray.length should be 2 not 3...hmmm?

     

    >sajid

  • Hendrik Jan 71 posts 137 karma points
    Jul 17, 2010 @ 01:10
    Hendrik Jan
    0

    [empty]

  • Hendrik Jan 71 posts 137 karma points
    Jul 17, 2010 @ 01:14
    Hendrik Jan
    1

    Hey,

    Oh i Remember now :)

    if you us xsl:copy-of on the umbraco.library:GetMedia($imageRoot, true())
    U can see that the /Folder has more children than just Image or File. it also has an empty <contents></contents> child.

    Wich explains the third child.


    So i think that this would work:
    <xsl:call-template name="buildArray">  
      <xsl:with-param name="arrayNodes" select="umbraco.library:GetMedia($imageRoot, true())/Folder/Image"/>
    </xsl:call-template>

    An more ugly fix would be:
    <xsl:for-each select="$arrayNodes">
      <xsl:if test="@nodeTypeAlias = 'Image' "'>
        <xsl:value-of select="myFuncs:addtoArray(string(umbracoFile),string(caption))"/>
      </xsl:if>
    </
    xsl:for-each>


    As for the selector
    <xsl:variable name="imageRoot"     select="$currentPage/ancestor-or-self:: node[@level = 1]/data [@alias = 'frontPageImages']"/>

    i would make that
    <xsl:variable name="imageRoot" select="$currentPage/ancestor-or-self:: * [@isDoc][@level = 1]/frontPageImages"/>


    *note. A handy tool for converting:
    blackpoint.dk/.../convert-xml-schema-to-45-.aspx

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

    hi Neils

    using /Folder/Image results in error:

    or the alternative xslt results in:

    Microsoft.JScript.JScriptException: Object required
    at Microsoft.JScript.Convert.ToObject(Object value, VsaEngine engine)
    at System.Xml.Xsl.CompiledQuery.Script129.getUrl()


    how do i use: "@nodeTypeAlias = 'Image'   at time of selection like with this:

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

    i also tried

    <xsl:variable name="imageRoot" select="$currentPage/frontPageImages [ Not @isDoc]"/>

     

    as I thought maybe the actual containing folder was also going thru.

    but error on that too.

     

    >sajid

  • Hendrik Jan 71 posts 137 karma points
    Jul 17, 2010 @ 01:29
    Hendrik Jan
    0

    Hey

    U can try this for the nodes selector
    <xsl:with-param name="arrayNodes" select="umbraco.library:GetMedia($imageRoot, true())/Folder/* [@nodeTypeAlias = 'Image']"/>

  • Sajid Riaz 142 posts 165 karma points
    Jul 17, 2010 @ 01:32
    Sajid Riaz
    0

    Conversion tool is excellent, but it gave save javascript error on:

    <xsl:with-param name="arrayNodes" select="umbraco.library:GetMedia($imageRoot, 'true')/* [@isDoc]"

    if i chnage to

    <xsl:with-param name="arrayNodes" select="umbraco.library:GetMedia($imageRoot, 'true')/Folder/*"

     

    its ok.

  • Sajid Riaz 142 posts 165 karma points
    Jul 17, 2010 @ 01:37
    Sajid Riaz
    0

    Hey Neils, even with Nodes selector you kindly posted above get error:

     

    Microsoft.JScript.JScriptException: Object required
    at Microsoft.JScript.Convert.ToObject(Object value, VsaEngine engine)
    at System.Xml.Xsl.CompiledQuery.Script149.getUrl()

     

    looks like all the possibilities to prevent the content node from being passed thru to addToArray are being blocked by this error.

     

     

    >sajid

  • Hendrik Jan 71 posts 137 karma points
    Jul 17, 2010 @ 01:57
    Hendrik Jan
    0

    Hey Sajid,

    Ok I went to umbraco and tested your code, runs with no errors here :O (with /Folder/Image)
    Ofcourse i diddnt use your imageRoot variable, i just used a NodeId

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>

    <xsl:stylesheet 
    version="1.0"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    xmlns:umbraco.library="urn:umbraco.library"
    xmlns:myFuncs="urn:my-scripts" 
    exclude-result-prefixes="msxml myFuncs umbraco.library">


    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

    <xsl:param name="currentPage"/>

    <xsl:variable name="imageRoot" select="$currentPage/ancestor-or-self:: * [@isDoc][@level = 1]/frontPageImages"/>

    <msxsl:script implements-prefix="myFuncs" language="JavaScript"
    <![CDATA[

    var myArray = new Array();
    var index;

    //generate random number
    function generateRandom(){ 
    index= Math.floor(Math.random() * myArray.length); 
    }

    //get image url from array
    function getUrl(){
    return myArray[index].url;

    }

    //get caption from array

    function getCaption(){
    return myArray[index].caption; 
    }

    function addtoArray(itemUrl, itemCaption){
    var item = {url: itemUrl, caption: itemCaption};
    myArray.push(item);
    }

    ]]>

    </msxsl:script

    <xsl:template match="/">


    <xsl:call-template name="buildArray">
      <xsl:with-param name="arrayNodes" select="umbraco.library:GetMedia($imageRoot, 'true')/Folder/Image"/>

    </xsl:call-template>


    <xsl:value-of select="myFuncs:generateRandom()" />
    <img>
    <xsl:attribute name="src">
    <xsl:text>/umbraco/imagegen.ashx?image=</xsl:text>
    <xsl:value-of select="myFuncs:getUrl()"/>
    <xsl:text>&amp;width=400</xsl:text>
    </xsl:attribute>
    <xsl:attribute name="style">
    <xsl:text>width:400px;</xsl:text>
    </xsl:attribute>
    </img>
    <id="caption">
    <xsl:value-of select="myFuncs:getCaption()"/>
    </p>


    </xsl:template>


    <xsl:template name="buildArray" >
    <xsl:param name="arrayNodes"/>
    <xsl:for-each select="$arrayNodes">

    <xsl:value-of select="myFuncs:addtoArray(string(./umbracoFile),string(./caption))"/>

    </xsl:for-each>

    </xsl:template>
    </xsl:stylesheet>
    An other option is to just keep the code when it worked but added 3 
    elements to the array and filter the contents node like this

    function addtoArray(itemUrl, itemCaption){
    if(
    itemUrl == undefined) return false
    var item = {url: itemUrl, caption: itemCaption};
    myArray.push(item);
    }
  • Sajid Riaz 142 posts 165 karma points
    Jul 17, 2010 @ 02:23
    Sajid Riaz
    0

    ok Neil will try the javascript option

    can you tell me in my xslt how can i get the complete xml doc returned so i can see all nodes?

     

    oh yeah I tried the xslt to kindly posted above.  but same error pinting at javascript getURL:

    but ok will try function above...other wise it does work if index is not 0.

     

    thanks ever so much for all yr kind efforts ;-)

     

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>

    <xsl:stylesheet
    version="1.0"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    xmlns:umbraco.library="urn:umbraco.library"
    xmlns:myFuncs="urn:my-scripts"
    exclude-result-prefixes="msxml myFuncs umbraco.library" >

      


    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

    <xsl:param name="currentPage"/>

    <xsl:variable name="imageRoot" select="$currentPage/ancestor-or-self:: * [@isDoc][@level = 1]/frontPageImages"/>

    <msxsl:script implements-prefix="myFuncs" language="JavaScript">


    <![CDATA[

    var myArray = new Array();
    var index;


    //generate random number
    function generateRandom(){     
      index= Math.floor(Math.random() * myArray.length);
       //array lenth is 3 as we 2 images and an empty content folder.
      if (index == 0){
        index = 1;
      }

    }

    function addtoArray(itemUrl, itemCaption){
        var item = {url: itemUrl, caption: itemCaption};
        myArray.push(item);
    }
    //get image url from array
    function getUrl(){
        return myArray[index].url;
    }

    //get caption from array
    function getCaption(){
        return myArray[index].caption;
    }



    ]]>
    </msxsl:script>

    <xsl:template match="/">


    <xsl:call-template name="buildArray">
      <xsl:with-param name="arrayNodes" select="umbraco.library:GetMedia($imageRoot, 'true')/Folder/Image"/>

      </xsl:call-template>



      
      
    <!--<xsl:element name="img">-->
      <img>
    <xsl:attribute name="src"
      <xsl:text>/umbraco/imagegen.ashx?image=</xsl:text>
    <xsl:value-of select="myFuncs:getUrl()"/>
      <xsl:text>&amp;width=200</xsl:text>
      </xsl:attribute>
    <xsl:attribute name="style">
      <xsl:text>width:400px;</xsl:text>
      </xsl:attribute>
      </img>
    <!--</xsl:element>-->
    <p id="caption">
    <xsl:value-of select="myFuncs:getCaption()"/>
      </p>


      
      


      </xsl:template>


    <xsl:template name="buildArray" >
    <xsl:param name="arrayNodes"/>
    <xsl:for-each select="$arrayNodes">

    <xsl:value-of select="myFuncs:addtoArray(string(./umbracoFile),string(./caption))"/>

      </xsl:for-each>

      </xsl:template>
      </xsl:stylesheet>

     

  • MartinB 411 posts 512 karma points
    Aug 10, 2010 @ 11:43
    MartinB
    0

    Hi Niels and Sajid

    Just wanted to thank you for assisting me with building an xslt that gets all images in a folder selected with mediaPicker.

    I'll then use jQuery cycle plugin to shuffle the frontpage image each time a user visits or refreshes.

    My xslt (hope to help others):

    <?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="media" select="$currentPage/bannerImage" />

    <xsl:template match="/">

    <textarea>
    <xsl:value-of select="$currentPage/bannerImage" />
    </textarea>

    <xsl:variable name="mediaItems" select="umbraco.library:GetMedia($media, true())"/>
    <xsl:for-each select="$mediaItems/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:template>

    </xsl:stylesheet>
  • MartinB 411 posts 512 karma points
    Aug 10, 2010 @ 11:45
    MartinB
    0

    Of course the <textarea> is for testing purposes.

  • Roger 195 posts 474 karma points
    Apr 24, 2013 @ 17:47
    Roger
    0

    Hi all, is there a way of getting media images from a media folder to display where the media folder = the media folder selected by a media picker on a content node?

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Apr 24, 2013 @ 17:53
    Chriztian Steinmeier
    0

    Hi Roger,

    You'll have much better luck with creating a new topic with your question.

    It's also easier for others to find an actual solution to a problem, since only a single reply can be marked as the answer.

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft