E4X는 어느 수준에서나 어린이의 속성에 따라 상위 노드의 속성을 얻을 수 있습니까?
-
07-07-2019 - |
문제
"서브 노드"요소의 무제한 자식 수준을 가질 수있는 "노드"로이 XML 스 니펫을 고려하십시오.
찾고 싶어요 @type
의 속성 node
주어진 경우 subnode
, 그것의 기준 @id
기인하다. 예를 들어, ID가 9 인 경우 위에서 유형 = "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
이 작업은 어떻게 할 수 있습니까?
해결책
이 E4X를 사용하여 유형 값을 얻을 수 있어야합니다.
xml.node.(descendants("subnode").@id.contains("8")).@type;
다른 팁
이 시도
for each(var node:XML in xml.node)
{
var subnodes:XMLList = node..subnode;
if(subnodes.(@id == '9').length() != 0)
return node.@type;
}
편집 :조차도 작동해야합니다.
if(node..subnode.(@id == '9').length() != 0)
E4X를 포기한 후 나는 "해킹"을 사용하고 대신 ActionScript에서 수행했습니다. 방법은 다음과 같습니다.
var p:XML = xml..subnode.(attribute('id').toLowerCase() === "8")[0];
//Traverse back up to the parent "node"
while ( p.name().toString() === "subnode" ) {
p = p.parent();
}
Alert.show(p.@type); //Should say "foo"
그래도 엉망인 것 같습니다. 여전히 일반 E4X 솔루션에 관심이 있습니다.
제휴하지 않습니다 StackOverflow