This page is no longer maintained — Please continue to the home page at www.scala-lang.org

XML namespaces

1 reply
Carsten Saager
Joined: 2008-12-19,
User offline. Last seen 42 years 45 weeks ago.

I have a problem here that drives me mad:

I have two XML nodes which declare the same namespace. More precisely
I want to add one to the other similar to

val part = ()

val root (
{part}
)

Problem: The namespace declaration gets repeated. This is no problem
until the process repeats and eventually a
is
created.

Any simple idea how to clean the namespace decls?

-Carsten

Chris Twiner
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: XML namespaces

Hiya,

If you mean how do you stop getting:

The only way to get it working, I have found, is to provide your own
bindings using Elem directly:

val ns = new NamespaceBinding("ns1", "http://yad.daf/", TopScope)

val parte = Elem("ns1", "Part", null, ns)
val roote = Elem("ns1", "Life", null, ns)
val roote1 = roote.copy(child = List(parte))

println(roote1)

will yield:

Swapping roote to use ns.copy() instead will force adding the ns
declaration back in :<. If you actually do just want to declare the
namespace and not have it bound replace the Elem("ns1" with Elem(null
(still using the same ns) and it will produce

If however you really mean it produces:
then I'd ask if you have a working example, because that would
definitely be a bug.

Scales, for example, will by default not redeclare things already in
scope (nor does it suffer from different instances):

import scales.xml._
import ScalesXml._

val ns = Namespace("http://yad.da/")
val ns1 = ns.prefixed("ns1")
val ns1c = ns.prefixed("ns1")

val part = Elem("Part",ns1) // the ns1 here is just for declaration
purposes, not for QName purposes
val root = Elem("Life",ns1c) /( part )

println(asString(root)) // <?xml version="1.0" encoding="UTF-8"?>

Or if you wanted Life and Part to use that namespace (without prefixes
use ns.apply instead of ns1.apply):

val part = Elem(ns1("Part")) // here ns1 is used for QName purposes,
Part is part of the prefixed namespace ns1
val root = Elem(ns1("Life")) /( part )

yielding (with ns1(" ): and with ns(" :

Cheers,
Chris

On Wed, Dec 21, 2011 at 11:46 PM, Carsten Saager wrote:
> I have a problem here that drives me mad:
>
> I have two XML nodes which declare the same namespace. More precisely
> I want to add one to the other similar to
>
> val part = ()
>
> val root (
> {part}
> )
>
> Problem: The namespace declaration gets repeated. This is no problem
> until the process repeats and eventually a
> is
> created.
>
> Any simple idea how to clean the namespace decls?
>
> -Carsten

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland