Search code examples
xmlscalaquotation-marks

Scala XML output an quotation mark ' " '


I'm trying to output to an .html file some info.

I'm reading from a file and getting values to know how much items my tables are going to have, like this:

val classes = (x \\ "class").length

I wanted to use the colspan with the value that I got, but I need to insert the quotation mark ' " '.

 var classe = {
  for (file <- x \ "java-class-file")
    yield <tr><th colspan="{classes}">{ file \ "@name" }</th></tr>
         {...}
}

But it doesn't work since its need something like:

colspan="2"

Instead I get:

colspan={classes}

How can I do it, since ' " ' doesn't work?


Solution

  • For the sake of completeness: in Scala's XML literal syntax you can interpolate attribute values by using braces without quotation marks:

    scala> val x = "foo"
    x: String = foo
    
    scala> <elem x={x}/>
    res0: scala.xml.Elem = <elem x="foo"/>
    

    The value needs to be a string, though, so in your case you want colspan={classes.toString}.