XSL: Just the filename without the path

时光毁灭记忆、已成空白 提交于 2021-02-08 17:01:03

问题


I'm new in XSL programming and I guess it is a simple question: How can I get the filename without the path? At the moment my code looks like this and I get the whole path: Result.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>

result.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>

I only need to get it like Test.dotx or Test.xlsx. I tried sth with <xsl:value-of select="substring-after-last($url, '/')" disable-output-escaping="yes" /> but got an error.

What can I do? How can I solve my problem? Thans for your support. Kind regards, Matthias


回答1:


If you're stuck with XSLT 1.0, you could use a recursive template like this:

Stylesheet

<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

Note, though, that this template probably won't do what you want if the last character in your path is a /, because it'll always just return what's after the last $char. In other words, for /var/log/tomcat/ (as opposed to /var/log/tomcat, without the trailing forward slash), this template would return an empty string.

To use this in your own code, you could copy-paste the substring-after-last template above into your own code and instead of:

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

You could use:

<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>



回答2:


Here is a simple, non-recursive XSLT 1.0 solution:

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

 <xsl:variable name="vDoc" select="document('')"/>
 <xsl:variable name="vNodes" select="$vDoc//node()|$vDoc//@*|$vDoc//namespace::*"/>

 <xsl:template match="/*">
   <xsl:variable name="vLength" select="string-length()"/>
   <xsl:variable name="vCur" select="."/>
     <xsl:for-each select="$vNodes[not(position() > $vLength)]">
      <xsl:if test=
       "substring($vCur,$vLength - position()+1,1)='/'
       and not(contains(substring($vCur,$vLength - position()+2), '/'))
       ">
       <xsl:value-of select="substring($vCur,$vLength - position()+2)"/>
      </xsl:if>
     </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the following XML document:

<t>http://abc.com/seg1/seg2/seg3/seg4/seg5/filename.html</t>

the wanted, correct result is produced:

filename.html


来源:https://stackoverflow.com/questions/15248057/xsl-just-the-filename-without-the-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!