【问题标题】:SharePoint XSL CounterSharePoint XSL 计数器
【发布时间】:2013-08-14 08:22:17
【问题描述】:

所以我一直用头撞墙一段时间,正在寻求帮助。我正在尝试在 sharepoint 设计器中创建一个新的 itemstyle,它基本上检查任务列表中的每个项目,然后计算已完成、进行中和未开始状态的总数。问题是据我所知,xsl 没有可变变量。我到目前为止是这样的:

<xsl:variable name="sChk">
        <xsl:value-of select="@Status"/>
</xsl:variable>
 <xsl:for-each select="@Status">
       <xsl:if test="$sChk = 'Completed' ">
           <!-- Add to Completed Counter -->
       </xsl:if>
       <xsl:if test="$sChk = 'In Progress' ">
               <!-- Add to In Progress Counter -->
       </xsl:if>
       <xsl:if test="$sChk = 'Not Started' ">
           <!-- Add to Not Started Counter -->
       </xsl:if>
        <br/>
    </xsl:for-each> 
    Out Of Loop:      
    Total Completed: <!-- Completed Value -->
    Total In Progress: <!-- In Progress Value -->
    Total Not Started: <!-- Not Started Value -->

任何和所有的帮助将不胜感激,谢谢!

编辑:所以我也尝试过这种递归方法,但这也不起作用......

<xsl:param name="cCount" select="0"/>
<xsl:param name="ipCount" select="0"/>
<xsl:param name="nsCount" select="0"/>

<xsl:choose>
    <xsl:when test="$sChk = 'Completed'">
        <xsl:call-template name="PSRView2.0">
            <xsl:with-param name="cCount" select="$cCount +1"/>
            <xsl:with-param name="ipCount" select="$ipCount"/>
            <xsl:with-param name="nsCount" select="$nsCount"/>              
        </xsl:call-template>
    </xsl:when>
    <xsl:when test="$sChk = 'In Progress'">
        <xsl:call-template name="PSRView2.0">
            <xsl:with-param name="cCount" select="$cCount"/>
            <xsl:with-param name="ipCount" select="$ipCount +1"/>
            <xsl:with-param name="nsCount" select="$nsCount"/>              
        </xsl:call-template>
    </xsl:when>
    <xsl:when test="$sChk = 'Not Started'">
        <xsl:call-template name="PSRView2.0">
            <xsl:with-param name="cCount" select="$cCount"/>
            <xsl:with-param name="ipCount" select="$ipCount"/>
            <xsl:with-param name="nsCount" select="$nsCount +1"/>               
        </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="$cCount"/>
        <xsl:value-of select="$ipCount"/>
        <xsl:value-of select="$nsCount"/>
    </xsl:otherwise>
</xsl:choose>  

【问题讨论】:

    标签: sharepoint xslt cqwp


    【解决方案1】:

    您是正确的,在 XSLT 中变量是不可变的。在你的情况下你需要使用的是 count 函数,它计算节点集中的所有项目。像这样的:

    <xsl:variable name="completed" select="count(task[@Status='Completed'])" />
    <xsl:variable name="inprogress" select="count(task[@Status='In Progress'])" />
    <xsl:variable name="notstarted" select="count(task[@Status='Not Started'])" />
    Total: <xsl:value-of select="$completed + $inprogress + $notstarted" />
    

    当然,您需要将“task”替换为您在 XSLT 中使用的任何元素名称。

    没有看到您的 XML,很难给出准确的答案,但作为示例,请考虑以下 XML

    <tasklist>
       <task status="Completed" />
       <task status="Completed" />
       <task status="In Progress" />
    </tasklist>
    

    那么 XSLT(仅用于获取总数)将如下所示

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
        <xsl:template match="tasklist">
            <xsl:variable name="completed" select="count(task[@Status='Completed'])" />
            <xsl:variable name="inprogress" select="count(task[@Status='In Progress'])" />
            <xsl:variable name="notstarted" select="count(task[@Status='Not Started'])" />
            Total: <xsl:value-of select="$completed + $inprogress + $notstarted" />
        </xsl:template>
    </xsl:stylesheet>
    

    请注意您需要如何定位在此处所有单个“任务”元素的父元素上。作为替代方案,您可以执行以下操作...

    <xsl:variable name="completed" select="count(//task[@Status='Completed'])" />
    

    无论 task 元素在 XML 中的什么位置,它们都会被计算在内。

    如果您真的不知道元素名称,但确定没有其他元素具有“状态”属性,您甚至可以执行以下操作:

    <xsl:variable name="completed" select="count(//*[@Status='Completed'])" />
    

    【讨论】:

    • 愚蠢的问题,我怎么知道我使用的是什么元素名称?
    • 另外,这是否意味着我不需要 for-each 循环?
    • 确实,这里不需要 for-each 循环,因为它只迭代一个属性,并且实际上并不使用循环中的属性值。至于元素名称,我假设您会知道 XML 的结构(毕竟,如果您知道属性称为“状态”,那么您也应该知道它属于哪个元素......)
    • 好吧,我尝试了该代码,但我只是让查询重复了列表中的任务数。由于它是一个任务列表,我假设元素是任务列表还是应该是内容类型?
    • 我已经扩展了我的答案以尝试提供一些进一步的提示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 2019-08-03
    • 1970-01-01
    • 2010-10-16
    • 1970-01-01
    相关资源
    最近更新 更多