I know this is a common subject and there are numerous articles in the forum, but I cannot get the xslt to work.
What I need to achieve is to randomly select one image from various images in a media folder. Following some examples, I've managed to list all of the images. But keep getting an xslt error when attempting to select an image using math.random().
It even has the code in it to select the random node. You just need to change it to $mediaID, and then you could just change the position to have to be equal to 1. There will be a more elegant way to get just one node from the random number but just modify the example slightly you can do it like this:
<msxsl:script language="c#" implements-prefix="randomTools"> <msxsl:assembly href="../bin/umbraco.dll"/> <![CDATA[ /// <summary> /// Gets a random integer that falls between the specified limits /// </summary> /// <param name="lowerLimit">An integer that defines the lower-boundary of the range</param> /// <param name="upperLimit">An integer that defines the upper-boundary of the range</param> /// <returns>A random integer within the specified range</returns> public static int GetRandom(int lowerLimit,int upperLimit) { Random r = umbraco.library.GetRandom(); int returnedNumber = 0; lock (r) { returnedNumber = r.Next(lowerLimit, upperLimit); } return returnedNumber; } ]]> </msxsl:script> </xsl:stylesheet>
<msxsl:script language="c#" implements-prefix="randomTools"> <msxsl:assembly href="../bin/umbraco.dll"/> <![CDATA[ /// <summary> /// Gets a random integer that falls between the specified limits /// </summary> /// <param name="lowerLimit">An integer that defines the lower-boundary of the range</param> /// <param name="upperLimit">An integer that defines the upper-boundary of the range</param> /// <returns>A random integer within the specified range</returns> public static int GetRandom(int lowerLimit,int upperLimit) { Random r = umbraco.library.GetRandom(); int returnedNumber = 0; lock (r) { returnedNumber = r.Next(lowerLimit, upperLimit); } return returnedNumber; } ]]> </msxsl:script> </xsl:stylesheet>
Now you can copy/paste the returned XML into an editor to get the overview of the structure and you should be able to figure out how to macth the XML.
Another thing...this bit of code...umbraco.library:GetMedia($mediaFolderID, 'false') should really look like this: umbraco.library:GetMedia($mediaFolderID, 1)...It's a comon misconception that writing 'false' is the way to do it...one I've also made myself in the past.
The extension expexts either true or false, dependant of it should display only the selected node or also it's possible child nodes. So it accepts either 1,0,true() or false() - when it encounters a string like 'false', it's actually interpreted as being true. if you had written 'someothertext' it would still return true.
However this code isn't applicable to my solution.
I know there should be a fairly easy way to get the SRC from one of these variables but I am very new to XSLT and can't quite figure this out. Looking for examples this specific is pretty darn tough.
Again I'm just looking for away to display the image now that I have all of the information.
<msxsl:script language="c#" implements-prefix="randomTools"> <msxsl:assembly href="../bin/umbraco.dll"/> <![CDATA[ /// <summary> /// Gets a random integer that falls between the specified limits /// </summary> /// <param name="lowerLimit">An integer that defines the lower-boundary of the range</param> /// <param name="upperLimit">An integer that defines the upper-boundary of the range</param> /// <returns>A random integer within the specified range</returns> public static int GetRandom(int lowerLimit,int upperLimit) { Random r = umbraco.library.GetRandom(); int returnedNumber = 0; lock (r) { returnedNumber = r.Next(lowerLimit, upperLimit); } return returnedNumber; } ]]> </msxsl:script>
I know I am close but being such a newbie I just cannot think of whats wrong!
Thanks again Jan for everything, you've already had a huge part in learning XSLT.
<msxsl:scriptlanguage="c#"implements-prefix="randomTools"> <msxsl:assemblyhref="../bin/umbraco.dll"/> <![CDATA[ /// <summary> /// Gets a random integer that falls between the specified limits /// </summary> /// <paramname="lowerLimit">An integer that defines the lower-boundary of the range</param> /// <paramname="upperLimit">An integer that defines the upper-boundary of the range</param> /// <returns>A random integer within the specified range</returns> public static int GetRandom(int lowerLimit,int upperLimit) { Random r = umbraco.library.GetRandom(); int returnedNumber = 0; lock (r) { returnedNumber = r.Next(lowerLimit, upperLimit); } return returnedNumber; } ]]> </msxsl:script>
In the xsl:if you were still testing on "node" instead of Image
Random Image from Media Section
Hi All
I know this is a common subject and there are numerous articles in the forum, but I cannot get the xslt to work.
What I need to achieve is to randomly select one image from various images in a media folder. Following some examples, I've managed to list all of the images. But keep getting an xslt error when attempting to select an image using math.random().
This is what I have:
All of the random code has been removed and I was hoping that someone could help me out.
Thanks in advance.
Eddie
There is a wiki reference article on getting a random: http://our.umbraco.org/wiki/reference/xslt/snippets/getting-a-series-of-unique-random-numbers.
It even has the code in it to select the random node. You just need to change it to $mediaID, and then you could just change the position to have to be equal to 1. There will be a more elegant way to get just one node from the random number but just modify the example slightly you can do it like this:
Hi Seith
Thanks for the wiki reference and the code snippet. Have updated the the previous xslt to the following and it works perfectly :-)
Thanks for the advice.
Eddie
I need to do exactly the same thing, but I can't seem to get this to work in my environment using the sample posted above.
The issue is that I'm not sure how to interpret the XSL, so I don't know how the variable "mediaFolderID" is getting populated in the pages.
Is that value coming from a property of the page template, and can it be a folder so it selects one of the children?
Sorry for the newb question, just getting started here.. :)
Hi Dave
Did you manage to get this sorted?
Thanks
Eddie
Hi Eddie,
I'm trying to add the above code to my website however I am not getting any output. I am using Umbraco 4.5.2.
All of my images are sitting in a media folder called "Homepage Banners" and I have the ID of 1057 for that media folder.
Now where would I need to change some of the variables to make this code work for my setup?
Here is my current XSLT:
Any help is much appreciated.
Owen
Hi Owen
The reason why you don't get any results is becaue the sample is written for the legacy schema and not the new one.
To see the XML you get returned in your $mediaFolderID variable wrap it in the following code, to get a dump of the it.
<textarea>
<xsl:copy-of select="$mediaFolderID" />
</textarea>
Now you can copy/paste the returned XML into an editor to get the overview of the structure and you should be able to figure out how to macth the XML.
Another thing...this bit of code...umbraco.library:GetMedia($mediaFolderID, 'false') should really look like this: umbraco.library:GetMedia($mediaFolderID, 1)...It's a comon misconception that writing 'false' is the way to do it...one I've also made myself in the past.
The extension expexts either true or false, dependant of it should display only the selected node or also it's possible child nodes. So it accepts either 1,0,true() or false() - when it encounters a string like 'false', it's actually interpreted as being true. if you had written 'someothertext' it would still return true.
Hope it makes sene :-)
/Jan
Hi Jan,
Thanks for your quick reply!
I have made some changes and and getting a dump of the $mediaID using the GetMedia Function with mediaFolderID of 1057, like this:
Now I get the following dump:
*NOTE* There are multiple images inside the <Folder> I am showing just one.
Now I am relatively new Umbraco/XSLT and I have the following code:
GetRandom takes and upper limit and a lower limit, and randomly returns a number between the two.
Now I am stuck on how to generate the Image properly using the new code.
Thanks in advance.
Owen
Hi Owen
Then you will need to match the "Image" element in your for-each, instead of "node"
/Jan
Hi Jan
Thanks for your help I have now made those changes.
I have been reading the forums trying to figure out a way of generating a Image for 4.5.2 but am fairly stumped.
The bit of code that I found was:
<xsl:variable name="image" select="umbraco.library:GetMedia($currentPage/test,'false')/umbracoFile" />
<img>
<xsl:attribute name="src">
/umbraco/imageGen.ashx?image=/<xsl:value-of select="$image"/>&width=89&height=147
</xsl:attribute>
<xsl:attribute name="alt">0</xsl:attribute>
</img>
However this code isn't applicable to my solution.
I know there should be a fairly easy way to get the SRC from one of these variables but I am very new to XSLT and can't quite figure this out. Looking for examples this specific is pretty darn tough.
Again I'm just looking for away to display the image now that I have all of the information.
Any help would be great,
Owen
Hi again
The sample you've found is also using the imageGen handler, which allows you to resize the image on the fly.
However what you need to do is getting the path from within the <umbracoFile> element...so what you need to do in your for-each loop is...
Hope this is what you are looking for?
/Jan
Jan!
I am so close I can feel it! But currently I am getting no output, no errors, nothing in stacktrace.
I went and checked the Macros/Templates and made sure they are all correct (which they are)
So that leaves me at the XSLT...
Here it is in its entirety:
I know I am close but being such a newbie I just cannot think of whats wrong!
Thanks again Jan for everything, you've already had a huge part in learning XSLT.
Owen
Hi
Try writing this
instad of this (Remove the ' ' around 1)
Does that help?
/Jan
Hi Jan,
Sorry no luck there, is there a way I can dump the image contents as text? Right here?
Ooooh...just spotted some more stuff that needs to be changed in the code.
Try pasting the following into your editor.
In the xsl:if you were still testing on "node" instead of Image
/Jan
JAN!
You did it! Thanks a million! If I only had 70 karma points!
Thank you again!
Owen
You're so very welcome. Nevermind the karma as long as you're able to move on with your project :-)
Happy coding.
/Jan
If you only want one image, there is a simple solution as shown here: http://our.umbraco.org/forum/developers/xslt/9262-One-single-random-imagepicture-from-a-media-folder
It's the solution from Jan, adapted by owen with a very simple "if" statement.
is working on a reply...