How one can access attributes with namespaces? My XML data are in a form
val d =
<
The API documentation refers to the following syntax ns \ "@{uri}foo"
.
In your example there is no namespace defined, and it seems Scala considers your attribute as unprefixed. See d.attributes.getClass
.
Now if you do this:
val d = <z:Attachment xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" rdf:about="#item_1"></z:Attachment>
Then:
scala> d \ "@{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about"
res21: scala.xml.NodeSeq = #item_1
scala> d.attributes.getClass
res22: java.lang.Class[_] = class scala.xml.PrefixedAttribute
You can always do
d match {
case xml.Elem(prefix, label, attributes, scope, children@_*) =>
}
or in your case also match on xml.Attribute
d match {
case xml.Elem(_, "Attachment", xml.Attribute("about", v, _), _, _*) => v
}
// Seq[scala.xml.Node] = #item_1
However, Attribute
does not care about the prefix at all, so if you need that, you need to explicitly use PrefixedAttribute
:
d match {
case xml.Elem(_, "Attachment", xml.PrefixedAttribute("rdf", "about", v, _), _, _*) => v
}
There is a problem, however, when there are multiple attributes. Anyone knows how to fix this?