Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用XSL统计SharePoint博客中排名前5位的文章_Sharepoint_Xslt_Blogs_Cqwp - Fatal编程技术网

使用XSL统计SharePoint博客中排名前5位的文章

使用XSL统计SharePoint博客中排名前5位的文章,sharepoint,xslt,blogs,cqwp,Sharepoint,Xslt,Blogs,Cqwp,基本上,我正在编辑ItemStyle.xsl,以使其检索我需要的内容并通过CQWP(内容查询Web部件)显示。我需要它只显示评论最多的前5个帖子。可以通过@NumComments检索注释。我对XSL不够熟悉,不知道如何做,我假设使用count?有什么建议吗 下面是该模板的当前XSL代码,它只显示所有帖子 <xsl:template name="MostCommented" match="Row[@Style='MostCommented']" mode="itemstyle">

基本上,我正在编辑ItemStyle.xsl,以使其检索我需要的内容并通过CQWP(内容查询Web部件)显示。我需要它只显示评论最多的前5个帖子。可以通过@NumComments检索注释。我对XSL不够熟悉,不知道如何做,我假设使用count?有什么建议吗

下面是该模板的当前XSL代码,它只显示所有帖子

<xsl:template name="MostCommented" match="Row[@Style='MostCommented']" mode="itemstyle">
    <xsl:variable name="SafeLinkUrl">
        <xsl:call-template name="OuterTemplate.GetSafeLink">
            <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="DisplayTitle">
        <xsl:call-template name="OuterTemplate.GetTitle">
            <xsl:with-param name="Title" select="@Title"/>
            <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
        </xsl:call-template>
    </xsl:variable>
    <div>
        <a href="{$SafeLinkUrl}" target="{$LinkTarget}" title="{@LinkToolTip}">
            <xsl:value-of select="$DisplayTitle"/>
        </a>
    </div>
</xsl:template>

您需要在与
行的父级匹配的模板中添加此代码

<xsl:apply-templates select="Row[@Style='MostCommented']" mode="itemstyle">
 <xsl:sort select="@NumComments" data-type="number" order="descending"/>
</xsl:apply-templates>
下面是一个完整的示例,说明了这项技术

<xsl:template name="MostCommented" match="Row[@Style='MostCommented']" mode="itemstyle">
 <xsl:if test="not(position() > 5)">
  <!-- Put all already-existing code here -->
 </xsl:if>
</xsl:template> 
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
     <t>
       <xsl:apply-templates select="*">
        <xsl:sort select="@valued"
            data-type="number" order="descending"/>
       </xsl:apply-templates>
     </t>
 </xsl:template>

 <xsl:template match="post">
  <xsl:if test="not(position() >5)">
   <xsl:copy-of select="."/>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>
<t>
   <post valued="10"/>
   <post valued="9"/>
   <post valued="8"/>
   <post valued="7"/>
   <post valued="6"/>
</t>

当此转换应用于以下XML文档时(因为您忘了提供一个!!!):


生成所需的正确结果

<xsl:template name="MostCommented" match="Row[@Style='MostCommented']" mode="itemstyle">
 <xsl:if test="not(position() > 5)">
  <!-- Put all already-existing code here -->
 </xsl:if>
</xsl:template> 
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
     <t>
       <xsl:apply-templates select="*">
        <xsl:sort select="@valued"
            data-type="number" order="descending"/>
       </xsl:apply-templates>
     </t>
 </xsl:template>

 <xsl:template match="post">
  <xsl:if test="not(position() >5)">
   <xsl:copy-of select="."/>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>
<t>
   <post valued="10"/>
   <post valued="9"/>
   <post valued="8"/>
   <post valued="7"/>
   <post valued="6"/>
</t>

我可以直接在CQWP web部件中而不是在ItemStyle.xsl中执行此操作

替换

<property name="QueryOverride" type="string" />

]>

我已经更新了我的答案,添加了一个完整的代码示例。Dimitre,我不确定在哪里添加“应用模板”部分。它会在同一个文档中吗?@zen:在您没有向我们展示的代码中,有一个
xsl:apply templates
指令选择您在问题中提供的模板执行。它可能以多种不同的方式编写,因此我无法告诉您如何定位此指令,因为我没有看到完整的代码,也没有看到源XML文档。您不必添加新的
xsl:apply templates
指令,只需将当前的
更改为
好,谢谢,我会查找的。它是SharePoint,所以很多东西是不可访问或隐藏的,等等@zen:XSLT代码不是隐藏的。此外,您还可以通过一个简单的