e4x:用破折号过滤子元素/属性?
假设我们有这个 e4x 片段:
zoo = <zoo />;
zoo.animal += <animal animal-type='bear' name='Woofer' />;
zoo.animal += <animal animal-type='panda' name='Ling-Ling' />;
zoo.animal += <animal animal-type='seal' name='Arthur' />;
我可以过滤以 W 或 L 开头的名称:
js>zoo.animal.(@name.match(/^[WL]/))
<animal animal-type="bear" name="Woofer"/>
<animal animal-type="panda" name="Ling-Ling"/>
但如何过滤动物类型?属性“animal-type”中有破折号,因此我无法使用 @animal-type 语法,并且更通用的语法 ['@animal-type'] 似乎不能用作谓词选择器。
有没有办法在 e4x 过滤谓词中选择当前节点?
更新:在这种情况下,我有一个解决方法,我知道所有元素都有一个“name”属性:
js>zoo.animal.(@name.parent()['@animal-type'].match(/^[bp]/))
<animal animal-type="bear" name="Woofer"/>
<animal animal-type="panda" name="Ling-Ling"/>
但这通常不起作用。
更新2:啊!如此接近:
f1=function(attr) {
var b = attr.match(/bear/) != null;
writeln(attr+":"+b);
return b;
}
f2=function(attr) {
var b = attr.match(/bear/) != null;
writeln(attr+":"+b);
return true;
}
js>zoo.*.(f1(@['animal-type']))
bear:true
panda:false
seal:false
js>zoo.*.(f2(@['animal-type']))
bear:true
panda:false
seal:false
<animal animal-type="bear" name="Woofer"/>
<animal animal-type="panda" name="Ling-Ling"/>
<animal animal-type="seal" name="Arthur"/>
WTF?我可以访问 animal-type
属性,如果我返回无条件 true,我可以使用函数作为过滤谓词,但如果我返回比较结果,它似乎会忽略该结果。
Let's say we have this e4x snippet:
zoo = <zoo />;
zoo.animal += <animal animal-type='bear' name='Woofer' />;
zoo.animal += <animal animal-type='panda' name='Ling-Ling' />;
zoo.animal += <animal animal-type='seal' name='Arthur' />;
I can filter on names that begin with W or L:
js>zoo.animal.(@name.match(/^[WL]/))
<animal animal-type="bear" name="Woofer"/>
<animal animal-type="panda" name="Ling-Ling"/>
But how do I filter on animal types? The attribute "animal-type" has a dash in it so I can't use the @animal-type syntax, and the more general syntax ['@animal-type'] doesn't seem to work as a predicate selector.
Is there a way to select the current node in an e4x filtering predicate?
update: I got a hack to workaround in this case where I know all the elements have a "name" attribute:
js>zoo.animal.(@name.parent()['@animal-type'].match(/^[bp]/))
<animal animal-type="bear" name="Woofer"/>
<animal animal-type="panda" name="Ling-Ling"/>
But this wouldn't work in general.
update 2: Argh! So close:
f1=function(attr) {
var b = attr.match(/bear/) != null;
writeln(attr+":"+b);
return b;
}
f2=function(attr) {
var b = attr.match(/bear/) != null;
writeln(attr+":"+b);
return true;
}
js>zoo.*.(f1(@['animal-type']))
bear:true
panda:false
seal:false
js>zoo.*.(f2(@['animal-type']))
bear:true
panda:false
seal:false
<animal animal-type="bear" name="Woofer"/>
<animal animal-type="panda" name="Ling-Ling"/>
<animal animal-type="seal" name="Arthur"/>
WTF? I can access the animal-type
attribute, and if I return an unconditional true, I can use a function as a filter predicate, but if I return the result of a comparison, it seems to ignore that result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
啊——当我运行时,Javascript 解释器处于某种奇怪的状态。如果我从头开始,这是有效的:
如果它返回一个结果,我还必须小心:
如果 XML 节点没有任何后代元素或文本内容,那么它们看起来“不可见”+您需要调用 toXMLString() 来转换它们到一个字符串。
Argh -- the Javascript interpreter was in some weird state when I was running. Here's what works if I start from scratch:
I also have to be careful if it returns one result:
XML nodes look "invisible" if they don't have any descendent elements or text content + you need to call toXMLString() to convert them to a string.