Hi,
I'm trying to get an image to display in my XSLT. I have created a content type with a few text fields and a media type, when I use insert XSLT value-of and select the media field, all I get is a display of a number, here is my code:
<ul>
<xsl:for-each select="$currentPage/ancestor-or-self::root/node [@nodeTypeAlias='WhatsOn']/node">
<li><xsl:value-of select="data [@alias = 'TourTitle']"/><br />
<xsl:value-of select="data [@alias = 'Media']"/>
<span style="font-size:11px;">
<xsl:value-of select="data [@alias = 'EventDescription']" disable-output-escaping="yes"/></span>
</li>
</xsl:for-each>
</ul>
Thanks.
You should use
<xsl:value-of select="umbraco.library:GetMedia(./data [@alias = 'Media'], 'false')/data [@alias = 'umbracoFile']"/>
Cheers,
/Dirk
Here's a short explanation on the use of GetMedia
Have a look at Lee Kelleher's blog post about this, it explains all of the magic! :)
Dirk!!! Stop using that example, (it's bad practice IMHO - but I still love you), the 'false' string equates to true() in XSLT - where 0 (zero) or false() should be used.
Then there is the assumption that GetMedia will return the data nodes. If there isn't, then a big dirty exception is thrown... which is why I'm trying to encourage the "safer" approach - especially for XSLT beginners.
@Darryl, here goes another plug for my blog post on "How to use umbraco.library GetMedia in XSLT", here's a snippet:
<xsl:variable name="mediaNode" select="umbraco.library:GetMedia(data[@alias='Media'], 0)" /> <xsl:if test="count($mediaNode/data) > 0 and string($mediaNode/data[@alias='umbracoFile']) != ''"> <img src="{$mediaNode/data[@alias='umbracoFile']}" alt="[Hey hey I'm an alt tag!]" /> </xsl:if>
Obviously, it's not a one-liner... but less headaches.
Right, I'll get off my high horse now.
Mucho amor, Lee.
Oops, was a bit slow in posting. Thanks guys!
Thanks for your help Lee, Dirks' worked for now, but I shall look at your 'safer' method.
Hi Darryl, cool. Dirk's answer will work for 99.9999999999% of the time with no problems!
The "safer" approach is there to catch the "what if's" ... like if "data[@alias='Media']" is empty, or doesn't contain a numeric value? or if the media node is empty, or has been deleted? or something else that you haven't thought of (yet).
If you know your content/data, that's great ... I've experienced too many content-editors doing crazy things lately! (especially deleting media nodes) ;-)
Cheers, Lee.