Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dan 1288 posts 3921 karma points c-trib
    Nov 20, 2009 @ 15:54
    Dan
    0

    Output media picker item in XSLT

    Hi,

    I'm using the repeatable custom content package to iterate through some items on my website homepage.  I've got this working fine for all of the text datatypes in the repeater, but one of the datatypes is a mediapicker item and I've no idea how to include this datatype in the output in XSLT.

    The code I have is as follows:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxml="urn:schemas-microsoft-com:xslt"
        xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:tagsLib="urn:tagsLib" xmlns:BlogLibrary="urn:BlogLibrary"
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets tagsLib BlogLibrary ">


    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>

        <!-- Input the repeatable custom contents property alias here -->
        <xsl:variable name="propertyAlias" select="string('promoArea')"/>
     
        <xsl:template match="/">
            <xsl:if test="$currentPage/data [@alias = $propertyAlias]/items/item">
                <xsl:for-each select="$currentPage/data [@alias = $propertyAlias]/items/item">
                    <h2>Repeatable Custom Content: Item <xsl:value-of select="position()"/> </h2>
                    <ul>
                        <li>
                            <xsl:value-of select="./data [@alias = 'promoHeader']" disable-output-escaping="yes"/>
                        </li>
                        <li>
                            <xsl:value-of select="./data [@alias = 'promoCopy']" disable-output-escaping="yes"/>
                        </li>
                    </ul>
                </xsl:for-each>
                <!-- Live Editing support for related links. -->
                <xsl:value-of select="umbraco.library:Item($currentPage/@id,$propertyAlias,'')" />
            </xsl:if>
        </xsl:template>

    </xsl:stylesheet>

    Could someone suggest how to do this, or point me in the direction of an example of a mediapicker datatype being called from XSLT in this kind of situation?

    Thanks folks!

  • dandrayne 1138 posts 2262 karma points
    Nov 20, 2009 @ 16:05
    dandrayne
    0

    Try

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

    for the path to the image

    Dan

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Nov 20, 2009 @ 16:08
    Jeroen Breuer
    0

    Maybe the following code helps althoug it's inline xslt:

    <umbraco:Item runat="server" field="mediaItem"
    xslt="concat('&lt;img src=&quot;', umbraco.library:GetMedia({0},'true')/data[@alias='umbracoFile'], '&quot; /&gt;')"
    xsltDisableEscaping="true"/>

     I found it at http://umbraco.org/documentation/books/inline-xslt

  • Dan 1288 posts 3921 karma points c-trib
    Nov 20, 2009 @ 16:26
    Dan
    0

    Thanks, I think I'm almost there - I've tried the following:

    <li>
    <xsl:element name="img">
    <xsl:attribute name="src">
                <xsl:value-of select="umbraco.library:GetMedia($currentPage/data[@alias='promoImage'], 'false')/data[@alias='promoImage']" />
            </xsl:attribute>
        </xsl:element>
    </li>

    But I get an error:

    Error occured
    System.OverflowException: Value was either too large or too small for an Int32.
       at System.Convert.ToInt32(Double value)
       at System.Double.System.IConvertible.ToInt32(IFormatProvider provider)
       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 (XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
       at Root(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
       at Execute(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
    
    at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results)
    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.XmlILCommand.Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, TextWriter results)
    at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, TextWriter results)
    at umbraco.presentation.webservices.codeEditorSave.SaveXslt(String fileName, String oldName, String fileContents, Boolean ignoreDebugging)

    It doesn't seem to be getting a valid integer on this part:

    $currentPage/data[@alias='promoImage']

    Any ideas?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Nov 20, 2009 @ 16:31
    Jeroen Breuer
    0

    I think it should be:

    <li>
       
    <xsl:element name="img">
           
    <xsl:attribute name="src">
               
    <xsl:value-of select="umbraco.library:GetMedia($currentPage/data[@alias='promoImage'], 'false')/data[@alias='umbracoFile']" />
           
    </xsl:attribute>
       
    </xsl:element>
    </li>
  • dandrayne 1138 posts 2262 karma points
    Nov 20, 2009 @ 16:32
    dandrayne
    0

    It tests your xslt against the root node, do if the property doesn't exist on your root node you get the error

    Try

    <xsl:if test="string($currentPage/data[@alias='promoImage']) != ''>
    <li>

       
    <xsl:element name="img">
           
    <xsl:attribute name="src">
               
    <xsl:value-of select="umbraco.library:GetMedia($currentPage/data[@alias='promoImage'], 'false')/data[@alias='promoImage']" />
           
    </xsl:attribute>
       
    </xsl:element>
    </li>
    </xsl:if>

     

    Dan

  • dandrayne 1138 posts 2262 karma points
    Nov 20, 2009 @ 16:34
    dandrayne
    0

    @jeroen

    oops, didn't scroll and check that!

    <xsl:if test="string($currentPage/data[@alias='promoImage']) != ''>
    <li>

       
    <xsl:element name="img">
           
    <xsl:attribute name="src">
               
    <xsl:value-of select="umbraco.library:GetMedia($currentPage/data[@alias='promoImage'], 'false')/data[@alias='umbracoFile']" />
           
    </xsl:attribute>
       
    </xsl:element>
    </li>
    </xsl:if>

    Is correct. 

    @Dan You're basically using getMedia to get the media node by passing in the id contained in "promoImage", then if you consider  the whole

    umbraco.library:GetMedia($currentPage/data[@alias='promoImage'], 'false')

    to be a node,

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

    Gets the part of the media node containing the url

    Dan

  • Dan 1288 posts 3921 karma points c-trib
    Nov 20, 2009 @ 16:34
    Dan
    0

    Same error :(

  • Dan 1288 posts 3921 karma points c-trib
    Nov 20, 2009 @ 16:41
    Dan
    0

    Actually this doesn't error now, but it returns nothing, as if the value of

    string($currentPage/data[@alias='promoImage'])

    Is empty.  It's not though - there is definitely an image in there.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Nov 20, 2009 @ 16:52
    Jeroen Breuer
    0

    Hmm you can test it by the code I provided before:

    <umbraco:Item runat="server" field="promoImage"
    xslt="concat('&lt;img src=&quot;', umbraco.library:GetMedia({0},'true')/data[@alias='umbracoFile'], '&quot; /&gt;')"
    xsltDisableEscaping="true"/>

    Place this on the template of the page and it should display the image...

  • Dan 1288 posts 3921 karma points c-trib
    Nov 20, 2009 @ 16:58
    Dan
    0

    Done that Jeroen, but nothing appears at all (nothing in the HTML at all, not just an incorrect image path etc).  There is an image selected for each item in the repeater - I've double checked by editing them - but it's as if it doesn't recognise that there is anything there.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Nov 20, 2009 @ 17:08
    Jeroen Breuer
    0

    Hmm I don't know what the problem is then. You can check if the id's are really saved in the xml file. In your Umbraco website go to /data/umbraco.config and go to the node in which you placed the mediapicker. Look for <data alias="promoImage"></data> and see if there is a id in it. This should be the id of the selected media item. If it's not there something went wrong with saving the item. Did you publish the page because you need to do that in order to save the data to the xml file.

  • dandrayne 1138 posts 2262 karma points
    Nov 20, 2009 @ 17:09
    dandrayne
    0

    If you're using repeatable custom content, inside the foreach loop it won't be "$currentPage", but

    current()

    Now try

    <xsl:if test="string(current()/data[@alias='promoImage']) != ''>
    <li>

       
    <xsl:element name="img">
           
    <xsl:attribute name="src">
               
    <xsl:value-of select="umbraco.library:GetMedia(current()/data[@alias='promoImage'], 'false')/data[@alias='umbracoFile']" />
           
    </xsl:attribute>
       
    </xsl:element>
    </li>
    </xsl:if>

     

    Dan

  • Dan 1288 posts 3921 karma points c-trib
    Nov 20, 2009 @ 17:13
    Dan
    0

    I'm just trying to debug this a little.  Doing the following:

                            <li>
                                <xsl:element name="img">
                                    <xsl:attribute name="src">
                                        <xsl:value-of select="./data[@alias='promoImage']" />
                                    </xsl:attribute>
                                </xsl:element>
                            </li>

    Does output a valid media node id - it outputs:

    <li><img src="1141" /></li>

    So something is working.  It just seems as if it can't get the file name of the file in that media node.

  • Dan 1288 posts 3921 karma points c-trib
    Nov 20, 2009 @ 17:18
    Dan
    0

    Yay!  Dan, thank you so much (again!) that's done the job.

    There's a missing quote in your solution, but here it is in all its glory for future XSLT strugglers:

    <xsl:if test="string(current()/data[@alias='promoImage']) != ''">
    <li>

       
    <xsl:element name="img">
           
    <xsl:attribute name="src">
               
    <xsl:value-of select="umbraco.library:GetMedia(current()/data[@alias='promoImage'], 'false')/data[@alias='umbracoFile']" />
           
    </xsl:attribute>
       
    </xsl:element>
    </li>
    </xsl:if>

    Thanks you both for your help, it's much appreciated!

  • dandrayne 1138 posts 2262 karma points
    Nov 20, 2009 @ 17:19
    dandrayne
    0

    Have you tried

    <xsl:if test="string(./data[@alias='promoImage']) != ''>
    <li>

       
    <xsl:element name="img">
           
    <xsl:attribute name="src">
               
    <xsl:value-of select="umbraco.library:GetMedia(./data[@alias='promoImage'], 'false')/data[@alias='umbracoFile']" />
           
    </xsl:attribute>
       
    </xsl:element>
    </li>
    </xsl:if>

    using .  is the same as current()

    Dan

  • dandrayne 1138 posts 2262 karma points
    Nov 20, 2009 @ 17:19
    dandrayne
    0

    oops, too late! Glad you got it sorted

    Dan

  • Nico Lubbers 151 posts 175 karma points
    Nov 28, 2009 @ 00:40
    Nico Lubbers
    0

    Hi All,

    I have the same problem with the error. I have a mediapicker and in my XSLT the output of a MediaPicker is a node-id.

    <xsl:variable name="imageFolder" select="$currentPage/data [@alias = 'MediaPicker']"/>
    <xsl:value-of select="$imageFolder"/> <!-- OUTPUTS: 1419 on the page -->

    The above output is 1419. So far so good, but when I put this into the following line:

    <xsl:variable name="mediaFolder" select="umbraco.library:GetMedia($imageFolder, 'true')" />

    I get a terrible error:

    Error occuredSystem.OverflowException: Value was either too large or too small for an Int32.
    at System.Convert.ToInt32(Double value)
    at System.Double.System.IConvertible.ToInt32(IFormatProvider provider)
    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)

     

    I looks like the same issue as this thread.

    The only way to solve it for now, is by hardcoding the following node id. Also the 'true' must be changed to a 1.

    <xsl:variable name="mediaFolder" select="umbraco.library:GetMedia(1419, 1)" />

     

    Do I need to cast something? Looks like the GetMedia does not accept strings as parameter.

    I am using Umb 4.0.2.1

    Nico

  • Micke 7 posts 27 karma points
    Dec 03, 2009 @ 11:32
    Micke
    0

    Hello. I think that maybe the value from mediapicker is not an integer. I tried to put in the value as "numbers" and that worked. Is there any way to convert the @alias id to a integer? (I'm very new to this).

  • dandrayne 1138 posts 2262 karma points
    Dec 03, 2009 @ 12:41
    dandrayne
    0

    In xml, everything is a string so it's not a case of having to cast variables.

    The problem with "the value was too large or small..." is that when saving the xsl parser checks your code against the topmost node. If your top node doesn't have the data type that you're checking against an error will be thrown.  You can get around this by checking the box to disable error checking, but this can bring other problems.

    You basically need to wrap a check around GetMedia, and call it only if there is a value.

    <xsl:if test="string($imageFolder) != ''">
    <xsl:variable
    name="mediaFolder" select="umbraco.library:GetMedia($imageFolder, 'true')" />
    </xsl:if>

    That should get rid if your error.

    Dan

     

  • Micke 7 posts 27 karma points
    Dec 03, 2009 @ 13:15
    Micke
    0

    I must be really bad at this! I have wrapped it in a check and the error message does not show anymore. However i do not get the expected result (a displayed image). If i print there is a value (at the bottom of the code below) displayed. Obviously there is a value. My test shows the value of 1259.

        <xsl:if test="string(./data [@alias='colImgBottom3']) != '' ">
        <img>
            <xsl:attribute name="src">
                <xsl:value-of select="umbraco.library:GetMedia(./data [@alias='colImgBottom3'], 'false')"/>
            </xsl:attribute>
        </img>
        </xsl:if>
        <xsl:value-of select="$currentPage/data [@alias = 'colImgBottom3']"/>

  • dandrayne 1138 posts 2262 karma points
    Dec 03, 2009 @ 13:40
    dandrayne
    0

    To actually get the image url you need the following:

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

    as getMedia gets the whole media node xml, not just the url

    Dan

  • Tommy Poulsen 514 posts 708 karma points
    Dec 03, 2009 @ 13:40
    Tommy Poulsen
    0

    Hi Micke, You need to add the path-part of the media item you fetch:

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

     

    >Tommy

     

     

     

     

     

  • Micke 7 posts 27 karma points
    Dec 03, 2009 @ 14:49
    Micke
    0

    Eureka! That and the @currentPage did it!

    Thank you very much! My final row goes like this:

    <xsl:value-of select="umbraco.library:GetMedia($currentPage/data [@alias='colImgBottom3'], 'false')/data [@alias = 'umbracoFile']"/>
Please Sign in or register to post replies

Write your reply to:

Draft