Copied to clipboard

Flag this post as spam?

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


  • Jonas Eriksson 930 posts 1825 karma points
    Dec 12, 2009 @ 20:29
    Jonas Eriksson
    0

    Filename without extension

    Hi!

    I needed a way to get media file names without their extensions. Did not find any quick way in pure xslt. So I made this script, anyone has a more direct way?:

    <xsl:template match="/">
    <!-- start writing XSLT -->
    <xsl:value-of select="prg:pathGetFileNameWithoutExtension('testfilename.foo.xls')"/>
    </xsl:template>
       <msxsl:script language="C#" implements-prefix="prg">
        <msxml:using namespace="System.IO"/>
            <![CDATA[
    public string pathGetExtension(string filename)
    {
        return Path.GetExtension(filename);
    }
    public string pathGetFileName(string filename)
    {
        return Path.GetFileName(filename);
    }
    public string pathGetFileNameWithoutExtension(string filename)
    {
        return Path.GetFileNameWithoutExtension(filename);
    }
        ]]>
    </msxsl:script>
    </xsl:stylesheet>

  • Paul Blair 466 posts 731 karma points
    Dec 13, 2009 @ 10:29
    Paul Blair
    0

    If you've uploaded them into the media section you can access the extension property, which you can do a string replace on to remove. Something like this:

        <xsl:for-each select="umbraco.library:GetMedia(./@id, 'true')/node">
          <xsl:variable name="extension">.<xsl:value-of select="./data[@alias='umbracoExtension']"/></xsl:variable>
          <xsl:variable name="theTitle" select="Exslt.ExsltStrings:replace(@nodeName, $extension, '')"/>

  • Jonas Eriksson 930 posts 1825 karma points
    Dec 13, 2009 @ 11:50
    Jonas Eriksson
    0

    Yes, did not know about that property, thanks, however - if the extension also exist as part of the filename this will not be a good solution. Perhaps better use the length of the extension and remove that number +1 of chars from the end. But the more I use xslt the more I want as short instructions as possible. For readability. Perhaps one should put functions like this in a xslt-dll-extension.

  • Paul Blair 466 posts 731 karma points
    Dec 13, 2009 @ 20:49
    Paul Blair
    0

    The extension variable I created also includes the fullstop (e.g. ".jpg") so as long as the file name is not some like "pic.jpgbig.jpg" it will work OK. Ie. bigpic_jpg.jpg will return bigpic_jpg as the file name.

  • Ivan 15 posts 125 karma points
    Jan 27, 2017 @ 22:40
    Ivan
    0

    How do I do that now? I'm not familiar with XSLT, so I'm looking for a way to do it in Razor, like in this default folder listing code:

        <ul>
            @foreach (var item in selection)
            {
                <li>
                    <a href="@item.umbracoFile">@item.Name</a>
                </li>
            }
        </ul>
    

    Any ideas?

    EDIT: nvm

    just used this expression and it worked

    @String.Format("{0}",Path.GetFileNameWithoutExtension(@item.Name))
    
Please Sign in or register to post replies

Write your reply to:

Draft