e4x:用破折号过滤子元素/属性?

发布于 2024-10-30 08:25:05 字数 1535 浏览 3 评论 0原文

假设我们有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花落人断肠 2024-11-06 08:25:05

啊——当我运行时,Javascript 解释器处于某种奇怪的状态。如果我从头开始,这是有效的:

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' />;

js>zoo.animal.(@['animal-type'].match(/^[bp]/))
<animal animal-type="bear" name="Woofer"/>
<animal animal-type="panda" name="Ling-Ling"/>
js>zoo.animal.(@['animal-type'].match(/^[ps]/))
<animal animal-type="panda" name="Ling-Ling"/>
<animal animal-type="seal" name="Arthur"/>

如果它返回一个结果,我还必须小心:

js>zoo.animal.(@['animal-type'].match(/^p/))
js>zoo.animal.(@['animal-type'].match(/^p/)).toXMLString()
<animal animal-type="panda" name="Ling-Ling"/>

如果 XML 节点没有任何后代元素或文本内容,那么它们看起来“不可见”+您需要调用 toXMLString() 来转换它们到一个字符串。

Argh -- the Javascript interpreter was in some weird state when I was running. Here's what works if I start from scratch:

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' />;

js>zoo.animal.(@['animal-type'].match(/^[bp]/))
<animal animal-type="bear" name="Woofer"/>
<animal animal-type="panda" name="Ling-Ling"/>
js>zoo.animal.(@['animal-type'].match(/^[ps]/))
<animal animal-type="panda" name="Ling-Ling"/>
<animal animal-type="seal" name="Arthur"/>

I also have to be careful if it returns one result:

js>zoo.animal.(@['animal-type'].match(/^p/))
js>zoo.animal.(@['animal-type'].match(/^p/)).toXMLString()
<animal animal-type="panda" name="Ling-Ling"/>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文