Copied to clipboard

Flag this post as spam?

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


  • dominik 711 posts 733 karma points
    Aug 30, 2012 @ 09:46
    dominik
    0

    Problem by getting the URL of a media item

    Hello,

    I am trying to get the URL of a macro parameter (mediaCurrent)

    I searched for it and found a lot of information but nothing helps.

    the <xsl:copy-of select="$imageLinkMedia"  /> shows:

    media/80122/brochure.pdfpdf181040

    But how can i get just the url of the document? I tried by using 

    <xsl:value-of select="$imageLinkMedia/data[@alias='umbracoFile']"/>

    But it does not work.

    Thanks

  • dominik 711 posts 733 karma points
    Aug 30, 2012 @ 10:12
    dominik
    0

    now i tried /macro/imageLinkMedia/*/umbracoFile 

    and this works

    Can somebody explain why? :-)

  • Dan 1288 posts 3921 karma points c-trib
    Aug 30, 2012 @ 10:43
    Dan
    0

    Hi Dominik,

    If you output this:

    <textarea><xsl:copy-of select="$imageLinkMedia" /></textarea>

    (The textarea will mean the the XML tags are output as plain text so you can see the full XML.)  You'll see that the media XML contains more than just the file path - it contains file size, extension and other custom properties you may have defined on the media types*.  So the file path is just part of the XML, but it's an important part as it's obviously what's needed to render the path to your media item.

    Hope this helps explain a little.

     

    * Sometimes I'll add a text string property on my image media type, called 'altAttribute' so that I can add alt text for images without having to pass that through the macro as a parameter.

  • dominik 711 posts 733 karma points
    Aug 30, 2012 @ 10:47
    dominik
    0

    HI Dan,

    Thanks,

    But i found some solution which mentions I have to use:

    /macro/imageLinkMedia/node
    /macro/imageLinkMedia/data
    /macro/imageLinkMedia/node/@id

    All were not working (always empty)

    only
    /macro/imageLinkMedia/*/umbracoFile  

    Is working - can you explain why ?

    thanks

  • Dan 1288 posts 3921 karma points c-trib
    Aug 30, 2012 @ 11:00
    Dan
    0

    The XSLT snippet in your original post seems to be for the old (pre Umbraco 4.5) XML schema.  What happened was that Umbraco 4.5 refined the XML structure for the whole Umbraco data-storage, which meant that the XSLT to format data from the XML changed in Umbraco 4.5.  Your original snippet would work for the old XML schema, but not for the new schema; so it's basically old XSLT code that doesn't work any more.

    There are wiki articles about the differences between the old and new schemas and also some examples of the different XSLT used for the different schemas in case you're interested.

  • dominik 711 posts 733 karma points
    Aug 30, 2012 @ 11:10
    dominik
    0

    Hi Dan,

    We are using umbraco 4.7 so the new schema should be included or?

  • Dan 1288 posts 3921 karma points c-trib
    Aug 30, 2012 @ 11:15
    Dan
    0

    Yes, by default all installations from 4.5 will use the new schema.  You can set it to use the old one, but I'm not sure why anyone would want to (unless it was to maintain functionality from a very old installation).

  • dominik 711 posts 733 karma points
    Aug 30, 2012 @ 11:17
    dominik
    0

    Thanks Dan,

    So can you please post how it should be written in the new one?

     

  • Dan 1288 posts 3921 karma points c-trib
    Aug 30, 2012 @ 11:24
    Dan
    0

    My personal approach is to use a media picker on the document type and then pass the value of that media picker through as a numeric parameter in the macro.  From that parameter you can use something like this to display the link:

    <xsl:variable name="mediaId" select="/macro/mediaId"/>

    <xsl:template match="/">
        <xsl:if test="$mediaId > 0">
            <xsl:variable name="mediaNode" select="umbraco.library:GetMedia($mediaId, 0)" />
            <xsl:if test="$mediaNode/umbracoFile">
                <a href="{$mediaNode/umbracoFile}" title="">Download file</a>
            </xsl:if>
        </xsl:if>
    </xsl:template>

     

  • dominik 711 posts 733 karma points
    Aug 30, 2012 @ 11:32
    dominik
    0

    Hi Dan,

    Property is a macro property and not a document type property because this macro is used on many different document types.

    Thanks 

  • Dan 1288 posts 3921 karma points c-trib
    Aug 30, 2012 @ 11:37
    Dan
    0

    Yeah, that should be fine.  All the example above does is bring in a macro property which is the id of your media item, so it doesn't matter where the macro is called from, so long as it contains a property which is the id of the media item.  Does that make sense or have I mis-understood?

  • dominik 711 posts 733 karma points
    Aug 30, 2012 @ 11:47
    dominik
    0

    Hi Dan

    But <xsl:variablename="mediaId"select="/macro/mediaId"/>

    means that macro alias is "mediaId" or? 

    So let us say my macro alias is "imageLink" and there is a property called "imageLinkMedia" (type= mediaCurrent)

     

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 30, 2012 @ 12:02
    Chriztian Steinmeier
    1

    Hi guys,

    There's a subtle difference between using a mediaId property and the mediaCurrent macro parameter, which has bit me more than once:

    * Using a media picker on a document type you will have to use GetMedia() to get the XML (Dan's code works for this scenario)

    * Using mediaCurrent your macro will have the XML already, and you can get it like this:

    <xsl:variable name="media" select="/macro/imageLinkMedia"/>
    
    <xsl:template match="/">
        <xsl:if test="normalize-space($media)">
            <xsl:variable name="mediaNode" select="$media/Image" /><!-- If your media is a custom media type, use its alias here -->
            <xsl:if test="$mediaNode/umbracoFile">
                <a href="{$mediaNode/umbracoFile}" title="">Download file</a>
            </xsl:if>
        </xsl:if>
    </xsl:template>

    /Chriztian

  • dominik 711 posts 733 karma points
    Aug 30, 2012 @ 12:08
    dominik
    0

    HI Chriztian 

    Thanks for your response.

    A few questions to your code. What exactly the "normalize-space" will do?

    Media type can either be a image, a pdf, a doc, a xls or something else

    THanks a lot

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 30, 2012 @ 12:26
    Chriztian Steinmeier
    0

    Hi Dominik,

    The normalize-space() function just makes sure that an element has a value, by collapsing all whitespace.

    If you need to create different output for the edia types, use match templates:

    <xsl:variable name="media" select="/macro/imageLinkMedia"/>
    
    <xsl:template match="/">
        <xsl:if test="normalize-space($media)">
            <!-- Grab the node if it has an umbracoFile property -->
            <xsl:variable name="mediaNode" select="$media/*[umbracoFile]" />
            <!-- Process node whatever it is -->
            <xsl:apply-templates select="$mediaNode" />
        </xsl:if>
    </xsl:template>
    
    <xsl:template match="Image">
        <a href="{umbracoFile}"><img src="{umbracoFile}" width="100" alt="{@nodeName}" /></a>
    </xsl:template>
    
    <xsl:template match="File">
        <a href="{umbracoFile}" title="{@nodeName}">Download file</a>
    </xsl:template>
    
    <!-- Catch any non-specified types -->
    <xsl:template match="*">
        <p>No template for <xsl:value-of select="name()" /> media...</p>
    </xsl:template>
    

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft