Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing XML literal as a parameter in Scala

Tags:

xml

scala

I can pass a variable as a multivalued parameter:

scala> <b/>
res26: scala.xml.Elem = <b></b>

scala> Elem(null,"a",Null,TopScope,res26)
res27: scala.xml.Elem = <a><b></b></a>

But I can't pass an XML literal as a multivalued parameter:

scala> Elem(null,"a",Null,TopScope,<b/>)
<console>:12: error: not found: value <
Elem(null,"a",Null,TopScope,<b/>)

But I can pass an XML literal as a simple parameter

scala> def bar(s:String,n:Elem) = s+n.toString
bar: (s: String, n: scala.xml.Elem)java.lang.String
scala> bar("super ", <a/>)
res30: java.lang.String = super <a></a>

?

Thanks

like image 994
Jérôme Avatar asked Sep 26 '11 09:09

Jérôme


1 Answers

Adding a space before the XML element makes it work:

scala> Elem(null, "a", Null, TopScope, <b/>)
resN: scala.xml.Elem = <a><b></b></a>

From the Scala Language Specification, Section 1.5:

In order to allow literal inclusion of XML fragments, lexical analysis switches from Scala mode to XML mode when encountering an opening angle bracket ’<’ in the following circumstance: The ’<’ must be preceded either by whitespace, an opening parenthesis or an opening brace and immediately followed by a character starting an XML name

like image 96
Philippe Avatar answered Nov 01 '22 23:11

Philippe