答案 0 :(得分:7)
要理解的一些事情:
.
指当前节点(又名“上下文节点”)|
)从不复制节点,即(.|.)
导致一个节点,而不是两个self::
轴(例如self::*
用于查明节点是否为元素),但self::@*
不起作用,因此我们必须使用不同的东西知道这一点,你可以说:
../@*
获取当前节点父级的所有属性(所有“兄弟属性”,如果愿意的话)(.|../@*)
将当前节点与它们联合起来 - 如果当前节点是属性,则整体计数不会更改(按照上面的#3)count(.|../@*)
等于count(../@*)
,则当前节点必须是属性节点。答案 1 :(得分:2)
为了完整起见,在XSLT 2.0中你可以做到
<xsl:if test="self::attribute()">...</xsl:if>
答案 2 :(得分:1)
这是如何工作的
count( # Count the nodes in this set
.|../@*) # include self and all attributes of the parent
# this counts all of the distinct nodes returned by the expression
# if the current node is an attribute, then it returns the count of all the
# attributes on the parent element because it does not count the current
# node twice. If it is another type of node it will return 1 because only
# elements have attribute children
=count( # count all the nodes in this set
../@*) # include all attribute nodes of the parent. If the current node is not an
# attribute node this returns 0 because the parent can't have attribute
# children