Scala XML API: Why allow NodeSeq as attribute values?

Posted by Synesso on Stack Overflow See other posts from Stack Overflow or by Synesso
Published on 2011-01-07T03:16:37Z Indexed on 2011/01/07 3:54 UTC
Read the original article Hit count: 188

Filed under:
|
|

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

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

This means you can assign XML as an attribute value.

scala> <a b={<z><x/></z>}/>.attribute("b")            
res16: Option[Seq[scala.xml.Node]] = Some(<z><x></x></z>)

scala> <a b={<z><x/></z>}/>.attribute("b").map(_ \ "x")
res17: Option[scala.xml.NodeSeq] = Some(<x></x>)

scala> new xml.PrettyPrinter(120, 2).format(<a b={<z><x/></z>}/>)
res19: String = <a b="<z><x></x></z>"></a>

This seems funky to me. I've never seen XML as attribute values in the real world. Why is it allowed? Why is an attribute value simply not of type String?

© Stack Overflow or respective owner

Related posts about Xml

Related posts about scala