2013-03-08 110 views
0

我沒有看到解決方案,並希望有人能幫助我。我想按標題對結果進行排序。但我不知道在哪裏以及如何將排序放入XSL文件。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> 
<?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> 

回答

0

您必須添加<xsl:sort>(參見:w3schools: xsl:sort Element)。 它應該被添加到您的xsl:apply-templates元素(或者如果您將使用xsl:for-each,那麼在那裏)。

+0

嗨塞巴斯蒂安,感謝您的反饋。你能在我的代碼中給我一個例子嗎?我知道我需要但是在哪裏以及如何? – Smeets 2013-03-08 14:36:09

+0

嗨,我說像這樣\t'的 \t \t的 \t'但我得到了一個錯誤。問題在哪裏,或者我怎樣才能使XSL可行? – Smeets 2013-03-11 06:47:54

+0

您應該分別添加''並在其間包含'xsl:sort'。你能把我從解析器中得到的錯誤信息發給我嗎? – sebastian 2013-03-11 07:45:18