从孩子中计算祖父母内部的父节点.XSL [英] Couunt parent nodes inside grandparent from child. XSL

查看:38
本文介绍了从孩子中计算祖父母内部的父节点.XSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的 xml 文件.

i have an xml file like this.

<a>
    <b>
        <c>
            <d>Text</d>
        </c>
        <c>
            <d>Text</d>
        </c>
    </b>
    <b>
        <c>
            <d>Text</d>
        </c>
        <c>
            <d>Text</d>
        </c>
    </b>
</a>

我在 节点,我需要获取 节点内的 节点数>.请注意,这只是一个示例,因此我们可以拥有更深层次的结构,并且 不会是 的直接父级.所以,我需要什么.我需要在第一个 <d> 节点 1 中(因为我们在 中,它是 中的第一个) 在第二个 2、第三个 1 和第四个 2 中.我们不需要计算所有 元素,而只计算当前 的父元素在父 内.

I'm in <d> node and i need to get the number of <c> node inside <b>. Be aware that this is just a sample so we can have deeper structure and <c> will not be direct parent of <d>. So, what i need. I need to have in first <d> node 1 (because we inside <c> and it is first in <b>) in the second 2, in third 1 and in fourth 2. We don't need to count all <c> elements but only parents of current <d> inside parent <b>.

现在我有计数(前导::c).但它计算了所有 4 个 点头.

For now i have count(preceding::c) . But it counts all 4 <c> nods.

请帮忙.谢谢.

附言我正在使用 xsl 1.0

P.S. I'm using xsl 1.0

推荐答案

我需要在第一个 <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.

这篇关于从孩子中计算祖父母内部的父节点.XSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