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>
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 " ">
]>
<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>
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
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
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
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:
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
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 " "> ]>
<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>
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
fixed heres the xslt:
<?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.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>
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:
/Chriztian
Thanks Chriztian,
How can i get to see the nodes of what i'm searching for?
>sajid
is working on a reply...
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.