This page is no longer maintained — Please continue to the home page at www.scala-lang.org

XML literals - starting with some curly braces

3 replies
Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.

hi,

i would like to use the XML literals but have come across this problem:

trait SessionElement {
def name: String
def toXML: scala.xml.Elem
}

trait SessionElementSeq[ T <: SessionElement ] extends SessionElement {
def elements: List[ T ]

def toXML =
<{name}>
{elements.map(_.toXML)}

}

... here i cannot start the literal using scala code (function 'name')... any chance to overcome this?

thanks, -sciss-

Silvio Bierman
Joined: 2009-02-16,
User offline. Last seen 1 year 16 weeks ago.
Re: XML literals - starting with some curly braces

As I understand it the element label has to be a constant when using an XML
literal. You will have to invoke the Elem constructor explicitly.

Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: XML literals - starting with some curly braces

ok, a pity.

also it's nice to have the \\ attribute extractor. but... there is no way to serialize into attributes it seems:

scala> val age = 32
age: Int = 32

scala> val joe =
:5: error: overloaded method constructor UnprefixedAttribute with alternatives (key: String,value: Option[Seq[scala.xml.Node]],next: scala.xml.MetaData)scala.xml.UnprefixedAttribute (key: String,value: String,next: scala.xml.MetaData)scala.xml.UnprefixedAttribute (key: String,value: Seq[scala.xml.Node],next1: scala.xml.MetaData)scala.xml.UnprefixedAttribute cannot be applied to (java.lang.String,Int,scala.xml.MetaData)
val joe =
> As I understand it the element label has to be a constant when using an XML
> literal. You will have to invoke the Elem constructor explicitly.
>

David Hall 4
Joined: 2009-08-21,
User offline. Last seen 42 years 45 weeks ago.
Re: XML literals - starting with some curly braces

On Fri, Jan 15, 2010 at 5:26 PM, Sciss wrote:
> ok, a pity.
>
> also it's nice to have the \\ attribute extractor. but... there is no way to serialize into attributes it seems:
>
> scala> val age = 32
> age: Int = 32
>
> scala> val joe =     | name = "Joe"
>     | rank = "code monkey"
>     | age = {age}
>     | />
> :5: error: overloaded method constructor UnprefixedAttribute with alternatives (key: String,value: Option[Seq[scala.xml.Node]],next: scala.xml.MetaData)scala.xml.UnprefixedAttribute (key: String,value: String,next: scala.xml.MetaData)scala.xml.UnprefixedAttribute (key: String,value: Seq[scala.xml.Node],next1: scala.xml.MetaData)scala.xml.UnprefixedAttribute cannot be applied to (java.lang.String,Int,scala.xml.MetaData)
>       val joe =                  ^
>

This error at least is just because age needs to be a string:

scala> val age = 32.toString
age: java.lang.String = 32

scala> val joe =
joe: scala.xml.Elem =

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland