XSL:只有文件名,没有路径

2023-12-27

我是 XSL 编程新手,我想这是一个简单的问题: 如何获取没有路径的文件名? 目前我的代码如下所示,我得到了整个路径:结果.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<All_Results>
    <Result>
      <id>1</id>
      <workid>144</workid>
      <rank>100000000</rank>
      <title>Test Dokument</title>
      <author_multival>Test</author_multival>
      <author>Test</author>
      <size>34185</size>
      <url>https://test.test.com/docs/globalit/Lists/Documents/Datumtest/Test.docx</url>
      <urlEncoded>https%3A%2F%2Ftest%2Etest%2Ecom%2Fdocs%2Fglobalit%2FLists%2FDocuments%2FDatumtest%2FTest%2Edocx</urlEncoded>
      <description></description>
      <write>5/3/2013</write>
      <sitename>https://test.test.com/docs/globalit/Lists/Documents/Datumtest</sitename>
      <collapsingstatus>0</collapsingstatus>
      <hithighlightedsummary>Noch mehr text Noch mehr text  <ddd /> </hithighlightedsummary>
      <hithighlightedproperties>
        <HHTitle>Test Dokument</HHTitle>
        <HHUrl>https://test.test.com/docs/globalit/Lists/Documents/Datumtest/Test.docx</HHUrl>
      </hithighlightedproperties>
      <contentclass>STS_ListItem_DocumentLibrary</contentclass>
      <isdocument>True</isdocument>
      <picturethumbnailurl></picturethumbnailurl>
      <serverredirectedurl>https://test.test.com/docs/globalit/_layouts/WordViewer.aspx?id=/docs/globalit/Lists/Documents/Datumtest/Test.docx&amp;DefaultItemOpen=1</serveedirectedurl> 
<doclanguage>German</doclanguage>
  <doctitle>Test</doctitle>
  <docauthor>Team</docauthor>
  <revisiondate>5/3/2013</revisiondate>
  <filename>Test.docx</filename>
  <fileextension>DOCX</fileextension>
  <imageurl imageurldescription="Microsoft Word">/_layouts/images/icdocx.png</imageurl>
</Result>
</All_Results>

