Accessing XML attributes with namespaces

后端 未结 2 2079
一整个雨季
一整个雨季 2021-02-20 17:23

How one can access attributes with namespaces? My XML data are in a form

val d = 
<
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-20 17:53

    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?

提交回复
热议问题