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
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.