在XSLT中设置变量值的其他方法?

时间:2010-10-12 09:30:46

标签: sharepoint xslt variables filter

我在SharePoint 2007 DataFormWebPart上有一个基本的XSLT过滤器:

<xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row[((ddwrt:FormatDateTime(string(@MyDate) ,1061 ,'MM'))=$MyParameter)]"/>

$ MyParameter 来自ASP.NET控件。但是尝试以任何其他方式设置变量值会导致错误:

<xsl:variable name="Rows">
<xsl:value-of select="/dsQueryResponse/Rows/Row[((ddwrt:FormatDateTime(string(@MyDate) ,1061 ,'MM'))=$MyParameter)]"/>
</xsl:variable>

<xsl:variable name="Rows">
/dsQueryResponse/Rows/Row[((ddwrt:FormatDateTime(string(@MyDate) ,1061 ,'MM'))=$MyParameter)]
</xsl:variable>

我得到的错误是:参数1必须返回一个节点集。 - &GT;计数($行)LT; -

最终,我正在努力实现类似的目标:

<xsl:variable name="Rows">
<xsl:choose>
  <xsl:when test="($MyParameter2 = '1')">
    <xsl:value-of select="/dsQueryResponse/Rows/Row[((ddwrt:FormatDateTime(string(@MyDate) ,1061 ,'MM'))=$MyParameter)]"/>
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="/dsQueryResponse/Rows/Row[((ddwrt:FormatDateTime(string(@MyDate) ,1061 ,'MM'))=$otherParameter)]"/>
  </xsl:otherwise>
</xsl:choose>
</xsl:variable>

使用XSLT是否可以实现这一点,还是应该在SharePoint Designer中寻找其他可能性?

1 个答案:

答案 0 :(得分:5)

使用 count() 功能时,它会计算参数中指定的 node-set 中的节点

您尝试构建$Rows变量的另外两种方法是分配字符串,而不是节点集

  1. 当您以第一种方式设置变量时,select语句会从评估的XPATH表达式返回节点集

  2. xsl:value-of会返回一个字符串。因此,当您以第二种方式创建变量时,您将分配节点的字符串值 - 使用XPATH选择的设置。

  3. 将字符串放在xsl:variable内的字符串中,正如您已完成的第三种方式,将该字符串值分配给$Rows变量。虽然该值碰巧是 XPATH表达式,但它不会被评估。它只是将文字字符串“/ dsQueryResponse / Rows / Row [((ddwrt:FormatDateTime(string(@ MyDate),1061,'MM'))= $ MyParameter)]”分配给$Rows变量。

  4. 解决方案:尝试将您的XPATH标准合并到一个select语句中并合并逻辑以测试谓词过滤器中的$MyParameter2值:

    <xsl:variable name="Rows" 
        select="/dsQueryResponse/Rows/Row[
            ((ddwrt:FormatDateTime(string(@MyDate) ,1061 ,'MM'))=$MyParameter) 
            and $MyParameter2='1'
          ] 
      | 
        /dsQueryResponse/Rows/Row[
            ((ddwrt:FormatDateTime(string(@MyDate) ,1061 ,'MM'))=$otherParameter) 
            and $MyParameter2 !=1
           ]" 
     />
    
相关问题