通过XSL中的多个递归模板继承属性值
假设我有一个如下所示的 XML 文件:
<a id="x">
<b>
<b>
<a id="y">
<b>
<c />
</b>
</a>
</b>
<c>
<a id="z">
<b>
<c />
</b>
</a>
</c>
</b>
</a>
经过几个小时的工作,我终于得到了它,以便“a”、“b”和“c”模板正确递归。我可能剩下的一个问题是我需要 a/@id
的值在 b
和 c
中可用。但我还需要能够在该值到达内部 a
时重置该值。 (也就是说,在 a#x > b > b > a#y > b > c
中,我需要值“y”,但在 a#x > b > b
我需要值“x”。换句话说,我需要a/@id
的最新值。)
事实是,问题变得复杂了。 b
元素可以嵌套不确定的次数,因此这不仅仅是执行 ../../@id
或其他操作的情况。
由于变量或参数似乎不适用于此目的,因此我认为可接受的解决方法是自动将 @id
向下传递到每个子元素。但是,我无法找到使用 xsl:attribute 来执行此操作的方法,因为这似乎总是修改输出树,而不是输入或当前正在处理的树。
有什么建议吗?
Let's say I have an XML file that looks like this:
<a id="x">
<b>
<b>
<a id="y">
<b>
<c />
</b>
</a>
</b>
<c>
<a id="z">
<b>
<c />
</b>
</a>
</c>
</b>
</a>
After many hours of working, I've finally gotten it so that the "a", "b", and "c" templates recurse properly. The one probably I have left is that I need the value of a/@id
to be available in b
and c
. But I also need to be able to reset that value when it gets to the inner a
. (That is, in a#x > b > b > a#y > b > c
I need the value 'y', but in a#x > b > b
I need the value 'x'. In other words, I need the most ancestorally-recent value of a/@id
.)
The problem is complicated by the fact that the b
elements can be nested an indeterminate number of times, so it's not simply the case of doing ../../@id
or something.
Since it doesn't seem like variables or parameters would work for this, I was thinking an acceptable workaround would be to automatically carry the @id
down to each child element. However, I couldn't figure out a way to do this using xsl:attribute
, as that always seems to modify the output tree, rather than the input or currently-processing tree.
Any tips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能只做
ancestor::a[first()]/@id
吗?Can't you just do
ancestor::a[first()]/@id
?