2013-03-26 25 views
-1

首先,我是XSLT的新手。 我正在使用Sharepoint列表,如果有特定区域中的数据,我需要获取链接才能显示。如果某个季度没有数据,我需要有一个标签说明。Xstl全局变量设置为每个并在foreach后使用

所以我所做的就是我创建了一个foreach循环,用于给定年份的同一月份的每个数据。我知道我不能在xslt中重新分配一个变量,但我不知道如何执行我想要的操作。 这是我的代码示例。由于我正在使用Sharepoint,因此我没有访问XML。 :/

<xsl:variable name="DataQ1" select="'False'"/> 
<xsl:variable name="DataQ2" select="'False'"/> 
<xsl:variable name="DataQ3" select="'False'"/> 
<xsl:variable name="DataQ4" select="'False'"/> 
<xsl:for-each select="../Row[generate-id()=generate-id(key('MonthKey', substring(@Date,6,7))[substring('@Date',1,4) = $varYear)][1])]"> 
    <xsl:variable name="currentMonth" select="number(substring(@Date,6,7))"/> 
    <xsl:choose> 
     <xsl:when test="$currentMonth &gt;= 1 and $currentMonth $lt;=4"> 
      <!--set $DataQ1 to true--> 
     </xsl:when> 
     <xsl:when test="$currentMonth &gt;= 4 and $currentMonth $lt;=7"> 
      <!--set $DataQ2 to true--> 
     </xsl:when> 
     <xsl:when test="$currentMonth &gt;= 7 and $currentMonth $lt;=10"> 
      <!--set $DataQ3 to true--> 
     </xsl:when> 
     <xsl:otherwise> 
      <!--set $DataQ4 to true--> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:for-each> 
<div> 
    <xsl:choose> 
     <xsl:when test="$DataQ1= 'True'"> 
      <a> 
       <xsl:attribute name="href"> 
        <xsl:value-of select="www.example.come"/> 
       </xsl:attribute> 
       <xsl:value-of select="'LinkToDataofQ1'"/> 
      </a> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="'There's no data for this quarter.'"/> 
     </xsl:otherwise> 
    </xsl:choose> 
</div> 
+0

请编辑问题并提供确切的(最好是小的)源XML文档和确切的转换结果。另外,请说明转换必须执行的要求(规则/限制)。 – 2013-03-27 04:07:53

回答

1

您在示例代码中使用key函数,但未发布密钥声明。但我认为你可以实现你想要用下面的代码是什么:

<div> 
    <xsl:choose> 
     <xsl:when test="../Row[substring(@Date, 1, 4) = $varYear and substring(@Date, 6, 2) &gt;= 1 and substring(@Date, 6, 2) &lt; 4]"> 
      <a href="http://www.example.com/">LinkToDataofQ1</a> 
     </xsl:when> 
     <xsl:otherwise>There's no data for this quarter.</xsl:otherwise> 
    </xsl:choose> 
</div> 

其他一些注意事项:

  • 在您的测试Q1你写$currentMonth <= 4。我想你想要的是$currentMonth < 4
  • 要从@Date中提取月份,请使用substring(@Date, 6, 7)substring的第三个参数是子字符串的长度,而不是结束索引。所以你可能应该写substring(@Date, 6, 2)
  • 而不是<xsl:value-of select="'string'"/>,你可以简单地写string
+0

谢谢你,这是我正在寻找的东西。并为我的许多错误感到抱歉。 :/很高兴看到你仍然可以理解我想要的东西! – Frederic 2013-03-27 13:53:16