2010-01-12 139 views
0

我使用,我已经创建<xsl:template>标签,它不使用<xsl:for-each>声明了一些验证和设置的<xsl:variable><xsl:param>价值真或假的XSLT条件。XSLT:模板标签变量和使用打破for-each循环

  1. 如果条件为真,是否有任何方法可以打破for-each中的语句?
  2. 我们可以使用来自主调用例程的Template变量或param的值吗?

例子:

<!-- Main Xslt --> 
<xsl:template> 
    <xsl:call-template name ="TestTemplate"> 
    <!-- 
     Here I want to use the variable or param that 
     is defined in TestTemplate, is it possible? 
    --> 
    </xsl:call-template> 
</xsl:template> 

<xsl:template name ="TestTemplate"> 
    <xsl:param name="eee"/> 
    <xsl:for-each select ="//RootNode/LeafNode"> 
    <xsl:choose> 
     <xsl:when test ="@Type='ABC'"> 
     <xsl:value-of select ="true"/> 
     </xsl:when> 
     <xsl:otherwise>false</xsl:otherwise> 
    </xsl:choose> 
    </xsl:for-each> 
</xsl:template> 

回答

0

广告1.我认为这是不可能的,但我不知道

广告2.是的,你可以使用参数,但它关注,因为它是恒定的。 XSL中的所有变量和参数都是常量。看看W3School - variable

举:

一旦您设置一个变量的值,你不能改变或修改该值!

同样的事情是参数。

可以调用带(恒定)参数模板:

<call-template name="myTemplate"> 
    <xsl:with-param name="name" select="expression"> 
</call-template> 

W3School - with parameter真的是很好的参考页。

3

您的问题:

有什么办法打破本声明的for-each如果条件是真的吗?

不,通常这也是不必要的。 XSLT不是命令式编程语言,命令式的方法在这里并不适用。

你似乎想做什么是表达“找到的第一个<LeafNode>其中@Type='ABC',并返回true或false取决于是否有一个

传统语言要做到这一点的方法是喜欢你的做法:对每个节点,检查条件,如果条件满足,则返回

在XSLT,您只需选择节点使用XPath:

//RootNode/LeafNode[@Type='ABC'] 

任该结果包含一个节点,或者我t不。没有必要为每一个。

我们可以使用来自主调用例程的模板变量或参数的值吗?

不是。变量和参数的范围是严格的。一旦处理离开其父元素,它们就会超出范围。他们也是不变的,一旦宣布他们不能改变。

做你想要的这里的方式是使模板输出所需的值,并捕获它的一个变量:

<xsl:template> 
    <xsl:variable name="returnValue"> 
    <xsl:call-template name="TestTemplate" /> 
    </xsl:variable> 
</xsl:template> 

<xsl:template name="TestTemplate"> 
    <!-- the following expression emits true or false --> 
    <xsl:value-of select=" 
    count(//RootNode/LeafNode[@Type='ABC']) gt; 0 
    " /> 
</xsl:template> 

最后两个提示:

  • 避免'//'操作不惜一切代价。大部分的它的使用是没有必要的
  • 第一,最上面的元素在文档中的时间不是“根节点”,它是“文档元素”

这是一个重要的区别。 “根节点”前前的文档元素,所以上面的XPath应该更像这样(语义上):

/DocumentElement/LeafNode 
^------ *this* slash represents the "root node"