在Scala中执行复杂的XPath查询 [英] Performing complicated XPath queries in Scala

查看:340
本文介绍了在Scala中执行复杂的XPath查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在scala中使用哪种最简单的API来对文档执行以下XPath查询?

What's the simplest API to use in scala to perform the following XPath queries on a document?

//s:Annotation[@type='attitude']/s:Content/s:Parameter[@role='type' and not(text())]

//s:Annotation[s:Content/s:Parameter[@role='id' and not(text())]]/@type

s 被定义为特定命名空间的昵称)

(s is defined as a nickname for a particular namespace)

我可以在Scala的XML库上找到的唯一文档没有关于执行复杂的真实XPath查询的信息。

The only documentation I can find on Scala's XML libraries has no information on performing complicated real XPath queries.

我以前喜欢 JDOM 用于此目的(在Java中),但由于JDOM不支持泛型,因此在Scala中使用会很痛苦。 (Java的其他XML库在Java中往往更加痛苦,但我承认我不太了解景观。)

I used to like JDOM for this purpose (in Java), but since JDOM doesn't support generics, it will be painful to work with in Scala. (Other XML libraries for Java have tended to be even more painful in Java, but I admit I don't know the landscape real well.)

推荐答案

我想我会轻易拉扯 XOM 。 XOM作者决定不公开子节点集合等等,这有点令人遗憾,但是他们在Java中比在Scala中做更多的工作和更少的优势。 (而且它是一个设计良好的库。)

I think I'm going to go with lightly pimping XOM. It's a bit of a shame the XOM authors decided against exposing collections of child nodes and the like, but they had more work and less advantage to doing so in Java than in Scala. (And it is an otherwise well-designed library.)

编辑:毕竟我最终拉动了JDOM,因为XOM没有编译XPath提前查询。由于这次我的大部分努力都是针对XPath的,所以我能够找到一个能够回避大多数泛型问题的好模型。提出合理的通用版本的方法 getChildren getAttributes 和<$ c不应该太难$ c> getAdditionalNamespaces in org.jdom.Element (通过使用略有改变名称的新方法对库进行操作。)我不认为有一个修复 getContent ,我不确定 getDescendants

I wound up pimping JDOM after all, because XOM doesn't compile XPath queries ahead of time. Since most of my effort was directed towards XPath this time, I was able to come up with a good model that sidesteps most of the generics issues. It shouldn't be too hard to come up with reasonable genericized versions of the methods getChildren and getAttributes and getAdditionalNamespaces in org.jdom.Element (by pimping the library with new methods that have slightly changed names.) I don't think there's a fix for getContent, and I'm not sure about getDescendants.

这篇关于在Scala中执行复杂的XPath查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