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?
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>
Note: you need to
import scala.xml.Null
to get this to work, and not scala.Null
, which also exists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With