XSL:只是没有路径的文件名

时间:2013-03-06 13:01:58

标签: xslt substring concat

我是XSL编程的新手,我想这是一个简单的问题: 如何在没有路径的情况下获取文件名? 目前我的代码看起来像这样,我得到了整个路径: 的为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>

我只需要像Test.dotx或Test.xlsx一样。 我尝试了<xsl:value-of select="substring-after-last($url, '/')" disable-output-escaping="yes" />,但收到了错误。

我该怎么办?我怎样才能解决我的问题? 有你的支持。 亲切的问候, 的Matthias

2 个答案:

答案 0 :(得分:5)

如果你坚持使用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>

输出

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>

答案 1 :(得分:1)

这是一个简单的非递归XSLT 1.0解决方案

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

对以下XML文档应用此转换时:

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

产生了想要的正确结果:

filename.html