2011-12-20 57 views
3

我要检查,如果所有的孩子都有一个属性集,如果是在FO添加1个元素:如果所有的孩子都有一个属性

<row> 
    <entry attribute="true"></entry> 
    <entry attribute="true"></entry> 
    <entry attribute="true"></entry> 
</row> 

例;如果每个条目元素都具有attribute = true,则添加一个元素。

<xsl:template match="row"> 
    <fo:table-row> 
    <xsl:apply-template/> 
    </fo:table-row> 

    <xsl:if test=""><!-- What to write here? --> 
    <fo:table-row/> 
    </xsl:if> 
</xsl:template> 
+0

你可能有兴趣在一个更短,更高效的解决方案:) – 2011-12-20 13:20:30

+0

在我的答案需要调整最初的表达,现在已经完成。 – 2011-12-20 13:35:13

回答

6
count(*[@attribute="true"]) = count(*) 
+0

它的工作,谢谢! – user1065283 2011-12-20 11:57:47

0

什么弗朗西斯说,或者类似的东西

count(*[not(@attribute="true")]) 
+0

我喜欢这个,但是你不需要另一个'not()'吗? 'not(count(* [not(@ attribute =“true”)]))' – 2011-12-20 10:26:08

+0

取决于你在检查什么:)我会用'= 0'而不是在这里。毕竟'count'不完全是布尔值。 – 2011-12-20 10:39:09

+0

我想我会跟数字1一起去。谢谢你的回复! – user1065283 2011-12-20 11:59:06

2

这可能是多一点点效率,因为没有所有属性的计数是必要的和评估可以发现立即停止条件为@attribute = 'true'的第一个子元素为false()

<xsl:if test="not(*[not(@attribute = 'true')])"> 
    <fo:table-row/> 
    </xsl:if> 

说明

这是双重否定规则的另一种应用:

for every $x some property-y is true 

等同于:

there isn't any $z in $x such that for $z property-y is not true 
2

或者如果你使用XPath 2.0并且你喜欢使你的代码可读性

<xsl:if test="every $a in * satisfies $a/@attribute='true'"> 
    <fo:table-row/> 
    </xsl:if> 
相关问题