Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala XML \\ copies xmlns attribute. Why and how can I stop it?

In Scala REPL:

val input = <outerTag xmlns="http://xyz"> <innerTag> </innerTag> </outerTag>

input\\@"innerTag"

=>

<innerTag xmlns="http://xyz"> </innerTag>

How do I stop Scala do this? Why can't it just give me <innerTag> </innerTag>? How can I stop this happening (or remove the xmlns attributes simply)?

Thanks!

Joe

Clarification: My overall task is chopping up an XML file and recombining it. So this node will be taken from beneath the root node (which has the xmlns attribute) and then integrated back into a document under a root which again has the xmlns.

like image 239
Joe Avatar asked Dec 16 '25 14:12

Joe


1 Answers

Use named parameter and Elem.copy() in Scala 2.8.0:

scala> import scala.xml._
import scala.xml._

scala> val outer = <outerTag xmlns="http://xyz"><innerTag></innerTag></outerTag>
outer: scala.xml.Elem = <outerTag xmlns="http://xyz"><innerTag></innerTag></outerTag>

scala> outer \\ "innerTag" map { case e: Elem => e.copy(scope = TopScope) }
res0: scala.xml.NodeSeq = <innerTag></innerTag>
like image 77
Walter Chang Avatar answered Dec 19 '25 05:12

Walter Chang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!