【问题标题】:Alternative ways for setting variable value in XSLT?在 XSLT 中设置变量值的替代方法?
【发布时间】:2010-10-12 09:30:46
【问题描述】:

我在 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 必须返回一个节点集。 -->count($Rows)

最终,我正在尝试实现类似的目标:

<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 中寻找其他可能性?

【问题讨论】:

标签: sharepoint xslt variables filter


【解决方案1】:

当您使用count()函数时,它会统计参数中指定的node-set中的nodes

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

  1. 当您以第一种方式设置变量时,您的 select 语句会从评估的 XPATH 表达式中返回一个 node-set

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

  3. xsl:variable 中放置一个字符串,就像您在第三种方法中所做的那样,将该字符串值分配给$Rows 变量。尽管该值 恰好是一个 XPATH 表达式,但它不会被这样评估。它只是将文字字符串 "/dsQueryResponse/Rows/Row[((ddwrt:FormatDateTime(string(@MyDate) ,1061 ,'MM'))=$MyParameter)]" 分配给 $Rows 变量。

解决方案:尝试将您的 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
       ]" 
 />

【讨论】:

  • +1 好答案。我想这次你可以使用or 运算符来减少表达式。
  • @Indrek,您可能可以通过创建一个密钥 &lt;xsl:key name="rowsByDateTime" match="/dsQueryResponse/Rows/Row" use="ddwrt:FormatDateTime(string(@MyDate) ,1061 ,'MM'))" /&gt; 来提高它的性能;然后使用该密钥:select="key('rowsByDateTime', $MyParameter)[$MyParameter2 = '1'] | key('rowsByDateTime', $otherParameter)[$MyParameter2 !=1]
猜你喜欢
  • 2022-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-21
  • 2010-11-04
  • 1970-01-01
  • 2017-07-17
  • 1970-01-01
相关资源
最近更新 更多