If you know how to use a <xsl:choose> let's work with that.Let's say that you have two media pickers called "alternateEventImage" and "eventImage". You should be able to do something like this if we are working inside a for-each (it looks like that in your first post since you're using the current() ):
This is not the most beatiful code, and you could solve the problem in other ways, but when you already know how a <xsl:choose> works, I think that you'll understand this pretty fast.
What the code does, is first checking if the alternativeEventImage media picker has a selected media node. If that's not the case, then we're trying out the eventImage media picker. If none of the two media pickers has got a value, then nothing will be rendered in the frontend.
Thanks for that, but I'm afraid it doesn't work for what need to do. I'm dealing with images on the current page, not a child node.
I'm trying to say "If imageB exists for this page, show it; otherwise, show imageA". It's an Event page – imageA is for display by the parent page; imageB (which may or may not exist) is for display on the Event page itself.
Just can't get this to work! When/otherwise logic is fine (tested by rendering plain text to page). However, image just doesn't render. I get "Error parsing XSLT file: \xslt\placeEventImage.xslt". If I try to put in the <xsl:otherwise> part I get an error when saving the XSLT file (see below), so I'll just concentate on the <xsl:when...> part here.
The error that you get with System.OverflowException etc. occurs because you haven't tested if the media picker has a value or not. When you hit the Save-button the XSLT-file tries to run the code, and at that time the "newsImage"-fields doesn't have a value, so the GetMedia extension will fail. You should be able to check if the property has a value by changing your code to this:
You say that you get the "Error parsing XSLT file"-error with that code. Could you try adding ?umbdebugshowtrace=true to the URL of that page? Then you can maybe see the error that occurs when you scroll down the page. And if so, could you please provide us with the description of the error?
Value was either too large or too small for an Int32. Value was either too large or too small for an Int32. at System.Convert.ToInt32(Double value) at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) at System.Xml.Xsl.Runtime.XmlQueryRuntime.ChangeTypeXsltArgument(XmlQueryType xmlType, Object value, Type destinationType) at System.Xml.Xsl.Runtime.XmlQueryContext.InvokeXsltLateBoundFunction(String name, String namespaceUri, IList`1[] args)
at System.Xml.Xsl.CompiledQuery.Query.<xsl:template
match="/">(XmlQueryRuntime
{urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator
{urn:schemas-microsoft-com:xslt-debug}current, Double
{urn:schemas-microsoft-com:xslt-debug}position, Double
{urn:schemas-microsoft-com:xslt-debug}last, IList`1
{urn:schemas-microsoft-com:xslt-debug}namespaces) in
C:\virtualservers\stp.urltba.com\xslt\placeEventImage.xslt:line 19
at
System.Xml.Xsl.CompiledQuery.Query.<xsl:apply-templates>(XmlQueryRuntime
{urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator , Double ,
Double ) at System.Xml.Xsl.CompiledQuery.Query.Root(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime) at System.Xml.Xsl.CompiledQuery.Query.Execute(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument,
XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter
writer, Boolean closeWriter) at
System.Xml.Xsl.XmlILCommand.Execute(IXPathNavigable contextDocument,
XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter
results) at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, TextWriter results) at umbraco.macro.GetXsltTransformResult(XmlDocument macroXML, XslCompiledTransform xslt, Dictionary`2 parameters) at umbraco.macro.GetXsltTransformResult(XmlDocument macroXML, XslCompiledTransform xslt) at umbraco.macro.loadMacroXSLT(macro macro, Hashtable attributes, Hashtable pageElements)
Okay, so you haven't got any other code in the file. Just wanted to check that to see if it could be something else that caused the error. I guess that the page that get's the error has a value in the "alternateEventImage"-field right?
And just to be sure, the choosen image in that field, is it still available in the media section? It hasn't been deleted or something like that?
Actually Robin, could you try outcommenting the entire <xsl:choose>, and just render the content of the alternateEventImage-field (if this field is not empty of course), like this:
One thing that just came to my mind when I thought about this problem was, are we at all talking about a media picker? The two properties on your page (alternateEventImage and eventImage) are they of the type "Media picker"?
Or are you using another data type for these two fields? Like eg. the "Upload"-type maybe?
If you are usign the upload data type we do not need to use the xslt extension, but you can just do like this:
I did port s reply to this the other day, but it didn't seem to post properly.
I was using Upload rather than Media Picker, which I don't think I'd have much use for.
Thanks for soling this for me, and I'm sorry if I've wasted your time with something that ended up being so straightforward: I guess I'll have to learn to be much more specific.
Display alternate image if it exists
I need to be able to show either the 'main image' or an alternative image if it exists.
I currently have:
<xsl:if test="string(current()/alternateEventImage) != ''">
<img src="{current()/alternateEventImage}" alt="{current()/alternateEventImageAltAndTitle}" title="{current()/alternateEventImageAltAndTitle}" />
</xsl:if>
<xsl:if test="string(current()/alternateEventImage) = ''">
<img src="{current()/eventImage}" alt="{current()/eventImageAltAndTitle}" title="{current()/eventImageAltAndTitle}" />
</xsl:if>
I have previously tried it with when/otherwise, but couldn't get that to work either.
Perhaps I should clarify. I want to do this if alternateEventImage exists (has been uploaded to the page node under Content:
<img src='<umbraco:Item field="alternateEventImage" runat="server"></umbraco:Item>' width="540" height="190"
title='<umbraco:Item field="alternateEventImageAltAndTitle" runat="server"></umbraco:Item>'
alt='<umbraco:Item field="alternateEventImageAltAndTitle" runat="server"></umbraco:Item>' />
Or this if alternateEventImage does not exist:
<img src='<umbraco:Item field="eventImage" runat="server"></umbraco:Item>' width="540" height="190"
title='<umbraco:Item field="eventImageAltAndTitle" runat="server"></umbraco:Item>'
alt='<umbraco:Item field="eventImageAltAndTitle" runat="server"></umbraco:Item>' />
Hi Robin
This should be possible through XSLT.
If you know how to use a <xsl:choose> let's work with that.Let's say that you have two media pickers called "alternateEventImage" and "eventImage". You should be able to do something like this if we are working inside a for-each (it looks like that in your first post since you're using the current() ):
This is not the most beatiful code, and you could solve the problem in other ways, but when you already know how a <xsl:choose> works, I think that you'll understand this pretty fast.
What the code does, is first checking if the alternativeEventImage media picker has a selected media node. If that's not the case, then we're trying out the eventImage media picker. If none of the two media pickers has got a value, then nothing will be rendered in the frontend.
/Kim A
Hi Kim
Thanks for that, but I'm afraid it doesn't work for what need to do. I'm dealing with images on the current page, not a child node.
I'm trying to say "If imageB exists for this page, show it; otherwise, show imageA". It's an Event page – imageA is for display by the parent page; imageB (which may or may not exist) is for display on the Event page itself.
Okay so imageB(alternateEventImage) is located on the current page and imageA(eventImage) is located on the parent page. Am I right?
If that's the case, you can change the code in my first post to something this:
Does that work, and did I understand your content structure the right way?
/Kim A
Just can't get this to work! When/otherwise logic is fine (tested by rendering plain text to page). However, image just doesn't render. I get "Error parsing XSLT file: \xslt\placeEventImage.xslt". If I try to put in the <xsl:otherwise> part I get an error when saving the XSLT file (see below), so I'll just concentate on the <xsl:when...> part here.
I've also tried it with the following code inside <xsl:when...>:
By the way, both images are on the current page.
First line of error mentioned above (when using both 'when' and 'otherwise'):
System.OverflowException: Value was either too large or too small for an Int32.
The error that you get with System.OverflowException etc. occurs because you haven't tested if the media picker has a value or not. When you hit the Save-button the XSLT-file tries to run the code, and at that time the "newsImage"-fields doesn't have a value, so the GetMedia extension will fail. You should be able to check if the property has a value by changing your code to this:
If both of the images are placed on the current page, you should be able to change the code to this (without the ../):
You say that you get the "Error parsing XSLT file"-error with that code. Could you try adding ?umbdebugshowtrace=true to the URL of that page? Then you can maybe see the error that occurs when you scroll down the page. And if so, could you please provide us with the description of the error?
/Kim A
Yes, still getting that error. Trace output is:
Value was either too large or too small for an Int32.
at System.Convert.ToInt32(Double value)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Xml.Xsl.Runtime.XmlQueryRuntime.ChangeTypeXsltArgument(XmlQueryType xmlType, Object value, Type destinationType)
at System.Xml.Xsl.Runtime.XmlQueryContext.InvokeXsltLateBoundFunction(String name, String namespaceUri, IList`1[] args)
at System.Xml.Xsl.CompiledQuery.Query.<xsl:template match="/">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current, Double {urn:schemas-microsoft-com:xslt-debug}position, Double {urn:schemas-microsoft-com:xslt-debug}last, IList`1 {urn:schemas-microsoft-com:xslt-debug}namespaces) in C:\virtualservers\stp.urltba.com\xslt\placeEventImage.xslt:line 19
at System.Xml.Xsl.CompiledQuery.Query.<xsl:apply-templates>(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator , Double , Double )
at System.Xml.Xsl.CompiledQuery.Query.Root(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
at System.Xml.Xsl.CompiledQuery.Query.Execute(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer, Boolean closeWriter)
at System.Xml.Xsl.XmlILCommand.Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter results)
at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, TextWriter results)
at umbraco.macro.GetXsltTransformResult(XmlDocument macroXML, XslCompiledTransform xslt, Dictionary`2 parameters)
at umbraco.macro.GetXsltTransformResult(XmlDocument macroXML, XslCompiledTransform xslt)
at umbraco.macro.loadMacroXSLT(macro macro, Hashtable attributes, Hashtable pageElements)
Could you try showing us your entire XSLT code from placeEventImage.xslt?
I have a feeling that we're missing something obvious here, but if you could show us your entire code it would be helpful :)
/Kim A
I am new to Umbraco, so it is bound to be something obvious (to all but me!)…
Ohh, I meant the xslt code from the placeEventImage.xslt-file. Not the stack trace output :)
/Kim A
Hmmm… I did think that was a rather larg chunk of data!
Okay, so you haven't got any other code in the file. Just wanted to check that to see if it could be something else that caused the error. I guess that the page that get's the error has a value in the "alternateEventImage"-field right?
And just to be sure, the choosen image in that field, is it still available in the media section? It hasn't been deleted or something like that?
/Kim A
Actually Robin, could you try outcommenting the entire <xsl:choose>, and just render the content of the alternateEventImage-field (if this field is not empty of course), like this:
Then we should be able to see if this value is messed up, or at least give us a clue about it I hope :)
What's the value?
/Kim A
One thing that just came to my mind when I thought about this problem was, are we at all talking about a media picker? The two properties on your page (alternateEventImage and eventImage) are they of the type "Media picker"?
Or are you using another data type for these two fields? Like eg. the "Upload"-type maybe?
If you are usign the upload data type we do not need to use the xslt extension, but you can just do like this:
It was just a thought, and maybe I'm totally off here, but just wanted to make sure which data type you are using.
/Kim A
Hi Kim
I did port s reply to this the other day, but it didn't seem to post properly.
I was using Upload rather than Media Picker, which I don't think I'd have much use for.
Thanks for soling this for me, and I'm sorry if I've wasted your time with something that ended up being so straightforward: I guess I'll have to learn to be much more specific.
Robin
No worries Robin. I'm glad you got it solved. Usually I use the media picker, and I don't know why, but I just supposed that you did too :)
Anyway, now you have the solution when using either the media picker or the upload data type :)
/Kim A
is working on a reply...