I think that $imgFolder does not contain "./node" elements, but File sub nodes. So count($imgFolder/node) returns zero. I think everywhere you have node you need File.
Check the output from your copy-of statement, is this blank? You need to look at the source code to see this.
If this is blank, check that you are passing in a picked media folder, or valid mediaID in the macro call
Check that the case of the parameter folder matches.
If you are still not getting a value passed from the template (where the macro is called) to the XSLT, then change the argument name from folder to say mediaFolder, in case it is a reserved word.
Random image from media folder.
Using Umbraco 4.7
Can't seem to get this to work.
I changed out $mediaFolder for the direct id to test.
I just get a blank <img/> tag.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp " "> ]>
<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.ExsltMath="urn:Exslt.ExsltMath"
exclude-result-prefixes="msxml umbraco.library Exslt.ExsltMath">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:param name="currentPage"/>
<xsl:variable name="mediaFolder" select="/macro/folder" />
<xsl:template match="/">
<xsl:variable name="imgFolder" select="umbraco.library:GetMedia(1150, 'false')"/>
<xsl:variable name="imgRandom" select="floor(Exslt.ExsltMath:random() * count($imgFolder/node) + 1)"/>
<xsl:variable name="img" select="$imgFolder/node[position()=$imgRandom]/data[@alias='umbracoFile']" />
<img src="{$img}" alt="" />
</xsl:template>
</xsl:stylesheet>
I think that $imgFolder does not contain "./node" elements, but File sub nodes. So count($imgFolder/node) returns zero. I think everywhere you have node you need File.
Richard
Almost there,
<xsl:variable name="mediaFolder" select="/macro/folder" />
<xsl:template match="/">
<xsl:copy-of select="$mediaFolder" />
<xsl:if test="$mediaFolder != ''">
<xsl:variable name="imgFolder" select="umbraco.library:GetMedia($mediaFolder, 'false')"/>
<xsl:variable name="imgRandom" select="floor(Exslt.ExsltMath:random() * count($imgFolder/Image) + 1)"/>
<xsl:variable name="img" select="$imgFolder/Image[position()=$imgRandom]/umbracoFile" />
<img src="{$img}" alt="" />
</xsl:if>
</xsl:template>
Just cant get $mediaFolder to work now.
It's a "mediaCurrent" type in the macro.
I would try the following:
A copy-of produces this,
Alex,
You need to get the id attribute of the folder tag, which will be $mediaFolder/folder/@id, and pass this into the GetMedia method.
Richard
Turns out it was a case issue, the "folder" in the variable needed a capital F.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp " "> ]>
<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.ExsltMath="urn:Exslt.ExsltMath"
exclude-result-prefixes="msxml umbraco.library Exslt.ExsltMath">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:param name="currentPage"/>
<xsl:variable name="mediaFolder" select="/macro/folder/Folder/@id" />
<xsl:variable name="css" select="/macro/imageCSSClass" />
<xsl:template match="/">
<xsl:if test="$mediaFolder > 0">
<xsl:variable name="imgFolder" select="umbraco.library:GetMedia($mediaFolder, 'false')"/>
<xsl:variable name="imgRandom" select="floor(Exslt.ExsltMath:random() * count($imgFolder/Image) + 1)"/>
<xsl:variable name="img" select="$imgFolder/Image[position()=$imgRandom]/umbracoFile" />
<img src="{$img}" alt="" class="{$css}" />
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Good to see how you resolved your problem.
is working on a reply...