结果.xsl

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="TotalResults" />
  <xsl:template match="NumberOfResults" />

  <xsl:template name="DisplayString">
    <xsl:param name="str" />
    <xsl:if test='string-length($str) &gt; 0'>
      <xsl:value-of select="$str" />
    </xsl:if>
  </xsl:template>

  <xsl:template name="HitHighlighting">
    <xsl:param name="hh" />
    <xsl:apply-templates select="$hh"/>
  </xsl:template>

  <xsl:template name="ResultPreviewToolTip">
    <xsl:param name="contentclass" />
    <xsl:param name="picturethumbnailurl" />
    <xsl:param name="url" />
    <xsl:param name="title" />
    <xsl:param name="hithighlightedsummary" />
    <xsl:param name="description" />
    <xsl:param name="version" />

    <xsl:choose>
      <xsl:when test="$contentclass[. = 'STS_ListItem_PictureLibrary'] and $picturethumbnailurl[. != '']">
        <div>
          <a href="{$url}" title="{$title}">
            <img src="{$picturethumbnailurl}" alt="" />
          </a>
        </div>
      </xsl:when>
      <xsl:when test="contains( $url, 'jpg' ) or contains( $url, 'jpeg' ) or contains( $url, 'gif' ) or contains( $url, 'JPG' ) or contains( $url, 'JPEG' ) or contains( $url, 'GIF' )">
        <div>
          <img src="/_layouts/AssetUploader.aspx?Size=Medium&amp;ImageUrl={$url}" alt="" />
        </div>
      </xsl:when>
      <xsl:otherwise>
        <div>
          <xsl:choose>
            <xsl:when test="$hithighlightedsummary[. != '']">
              <b>Preview:</b>
              <br/>
              <xsl:call-template name="HitHighlighting">
                <xsl:with-param name="hh" select="$hithighlightedsummary" />
              </xsl:call-template>
            </xsl:when>
            <xsl:when test="$description[. != '']">
              <b>Preview:</b>
              <br/>
              <xsl:value-of select="$description"/>
            </xsl:when>
            <xsl:otherwise>
              No preview available
            </xsl:otherwise>
          </xsl:choose>
          <xsl:call-template name="DisplayString">
            <xsl:with-param name="str" select="$version" />
            <xsl:with-param name="text" select="'Version: '" />
            <xsl:with-param name="stringcolor" select="'#808080'" />
          </xsl:call-template>

        </div >
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="Result">
    <xsl:variable name="tdClass">
      <xsl:if test="(position() mod 2 = 0)">
        <xsl:value-of select="'even'" />
      </xsl:if>
      <xsl:if test="(position() mod 2 = 1)">
        <xsl:value-of select="'odd'" />
      </xsl:if>
    </xsl:variable>

    <tr>
      <td class="{$tdClass}">
        <a href="#" class="tooltip">
          <img>
            <xsl:attribute name="src">
              <xsl:value-of select="imageurl"/>
            </xsl:attribute>
          </img>
          <span>
            <xsl:call-template name="ResultPreviewToolTip">
              <xsl:with-param name="contentclass" select="contentclass" />
              <xsl:with-param name="description" select="description" />
              <xsl:with-param name="hithighlightedsummary" select="hithighlightedsummary" />
              <xsl:with-param name="picturethumbnailurl" select="picturethumbnailurl" />
              <xsl:with-param name="title" select="title" />
              <xsl:with-param name="url" select="url" />
              <xsl:with-param name="version" select="version" />
            </xsl:call-template>
          </span>
        </a>
      </td>
      <td>
        <a>
          <xsl:attribute name="href">
            <xsl:value-of select="url" disable-output-escaping="yes" />
          </xsl:attribute>
          <xsl:value-of select="filename"/>
        </a>
      </td>
      <td class="{$tdClass}">
        <a>
          <xsl:attribute name="href">
            <xsl:value-of select="url" disable-output-escaping="yes" />
          </xsl:attribute>
          <xsl:choose>
            <xsl:when test="doctitle != ''">
              <xsl:value-of select="doctitle"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="title"/>
            </xsl:otherwise>
          </xsl:choose>
        </a>
      </td>      
      <td class="{$tdClass}">
        <xsl:choose>
          <xsl:when test="docauthor != ''">
            <xsl:value-of select="docauthor"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="author"/>
          </xsl:otherwise>
        </xsl:choose>
      </td>
      <td class="{$tdClass}">
        <xsl:value-of select="revisiondate" />
      </td>
      <td class="{$tdClass}">
        <xsl:value-of select="doclanguage"/>
      </td>
      <td class="{$tdClass}">
        <a>
          <xsl:attribute name="href">
            <xsl:value-of select="sitename" disable-output-escaping="yes" />
          </xsl:attribute>
          <img src="/_layouts/images/breadcrumbbutton.png" style="border-style: none" />
        </a>
        <xsl:call-template name="ShowVersionHistory" />
      </td>
    </tr>

  </xsl:template>

  <xsl:template name="ShowVersionHistory">

    <!-- First, encode Url -->
    <xsl:variable name="EncodedUrl">
      <xsl:value-of disable-output-escaping="yes" select="ddwrt:UrlEncode(url)" />
    </xsl:variable>    

    <!--  does only work for office docuemnts -->
    <xsl:if test="string-length(serverredirectedurl) &gt; 0">

      <!-- get web url from office web app link -->
      <xsl:variable name="WebUrl">
        <xsl:value-of select="substring-before(serverredirectedurl, '_layouts')"/>
      </xsl:variable>

      <!-- create link -->
      <xsl:variable name="FinalLink">
        <xsl:value-of select="$WebUrl"/>
        <xsl:text>_layouts/Versions.aspx?FileName=</xsl:text>
        <xsl:value-of select="$EncodedUrl"/>
      </xsl:variable>

      <a href="{$FinalLink}" target="_blank" Title="Version History">
        <img src="/_layouts/images/versions.gif" style="border-style: none" />
      </a>
    </xsl:if> 

  </xsl:template>

  <xsl:template match="/">
    <table class="searchresult">
      <tr>
        <th width="18"></th>
        <th>Filename</th>
        <th>Title</th>
        <th>Author</th>
        <th>Revision Date</th>
        <th>Language</th>
        <th>Actions</th>
      </tr>
      <xsl:apply-templates />
    </table>
  </xsl:template>
