.attribute("b") re...">

Scala XML API: why allow NodeSeq as attribute values?

It seems the attribute values ​​are of type Seq[Node] .

 scala> <ab="1"/>.attribute("b") res11: Option[Seq[scala.xml.Node]] = Some(1) 

This means that you can assign XML as an attribute value.

 scala> <ab={<z><x/></z>}/>.attribute("b") res16: Option[Seq[scala.xml.Node]] = Some(<z><x></x></z>) scala> <ab={<z><x/></z>}/>.attribute("b").map(_ \ "x") res17: Option[scala.xml.NodeSeq] = Some(<x></x>) scala> new xml.PrettyPrinter(120, 2).format(<ab={<z><x/></z>}/>) res19: String = <ab="<z><x></x></z>"></a> 

It seems scared to me. I have never seen XML as attribute values ​​in the real world. Why is this allowed? Why is the attribute value simply not of type String ?

+7
source share
1 answer

From the scala.xml “draft” book from Burak Emir:

start quote

At first glance it seems that attributes should be only strings and nothing else. However, there are two reasons for allowing the same node (except element nodes) that can be displayed in XML: data values ​​and object references.

 <foo name= "s&uuml;ss" life={Atom(42)}> 

end quote

Now I tried this in 2.8.0 and it doesn’t quite compile - I need to use new Atom(42) . But I can print something like this:

 <foo name={List(Text("s"), EntityRef("uuml"), Text("ss"))}/> 

So, that was part of the rationale for using nodes for attributes. And yes, it's a little funky.

+4
source

All Articles