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

如何用XSLT 2.0(Saxon 6.5.5)提取网络路径中的Name部分

问题

需要从形如\\Server01\Name\的网络路径中提取“Name”部分,使用XSLT 2.0和Saxon 6.5.5。现有XML中Network节点的NetworkPath属性为该路径,当前尝试的XSLT代码返回的是Server01,但目标是获取Name,该如何调整?

XML示例

<?xml version="1.0" encoding="UTF-8"?>
<Example>
    <Network Name="test" NetworkPath="\\Server01\Name\"/>
</Example>

尝试的XSLT代码

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="text" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="/">
    
    <xsl:variable name="extractNetworkPathName">
        <xsl:variable name="input" select="/Example/Network/@NetworkPath"/>        
        <xsl:variable name="url_minus" select="substring-after($input,'\\')"/>

            
             <xsl:value-of select="substring-before($url_minus,'\')"/> 
    
    </xsl:variable>
      <xsl:value-of select="$extractNetworkPathName"/>
   
    </xsl:template>
    

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:transform>
解决方案

你当前的代码只截取了\\之后第一个\之前的内容,也就是服务器名Server01。要获取第二个分段的Name,需要先跳过服务器名部分,再截取目标内容。

基础修改版代码

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="text" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="/">
        <xsl:variable name="input" select="/Example/Network/@NetworkPath"/>
        <!-- 移除开头双反斜杠,得到Server01\Name\ -->
        <xsl:variable name="afterPrefix" select="substring-after($input, '\\')"/>
        <!-- 移除服务器名及后续反斜杠,得到Name\ -->
        <xsl:variable name="afterServer" select="substring-after($afterPrefix, '\')"/>
        <!-- 截取最后一个反斜杠前的内容,得到Name -->
        <xsl:variable name="targetName" select="substring-before($afterServer, '\')"/>
        
        <xsl:value-of select="$targetName"/>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:transform>

健壮性优化版(适配路径末尾有无反斜杠)

利用XSLT 2.0支持的tokenize函数拆分路径,处理更灵活:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="text" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="/">
        <xsl:variable name="input" select="/Example/Network/@NetworkPath"/>
        <!-- 用反斜杠拆分路径,过滤空字符串(开头\\会生成空元素) -->
        <xsl:variable name="pathSegments" select="tokenize($input, '\\')[normalize-space(.)]"/>
        <!-- 取第二个分段,即为目标Name -->
        <xsl:value-of select="$pathSegments[2]"/>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:transform>

内容的提问来源于stack exchange,提问作者Will Webb

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 15:39:21