JavaScript / Rhino:我可以在 E4X 查询中使用正则表达式来仅选择某些节点吗?

发布于 2024-08-07 18:13:58 字数 539 浏览 4 评论 0原文

我正在研究 Rhino (Mirth),并且我必须处理/解析具有以下结构的 XML:

<root>
<row>
<foo-1 />
<foo-2 />
<foo-3 />
<foo-4 />
...
<bar-1 />
<bar-2 />
<bar-3 />
<bar-4 />
...
<something-else-1 />
<something-else-2 />
</row>
</root>

我只想获取所有“foo”节点,如果可能的话避免使用循环。我一直在尝试类似的东西:

xml.row.(new RegExp("foo[1-4]").test()))

以及同一行的一些变体,但它似乎不起作用。是否有任何 E4X 语法/方法可以完成此任务?我已经在谷歌上搜索了一段时间,并且阅读了 ECMAS 文档,但我无法完成此操作。

提前致谢!

I'm working on Rhino (Mirth), and I've got to process / parse an XML with the following structure:

<root>
<row>
<foo-1 />
<foo-2 />
<foo-3 />
<foo-4 />
...
<bar-1 />
<bar-2 />
<bar-3 />
<bar-4 />
...
<something-else-1 />
<something-else-2 />
</row>
</root>

I want to get all the "foo" nodes only, avoiding the use of loops if possible. I've been trying somenthing like:

xml.row.(new RegExp("foo[1-4]").test()))

and a few variations of the same line, but it does not seem to work. Is there any E4X syntax / method to get this done?? I've been googling for a while, and i've read the ECMAS documentation, but i can't get this done.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

短暂陪伴 2024-08-14 18:13:58
xml.row.*.( /foo-[1-4]/.test(function::localName()) )
xml.row.*.( /foo-[1-4]/.test(function::localName()) )
缺⑴份安定 2024-08-14 18:13:58

顺便说一句,你也可以不用正则表达式来做到这一点。

var foo = xml.row.*.(String(localName()).indexOf("foo-") == 0);
alert(foo.length() + "\n" + foo.toXMLString());

Btw, you can do it without regex too.

var foo = xml.row.*.(String(localName()).indexOf("foo-") == 0);
alert(foo.length() + "\n" + foo.toXMLString());
一口甜 2024-08-14 18:13:58

这就是您应该能够做到的方式,但它在Rhino中不起作用:

xml.row.*.( /foo-[1-4]/.test(name()) ) // or localName()

目前,据我所知,您将需要使用循环。

如果您不介意使用数组而不是 XMLList,并且您的 Rhino 版本支持数组理解,您可以使用这个:

[foo for each (foo in xml.row.*) if (/foo-[1-4]/.test(foo.name()))]

它仍然使用循环,但至少更具声明性。

编辑:就像 Elijah Gray 在评论中提到的那样,如果您使用命名空间,则需要调用 localName()name().localName。当然,为了完全正确,您还需要比较命名空间 URI。

另外,显然,SpiderMonkey (Firefox) 要求您在过滤器谓词中使用 function:: 为函数调用添加前缀,因此过滤器将如下所示:

/foo-[1-4]/.test(function::name()) // or function::localName()

通常,Rhino 遵循 SpiderMonkey 的做法,因此当它支持函数时调用谓词时,您可能需要 function:: 前缀。

This is how you should be able to do it, but it doesn't work in Rhino:

xml.row.*.( /foo-[1-4]/.test(name()) ) // or localName()

For now, as far as I know, you will need to use a loop.

If you don't mind getting an array instead of an XMLList, and your version of Rhino supports array comprehensions, you could use this:

[foo for each (foo in xml.row.*) if (/foo-[1-4]/.test(foo.name()))]

It still uses a loop, but it's a little more declarative at least.

Edit: Like Elijah Grey mentioned in the comments, if you are using namespaces you would need to either call localName() or name().localName. Of course, to be completely correct, you would need to compare the namespace URI as well.

Also, apparently, SpiderMonkey (Firefox) requires you to prefix function calls with function:: in filter predicates, so the filter would look like this:

/foo-[1-4]/.test(function::name()) // or function::localName()

Typically, Rhino follows what SpiderMonkey does, so when it supports function calls in predicates, you may need the function:: prefix.

小帐篷 2024-08-14 18:13:58

A bug has been opened against Rhino relating to this behavior. If you feel brave, you may want to try out the patch proposed by Martin Blom, as noted in the comments to the bug.

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