</xsl:stylesheet>

我只需要像 Test.dotx 或 Test.xlsx 那样获取它。 我尝试过<xsl:value-of select="substring-after-last($url, '/')" disable-output-escaping="yes" />但出现错误。

我能做些什么?我该如何解决我的问题? 感谢您的支持。 亲切的问候, 马蒂亚斯


如果您坚持使用 XSLT 1.0,您可以使用如下递归模板:

样式表

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

  <xsl:template name="substring-after-last">
    <xsl:param name="string"/>
    <xsl:param name="char"/>

    <xsl:choose>
      <xsl:when test="contains($string, $char)">
        <xsl:call-template name="substring-after-last">
          <xsl:with-param name="string" select="substring-after($string, $char)"/>
          <xsl:with-param name="char" select="$char"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$string"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="/">
    <xsl:call-template name="substring-after-last">
      <xsl:with-param name="string" select="'/var/log/tomcat'"/>
      <xsl:with-param name="char" select="'/'"/>
    </xsl:call-template>
  </xsl:template>

</xsl:stylesheet>

Output

tomcat

但请注意,如果路径中的最后一个字符是/,因为它总是只返回最后一个之后的内容$char。换句话说,对于/var/log/tomcat/(相对于/var/log/tomcat,没有尾随正斜杠),此模板将返回一个空字符串。

要在您自己的代码中使用它,您可以复制粘贴substring-after-last将上面的模板放入您自己的代码中,而不是:

<xsl:attribute name="href">
  <xsl:value-of select="url" disable-output-escaping="yes" />
</xsl:attribute>

你可以使用:

<xsl:attribute name="href">
  <xsl:call-template name="substring-after-last">
    <xsl:with-param name="string" select="url"/>
    <xsl:with-param name="char" select="'/'"/>
  </xsl:call-template>
</xsl:attribute>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

XSL:只有文件名,没有路径 的相关文章

