CodeGarden 10: The sixth annual Umbraco Developer Conference
June 23-25th 2010 - free ASP.NET MVC pre-conference. Register today!

get image in xslt from 'media' content type

Go directly to solutionA reply has been marked as a solution
2/9/2010 11:06:47 AMAvatarDarryl GoddenLocation: lichfield, ukposts: 18Karma: 38

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.

2/9/2010 11:11:38 AMAvatarDirk De GraveLocation: LotenhulleMVP.admin.posts: 2966Karma: 2934
Comment with ID: 25767

Hi,

You should use

<xsl:value-of select="umbraco.library:GetMedia(./data [@alias = 'Media'], 'false')/data [@alias = 'umbracoFile']"/>

 

Cheers,

/Dirk

2/9/2010 11:13:34 AMAvatarDirk De GraveLocation: LotenhulleMVP.admin.posts: 2966Karma: 2934
Solution Comment with ID: 25768
3

Here's a short explanation on the use of GetMedia

2/9/2010 11:13:38 AMAvatarSebastiaan JanssenLocation: 2584GJ, The Hague, The Netherlandsposts: 575Karma: 1362
Comment with ID: 25769

Have a look at Lee Kelleher's blog post about this, it explains all of the magic! :)

2/9/2010 11:26:41 AMAvatarLee KelleherLocation: Yatton, Bristol, UKposts: 463Karma: 1356
Comment with ID: 25773

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) &gt; 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.

2/9/2010 11:28:03 AMAvatarLee KelleherLocation: Yatton, Bristol, UKposts: 463Karma: 1356
Comment with ID: 25774

Oops, was a bit slow in posting.  Thanks guys!

2/9/2010 12:01:54 PMAvatarDarryl GoddenLocation: lichfield, ukposts: 18Karma: 38
Comment with ID: 25782

Thanks for your help Lee, Dirks' worked for now, but I shall look at your 'safer' method.

2/9/2010 12:14:31 PMAvatarLee KelleherLocation: Yatton, Bristol, UKposts: 463Karma: 1356
Comment with ID: 25784

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.

Please login or Sign up To post replies