I realise that Paul's follow up answer pretty much covers your question but I'd just like to add a few more points:
- I personally don't like the design of Scala XML, to the extent that I wrote an alternative library Scales Xml, but I wouldn't call it badly designed. Design elements of it are apparently also good enough to form the basis of Anti-Xml's approach (Elements owning their children, a concept of grouping nodes etc), but there are many quirks - attribute and text as containers being a large one.
- I've only recently committed descendant axis to Scales - its greedy nature works differently than descendant-or-self - as per the spec //para1 does not mean the same as the location path /descendant::para1
- I'm not sure you can attribute bad design to Anti-Xml either for its absence, its a young project (just over seven months old?) and they may simply not have gotten round to adding descendant yet.
Direct answer for the attribute question for Scales is:
val pre = Namespace("uri:test").prefixed("pre")
val elem = Elem("fred"l, emptyAttributes +
("attr", "value") +
Attribute(pre("attr"), "value"))
println("attributes are a map " + elem.attributes("attr"))
println("attributes are a set " + (
elem.attributes + ("attr", "new value")))
val xpath = top(elem) \@ pre("attr")
xpath foreach{ap => println(ap.name)}
giving
[info] attributes are a map Some(Attribute({}attr,value))
[info] attributes are a set ListSet(Attribute({}attr,new value), Attribute({uri:test}attr,value))
[info] {uri:test}attr
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:
type AttributeQName = EitherLike[PrefixedQName, NoNamespaceQName]
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).