E4X能否根据任意级别子节点的属性获取父节点的属性?
考虑这个带有“节点”的 XML 片段,它可以具有无限的子节点级别的“子节点”元素。
我想根据其 @id
属性查找任何给定 subnode
的 node
的 @type
属性。例如,如果我的 id 为 9,那么我想从上面返回 type="foo"。
<xml>
<node type="bar">
<subnode id="4">
<subnode id="5"/>
</subnode>
<subnode id="6"/>
</node>
<node type="foo">
<subnode id="7">
<subnode id="8">
<subnode id="9"/>
</subnode>
</subnode>
<subnode id="10"/>
</node>
</xml>
我提出了 E4X,但失败的是:
xml.node.(subnode.(@id == '8')).@type
我可以看出为什么它不起作用。下面的内容更有意义,但语法失败(在 AS3 中):
xml.node.(..subnode.(@id == '8')).@type
如何做到这一点?
Consider this XML snippet with "nodes" which can have unlimited child levels of "subnode" elements.
I want to find @type
attribute of the node
for any given subnode
, based on its @id
attribute. For example, if I have an id of 9 then I want to return the type="foo" from above.
<xml>
<node type="bar">
<subnode id="4">
<subnode id="5"/>
</subnode>
<subnode id="6"/>
</node>
<node type="foo">
<subnode id="7">
<subnode id="8">
<subnode id="9"/>
</subnode>
</subnode>
<subnode id="10"/>
</node>
</xml>
The E4X I have come up with, but which fails is:
xml.node.(subnode.(@id == '8')).@type
I can kind of see why it doesn't work. What would make more sense is the following but the syntax fails (in AS3):
xml.node.(..subnode.(@id == '8')).@type
How can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该能够使用此 E4X 获取类型值:
You should be able to get the type value using this E4X:
尝试这个
编辑:即使这样也应该有效:
Try this
EDIT: Even this should work:
放弃 E4X 后,我使用了“hack”并在 ActionScript 中完成了它。方法如下:
不过看起来很乱。仍然对任何简单的 E4X 解决方案感兴趣。
Having given up on E4X I used a "hack" and did it in ActionScript instead. Here's how:
Seems a mess though. Would still be interested in any plain E4X solution.