Alternatives - scalawag/sdom GitHub Wiki

Here are some weird things that I found with existing Scala xml libraries that led me to take a stab at improving on it.

scala.xml

scala.xml has problems with even the simplest namespace manipulations. It can't always parse documents that it produces.

import scala.xml._
val x1 = <a xmlns="a"><b xmlns="a"/></a> \ "b"
XML.loadString(x1.toString)

There's also namespace weirdness that happens when you try to compose documents out of parts. I think that this is because the namespace of an Elem is not stored directly but depends on its scope, which isn't quite handled consistently.

import org.scalatest.Matchers._

val a = <outer xmlns="NS"><inner/></outer>
val inner = <inner/>
val b = <outer xmlns="NS">{inner}</outer>

a.toString shouldEqual b.toString

So, they serialize to the same document, but behold (there's no direct way to get the namespace of an Elem):

def namespace(e:Elem) = e.scope.getURI(e.prefix)
namespace(( a \ "inner" ).head.asInstanceOf[Elem]) shouldEqual namespace(( b \ "inner" ).head.asInstanceOf[Elem])

You could argue that either namespace is the correct one but it should be consistent regardless. The second document after re-parsing is now consistent with the first.

val c = XML.loadString(b.toString)
namespace(( a \ "inner" ).head.asInstanceOf[Elem]) shouldEqual namespace(( c \ "inner" ).head.asInstanceOf[Elem])
⚠️ **GitHub.com Fallback** ⚠️