Copied to clipboard

Flag this post as spam?

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


  • wolulcmit 357 posts 693 karma points
    Aug 09, 2011 @ 17:34
    wolulcmit
    0

    best way to trim a filename?

    I'm bringing the following from umbraco.library:GetMedia($img,'false')/umbracoFile:
    /media/3410/searchengineland-periodic-table-of-seo.pdf
    which I'd like to trim to
    searchengineland-periodic-table-of-seo.pdf

    I thought ucomponents.strings:PathShortener() might do the trick but no dice.

    do I have to do some regex? is there a neat tool somewhere to acheive this?

    thanks for any help

    - Tim

  • Nigel Wilson 945 posts 2077 karma points
    Aug 09, 2011 @ 22:05
    Nigel Wilson
    0

    Hi there

    One way would be to use the substring-after function - you would have to use it in a nested fashion as per the following code: 


    So the inner substring is trimming the "/media/" bit as this will always be constant, and then the outer substring is returning everything after the next "/"

    I appreciate this is probably not the best solution (flexibility, extensibility, etc) but it works based on your example.

    Cheers

    Nigel

     

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 09, 2011 @ 22:28
    Chriztian Steinmeier
    2

    Hi Tim,

    Nigel's code seems to have been munged by the Forum Monster (TM) - but I'm guessing it's one of two approaches:

    <!-- 1. Umbraco Media items have a fixed pattern, so utilize that -->
    <xsl:value-of select="substring-after(substring-after($filePath, '/media/'), '/')" />
    
    <!-- 2. Recursive template (with tail-recursion, of course :-) -->
    <xsl:template name="extract-filename">
        <xsl:param name="path" select="." />
        <xsl:choose>
            <xsl:when test="not(contains($path, '/'))">
                <xsl:value-of select="$path" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="extract-filename">
                    <xsl:with-param name="path" select="substring-after($path, '/')" />
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>    
    </xsl:template>
    
    FYI: 'false' actually means true() in that GetMedia() call :-)  Use 0 (zero) or false() if you might hit a node that has descendants...

    /Chriztian

  • wolulcmit 357 posts 693 karma points
    Aug 10, 2011 @ 11:58
    wolulcmit
    0

    Awesome, thanks Christian (and thanks Nigel)

    both of these work, the recursive template example is pretty cool, I didn't know about not(contains()) I'm sure that will come in handy in future!

    thanks also for the heads up on the 'false' thing.

    P.S when are you releasing the 10 commandments of xslt book ;)

    - Tim

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Aug 10, 2011 @ 14:26
    Lee Kelleher
    0

    Loving the recursive template Chriztian!

    Thinking I'll add some extra functions to the uComponents' IO extensions! ;-) (e.g. Path.GetExtension, Path.GetFileName, Path.GetFileNameWithoutExtension, Path.GetDirectoryName)

    Cheers, Lee.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 10, 2011 @ 16:07
    Chriztian Steinmeier
    0

    @Tim: I might release a stone version, a wood version and possibly an EPUB - but definitely an XML version :-)

    @Lee: Those would probably be great additions!

    /Chriztian

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Aug 10, 2011 @ 16:17
    Lee Kelleher
    0

    It has been done.

    @Chriztian - I owe you an email from last week ... I will get around to it! :-)

Please Sign in or register to post replies

Write your reply to:

Draft