随机推荐

  • 默认加载哪些 ruby​​ 模块?

    直到最近 我还认为所有标准模块 那些可以在http ruby doc org stdlib http ruby doc org stdlib 默认情况下不加载 也就是说 您必须要求您将要使用的每一个 但从内容来看 确实有一些是被加载的 LO
  • 删除网格视图选项

    如何删除 显示分组依据框 和 删除此列 GridView菜单 当我去参加活动时 没有 ShowGridMenu 事件 所以对我不起作用 Use the GridView PopupMenuShowing http documentation
  • Powershell:无法与 .Net 程序集中存储的表单交互

    我只是想学习这个东西 并且将来想在我的一个项目中使用它 我有一个带有简单文本框的小表单 存储在 Net dll C 中 这是我在这个 dll 中的类 其中包含与此表单交互的方法 using System using System Colle
  • 按 HSV/HSB 对颜色列表进行排序

    我希望按 HSV HSB 值对很长的颜色列表进行排序 我想按色调 周六 亮度对它们进行排序 实际上 我需要的是一种方法来根据 HSV 的顺序判断一种颜色是出现在 之前 还是 之后 因为我只是要在 Java 中创建一个compareTo 并使
  • 将内容部署到多个服务器 (EC2)

    我一直在开发基于云 AWS EC2 的 PHP Web 应用程序 当涉及到使用多个服务器 全部在 AWS 弹性负载均衡器下 时 我遇到了一个问题 在一台服务器上 当我上传最新文件时 它们会立即在整个应用程序中投入使用 但当使用多个服务器时
  • django.core.exceptions.ImproperlyConfigured:加载 MySQLdb 模块时出错:

    我正在关注 django 教程 很多人都问过这个问题 但我认为我的情况有点独特 因为安装 python mysql 后 当我尝试执行 python manage pysyncdb 时 我仍然收到此错误 我在 virtualenv 中 因为我
  • 从数组中选取随机字符串

    我如何从数组中随机选择一个字符串 但不选择相同的字符串两次 string names image1 png image2 png image3 png image4 png image5 png 这可能吗 我正在考虑使用 return st
  • 从指向某个成员的指针获取指向对象的指针

    假设有一个结构 struct Thing int a bool b 我得到一个指向成员的指针b该结构的 例如某个函数的参数 void some function bool ptr Thing thing 如何获得指向包含对象的指针 最重要的
  • grunt 任务完成后运行命令?

    我想要运行命令 https stackoverflow com questions 10456865 running a command in a grunt task but after任务在咕噜声中完成 uglify compile o
  • 如果实现 __getattribute__ 有没有办法访问形式参数

    好像 getattribute 只有 2 个参数 self name 然而 在实际的代码中 我拦截的方法实际上带有参数 无论如何可以访问这些参数吗 Thanks Charlie 获取属性 只是返回所请求的属性 如果是方法 则返回 call
  • 从其他进程读取和写入

    我希望能够从另一个进程的内存中读取和写入 我调用了这些函数Readprocessmemory and WriteProcessmemory from Kernel32 dll我用了GetProcessByName 函数来查找进程 这样就成功
  • 如何从 Rails 控制台使用 Devise 登录用户?

    加载Rails控制台后 我应该如何登录用户 Devise 提供了一个可以在测试中使用的测试助手 我尝试在控制台中使用 gt gt include Devise TestHelpers gt gt helper sign in User fi
  • 如何将异常从一个进程传递到另一个进程?

    如果停止函数中的运行状态为 停止 我想在上传函数中引发异常 这似乎不起作用 我正在使用 Pipe 来传递异常 怎么了 def upload instances u1 for instance in instance try u1 recv
  • iOS/Android 检测和重定向

    js新手 慢慢来 D 需要根据用户使用的操作系统进行重定向 如果ios重定向到x 如果android重定向到y 否则 留在原来的地址 我的问题 这些片段够了吗
  • 从 Shell 输出生成文档

    有没有一种方法 工具可以直接从我的 Shell 输出甚至保存的日志生成 HTML 文档 类似于 doxygen 的作用 如果没有可用的东西 你们对如何使用现有工具做到这一点有什么创意吗 我想 在打字时 我可以放置某种标记或特殊字符 然后让一
  • C# 从一个列表中减去另一个列表或检查一个列表是否完全包含在另一个列表中

    如何从一个列表中减去另一个列表 List
  • Python 3.5+ 中类型提示的动态检查[重复]

    这个问题在这里已经有答案了 The typing https docs python org 3 library typing html模块在 Python 3 5 中实现类型提示 然而 这并不是强制执行的 它目前似乎只是为了静态类型检查器
  • 在 Google People API 中未给出的特定时间后更新联系人条目的选项

    我们使用 Google Contacts API 获取在特定时间后更新的联系人条目 以便在我们端保留联系人的更新副本 在 Google Contacts API 中 有一个选项可以使用以下命令在特定时间后更新联系人条目 更新分钟 https
  • 使用 Python 识别 Mac OS X 中的包目录

    Mac OS X Finder 使用 包 的概念使某些文件夹的内容对用户不透明 我在用着os walk 枚举目录树 并且我想跳过对应用程序包等包的枚举 The mdls命令行实用程序可用于检查是否com apple package在里面kM
  • XSL:只有文件名,没有路径

    我是 XSL 编程新手 我想这是一个简单的问题 如何获取没有路径的文件名 目前我的代码如下所示 我得到了整个路径 结果 xml