How to use umbraco.library GetMedia in XSLT for Umbraco v4.5

This is a quick follow-up on my previous blog post: “How to use umbraco.library GetMedia in XSLT“.  At the request of fellow Umbraco South-West UK developer, Dan, that I should update the code snippets for the new XML schema in Umbraco v4.5+

First a quick notice; if you are using v4.5.0, then please upgrade to v4.5.1, as there was a tiny bug in GetMedia that caused great confusion and headaches – you have been advised!

Without further ado, the updated XSLT snippet that you came here for…

<xsl:template match="/">
	<xsl:variable name="mediaId" select="number($currentPage/mediaId)" />
	<xsl:if test="$mediaId > 0">
		<xsl:variable name="mediaNode" select="umbraco.library:GetMedia($mediaId, 0)" />
		<xsl:if test="$mediaNode/umbracoFile">
			<img src="{$mediaNode/umbracoFile}" alt="[image]" height="{umbracoHeight}" width="{umbracoWidth}" />
		</xsl:if>
	</xsl:if>
</xsl:template>

Any questions? Come join us over at Our Umbraco… we are a friendly bunch!

9 thoughts on “How to use umbraco.library GetMedia in XSLT for Umbraco v4.5

  1. Thanks Lee for posting this. I am having trouble getting it to work, however, with my site logo that I have put in the Master Template.

    This is the code that works to display the logo. But it only works on the Homepage. It does not display on the inner text pages. It says: Error Parsing XSLT

    What am I missing?

  2. Hi Lauren, you can’t post XSLT code snippets in the comments. Again, best bet is to post on the Our Umbraco forum… I can help you out there. :-)

  3. Hey Lee,

    A little fix for this XSLT:
    height=”{umbracoHeight}” width=”{umbracoWidth}”

    Should be:
    height=”{$mediaNode/umbracoHeight}” width=”{$mediaNode/umbracoWidth}”

  4. A copy-of check on the initial mediaId variable returns NaN and no images is displayed in my version of 4.7.
    Seems someon else on the forum has the same issue :/