You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在.NET Core中使用XSLT 1.0从路径提取文件名?

Extract Filename from Any File Path in .NET Core XSLT 1.0

Since .NET Core only supports XSLT 1.0 (so we can’t use XSLT 2.0’s tokenize() function), here are a couple of solid ways to pull the filename from any file path—regardless of depth, name, or extension:

1. Use a Recursive XSLT Template (No Scripting Needed)

This approach relies on XSLT 1.0’s core string functions and recursion to strip off directory segments until we’re left with the filename. First, we normalize all path separators to / to handle both Windows (\) and Unix (/) paths, then recursively trim everything before the last separator.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- Recursive template to extract filename -->
  <xsl:template name="get-filename">
    <xsl:param name="file-path"/>
    
    <!-- Normalize all backslashes to forward slashes -->
    <xsl:variable name="normalized-path" select="translate($file-path, '\', '/')"/>
    
    <xsl:choose>
      <!-- If there's still a directory separator, recurse with the rightmost segment -->
      <xsl:when test="contains($normalized-path, '/')">
        <xsl:call-template name="get-filename">
          <xsl:with-param name="file-path" select="substring-after($normalized-path, '/')"/>
        </xsl:call-template>
      </xsl:when>
      <!-- No separators left—this is our filename -->
      <xsl:otherwise>
        <xsl:value-of select="$file-path"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Example usage -->
  <xsl:template match="/">
    <result>
      <!-- Test with a Windows path -->
      <windows-filename>
        <xsl:call-template name="get-filename">
          <xsl:with-param name="file-path" select="'C:\Users\Dmitry\Documents\project-report.docx'"/>
        </xsl:call-template>
      </windows-filename>
      <!-- Test with a Unix path -->
      <unix-filename>
        <xsl:call-template name="get-filename">
          <xsl:with-param name="file-path" select="'home/dmitry/projects/app/main.exe'"/>
        </xsl:call-template>
      </unix-filename>
    </result>
  </xsl:template>

</xsl:stylesheet>

2. Embed C# Code with msxsl:script (Leverage .NET's Built-in Path Handling)

If you’re okay with enabling script execution in your XSLT transform, you can directly use .NET’s System.IO.Path.GetFileName() method—it handles all edge cases (like trailing separators, empty segments) out of the box, which is way more robust than rolling your own regex or string logic.

XSLT Code:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:utils="http://your-namespace.com/utils">

  <!-- Embed C# function to get filename -->
  <msxsl:script language="C#" implements-prefix="utils">
    <![CDATA[
    public string ExtractFilename(string path)
    {
        // .NET's Path.GetFileName handles all path formats automatically
        return System.IO.Path.GetFileName(path);
    }
    ]]>
  </msxsl:script>

  <!-- Example usage -->
  <xsl:template match="/">
    <result>
      <filename>
        <xsl:value-of select="utils:ExtractFilename('/var/logs/app/error.log')"/>
      </filename>
    </result>
  </xsl:template>

</xsl:stylesheet>

Enable Script Execution in .NET Core:

When loading the XSLT transform, you need to enable script support via XsltSettings:

var xslt = new XslCompiledTransform();
// Second parameter enables script execution
var settings = new XsltSettings(enableDocumentFunction: false, enableScript: true);
xslt.Load("your-transform.xslt", settings, new XmlUrlResolver());

Quick Note on Regex in XSLT 1.0

While XSLT 1.0 doesn’t have native regex support, you could use the same msxsl:script approach to call .NET’s regex methods (like Regex.Match()), but that’s overkill here—Path.GetFileName() is purpose-built for this exact task and far more reliable.

内容的提问来源于stack exchange,提问作者Dmitry Peredera

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:01:16