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:
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.
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) ;-)
I'm trying to implement this but am getting nothing returned, possibly because the macro is inserted in the master template and the media picker is on a document type of a template under the master?
Not sure what's going on, any help would be greatly appreciated. I've used this code several times without issue...
Amir, are you using Umbraco 4.5 or higher with the new XML schema (which is enabled by default)? In that case this will not work indeed, have a look at Lee's blog post that covers getmedia using the new schema.
If you're using the old XML schema, this looks fine and you should try and try something like this to see what the result of the GetMedia call is:
Sebastiaan, thanks for your response. I was using 4.5.x (2 i think), with the legacy schema. After trying several other XSLTs with both the legacy and new schema I was having tons of errors, as well as errors pulling simple things like page fields into a template. I wonder if I had just messed up a config file at some point, I've never experienced a problem like that before...Regardless, I did a fresh install of Juno, left new schema enabled, and used the following (slight adaptation of Lee's code) which pulls the image from MediaPicker, alt tag from node name, and the height and width very nicely. Thanks for your help! - Amir
Yep, I forgot to set useLegacySchema to "true" initially and then went about my normal installs / config changes, causing myself a mess. Good time to start using the new way anyway ;)
Get image in XSLT from 'Media' content type
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.
Hi,
You should use
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:
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.
I'm trying to implement this but am getting nothing returned, possibly because the macro is inserted in the master template and the media picker is on a document type of a template under the master?
Not sure what's going on, any help would be greatly appreciated. I've used this code several times without issue...
Amir, are you using Umbraco 4.5 or higher with the new XML schema (which is enabled by default)? In that case this will not work indeed, have a look at Lee's blog post that covers getmedia using the new schema.
If you're using the old XML schema, this looks fine and you should try and try something like this to see what the result of the GetMedia call is:
Sebastiaan, thanks for your response. I was using 4.5.x (2 i think), with the legacy schema. After trying several other XSLTs with both the legacy and new schema I was having tons of errors, as well as errors pulling simple things like page fields into a template. I wonder if I had just messed up a config file at some point, I've never experienced a problem like that before...Regardless, I did a fresh install of Juno, left new schema enabled, and used the following (slight adaptation of Lee's code) which pulls the image from MediaPicker, alt tag from node name, and the height and width very nicely. Thanks for your help! - Amir
I suspect that you have been switching between the old and new schema before you did a reinstall?
If you don't do the switch in the way described here, you end up with old and new schema xml mixed in the umbraco.config, which would explain your results.
Yep, I forgot to set useLegacySchema to "true" initially and then went about my normal installs / config changes, causing myself a mess. Good time to start using the new way anyway ;)
is working on a reply...