XML 节点与 Scala 中的属性?
备注:请考虑 XPath 语法死在这里,谢谢。
我有 xml 节点(实际上是 HTML),我想获取它的一个属性。
在 C# (HTMLAgilityPack) 中,我可以通过名称获取属性对象。例如,有“a”节点,我可以要求“href”属性。
在 Scala 中,xml.Node 中有“attribute”方法,但这会返回 .. 节点 的序列。属性就是节点?怎么可能有多个同名的属性呢?我完全困惑了。
此外,还有 xml.Attribute 类,但我没有看到它在 xml.Node 中使用。
我有 PiS 书,但 XML 章节非常浅薄。
问题
我应该如何理解请求属性并获取节点集合?
IOW:返回节点的集合的选项而不是返回属性有什么意义?
- 选项 - 如果没有属性,集合应该为空,它是双倍语义
- 集合 - 这意味着可能有多个属性,所以我很好奇在什么情况下我会得到大小>的集合1 个
- 节点——属性是非常简单的实体,为什么这么大材小用并建议属性可以具有树结构
Remark: please consider XPath syntax dead here, thank you.
I have xml node (HTML actually), and I would like to get an attribute of it.
In C# (HTMLAgilityPack) I could get attribute object by name. For example having "a" node I could ask for "href" attribute.
In Scala there is "attribute" method within xml.Node, but this returns a sequence of.. nodes. An attribute is a node? How it is possible to have several attributes with the same name? I am completely puzzled.
Moreover there is xml.Attribute class but I don't see it used in xml.Node.
I have PiS book but XML chapter is very shallow.
The question
How should I understand asking for an attribute an getting collection of nodes?
IOW: what sense is in returning an option of collection of nodes instead of returning attribute?
- option -- if there is no attribute, collection should be empty, it is doubling semantics
- collection -- this implies there are multiple attribute possible, so I am curious in what scenario I get collection of size > 1
- node -- attribute is pretty simply entity, why such overkill and suggesting that attribute can have tree structure
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只想获取属性的值,是吗?在这种情况下,这很简单:
我知道您说过您明确对 XPath 语法不感兴趣,但在这种情况下,它确实相当简洁:
说了所有这些,您应该意识到属性存在很多问题Scala 的内置 XML 处理中的处理。例如,请参阅此、这个和这个。
You just want to get the value of an attribute, yes? In which case that's pretty easy:
I know that you said that you explicitly aren't interested in XPath syntax, but in this instance it really is rather neater:
Having said all of this, you should be aware that there are many problems with attribute handling in Scala's built-in XML handling. See, for example, this, this and this.
我意识到 Paul 的后续回答几乎涵盖了您的问题,但我想补充几点:
Scales 属性问题的直接答案是:
XPath语法
必须返回一个集合,因为它可以是到达匹配属性的任意数量的路径。元素属性本身是 QName 匹配“attr”,意味着没有命名空间和 attr 的本地名称。为了额外的理智,属性 QName 是:
编译器确保没有本地名称,只有 QNames 潜入。
顺便说一句,虽然我理解为什么 Scala XML XPath 之类的语法可能不有趣,但您应该看看基于 XPath 的查询的 Scales。
有基于 XPath 1.0 字符串的查询(尚未推送到非快照版本)和内部 dsl,可以让编译器/ide 帮助您(加上速度更快并直接使用 scala 代码的好处)。
I realise that Paul's follow up answer pretty much covers your question but I'd just like to add a few more points:
Direct answer for the attribute question for Scales is:
giving
The XPath syntax must return a collection as it could be any number of paths that reached a matching attribute. Element Attributes themselves are QName matched "attr" meaning no namespace and localName of attr. For additional sanity an attribute QName is:
The compiler makes sure no local name only QNames creep in.
As an aside, whilst I understand why the Scala XML XPath like syntax is probably uninteresting, you should have a look at Scales for XPath based querying.
There is both XPath 1.0 string based querying (not yet pushed into a non snapshot version) and an internal dsl that lets the compiler / ide help you out (plus the bonus of being far quicker and working with scala code directly).