tags:

views:

432

answers:

2

I want to be able to do this:

val myXml = <myTag { someAttributes }> </myTag>

(because I don't know what the attribute details are at compile time)

and this:

val myXml = <{someTag}></{someTag}>

This isn't valid Scala syntax. The closest I can come is using the Elem object to construct elements, but it's being a little troublesome (inserting PCDATA where I don't want it to). Is there any way of doing it like the above?

+3  A: 
val myXml = <myTag/> % Attribute(None, "name", Text("value"), Null)

See scala.xml.Attribute for different constructors.

Adding the same attribute to all children:

scala> val xml = <root><a/><b/><c/></root>
xml: scala.xml.Elem = <root><a></a><b></b><c></c></root>

scala> xml.child map (_ match {
     | case elem : Elem => elem % Attribute(None, "name", Text("value"), Null)
     | case x => x
     | })
res3: Sequence[scala.xml.Node] = ArrayBuffer(<a name="value"></a>, <b name="value"></b>, <c name="value"></c>)

You can also use the stuff in scala.xml.transform to do so recursively to all XML:

val rr = new RewriteRule {
  override def transform(n: Node): Seq[Node] = n match {
    case elem : Elem => elem % Attribute(None, "name", Text("value"), Null) toSeq
    case other => other
  }
}

val rt = new RuleTransformer(rr)

scala> rt(xml)
res5: scala.xml.Node = <root name="value"><a name="value"></a><b name="value"></b><c name="value"></c></root>

Or you can add attributes to arbitrary parts of the xml:

scala> val xml = <root>{<a/> % Attribute(None, "name", Text("value"), Null)}</root>
xml: scala.xml.Elem = <root><a name="value"></a></root>

EDIT

Changing the name is easy to do on Scala 2.8, like this:

val someTag = "tag"
val myXml = <root>{<a/>.copy(label = someTag)}</root>
Daniel
Thank you very much!
Joe
This doesn't let me create a node with a given label though. I can't see a way of creating an arbitrary XML structure. The extractor for Elem has a varargs for children. I want to be able to pass a NodeSeq (of any size).
Joe
any way of creating a node as in the second snippets in the OP?
IttayD
@IttayD Somehow I missed that!
Daniel
A: 

Note: you need to

import scala.xml.Null

to get this to work, and not scala.Null, which also exists.

Henry Story