我不明白为什么要测试
count(.|../@*)=count(../@*)
(来自Dave Pawson的主页(
识别属性节点:(
有人能给我一个详细的解释吗?
需要理解的几件事:
.
表示当前节点(也称为"上下文节点"(- 属性节点有一个父节点(它所属的元素(
- XPath并集运算(使用
|
(从不重复节点,即(.|.)
导致一个节点,而不是两个 - 理论上可以使用
self::
轴(例如,self::*
可以找出节点是否是元素(,但self::@*
不起作用,所以我们必须使用不同的东西
知道了这一点,你可以说:
../@*
获取当前节点父节点的所有属性(如果愿意,则为所有"同级属性"((.|../@*)
将当前节点与它们联合起来——如果当前节点是一个属性,则总计数不会改变(根据上面的#3(- 因此,如果
count(.|../@*)
等于count(../@*)
,则当前节点必须是属性节点
为了完整起见,在XSLT2.0中可以执行
<xsl:if test="self::attribute()">...</xsl:if>
以下是的工作原理
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