且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从孩子中计算祖父母内部的父节点.XSL

更新时间:2023-01-20 12:39:56

我需要在第一个 <d> 节点 1 中(因为我们在 中并且它在 )

I need to have in first <d> node 1 (because we inside <c> and it is first in <b>)

所以你需要在树上向上到当前节点的 c 祖先,然后计算该元素前面的 c 兄弟节点的数量:

So you need to go up the tree to the current node's c ancestor and then count the number of that element's preceding c siblings:

count(ancestor::c[1]/preceding-sibling::c) + 1

+1 是因为给定 b 中的第一个 c 没有前面的兄弟姐妹,第二个会有一个,依此类推.

The +1 is because the first c in a given b will have no preceding siblings, the second will have one, etc.

为了确保您在树上到达 b 的子元素 c(例如,如果可能存在其他名为 c 的元素)在两者之间)你可以更具体

To ensure you go up the tree to the c that is a child of b (e.g. if there may be other elements named c in between) you can be more specific

count(ancestor::c[parent::b][1]/preceding-sibling::c) + 1

只定位一个 c,它本身有一个 b 作为它的父对象.

to target only a c that itself has a b as its parent.