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

xml attribute remove bug

4 replies
francisco treacy
Joined: 2009-02-13,
User offline. Last seen 42 years 45 weeks ago.

Or am I doing something wrong?

val x =
val y = x % x.attributes.remove("class")

y does not have the attribute "class" removed, y == x.

This works though:
val y = x % x.attributes.append(new UnprefixedAttribute("id", "woteva", Null))

y reflects the new attribute "id".

More generally, I am finding working with XML in Scala very hard. Are
there some "must read/use" wikis/ articles/ libraries around I might
not be aware of?

I am using Scala 2.7.7.final. Thanks,

Francisco

mailleux
Joined: 2008-08-23,
User offline. Last seen 4 years 7 weeks ago.
Re: xml attribute remove bug


On Thu, Dec 3, 2009 at 8:02 AM, francisco treacy <francisco.treacy@gmail.com> wrote:
Or am I doing something wrong?

val x = <div class="body"><a><b></b></a></div>
val y = x % x.attributes.remove("class")

y does not have the attribute "class" removed, y == x.

Looking at the code I figured out why. % will concatenate you attributes. And the x.attributes.remove("class") returns an empty list. Which concatenated with x.attributes == x.attributes.


 
This works though:
val y = x % x.attributes.append(new UnprefixedAttribute("id", "woteva", Null))



y = x % new UnprefixedAttribute("class",null.asInstanceOf[String], x.attributes.remove("class"))

Will remove it. You basically say class is null, and the concatenation will remove the parameter. Not very pretty, but works.

 
y reflects the new attribute "id".

More generally, I am finding working with XML in Scala very hard. Are
there some "must read/use" wikis/ articles/ libraries around I might
not be aware of?


I have trouble ocassionally but bets making a SAX parser by a long shot. Always remember that  scala.xml._ is all immutable. So there is a tendency for non destructive operations. Look at the code when in doubt, that how I do it.

Thomas

 
I am using Scala 2.7.7.final. Thanks,

Francisco

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: xml attribute remove bug
Thomas makes a very good point about immutability. Have you looked at scala.xml.transform yet?

On Fri, Dec 4, 2009 at 3:03 PM, Thomas Sant Ana <mailleux@gmail.com> wrote:


On Thu, Dec 3, 2009 at 8:02 AM, francisco treacy <francisco.treacy@gmail.com> wrote:
Or am I doing something wrong?

val x = <div class="body"><a><b></b></a></div>
val y = x % x.attributes.remove("class")

y does not have the attribute "class" removed, y == x.

Looking at the code I figured out why. % will concatenate you attributes. And the x.attributes.remove("class") returns an empty list. Which concatenated with x.attributes == x.attributes.


 
This works though:
val y = x % x.attributes.append(new UnprefixedAttribute("id", "woteva", Null))



y = x % new UnprefixedAttribute("class",null.asInstanceOf[String], x.attributes.remove("class"))

Will remove it. You basically say class is null, and the concatenation will remove the parameter. Not very pretty, but works.

 
y reflects the new attribute "id".

More generally, I am finding working with XML in Scala very hard. Are
there some "must read/use" wikis/ articles/ libraries around I might
not be aware of?


I have trouble ocassionally but bets making a SAX parser by a long shot. Always remember that  scala.xml._ is all immutable. So there is a tendency for non destructive operations. Look at the code when in doubt, that how I do it.

Thomas

 
I am using Scala 2.7.7.final. Thanks,

Francisco




--
Daniel C. Sobral

I travel to the future all the time.
francisco treacy
Joined: 2009-02-13,
User offline. Last seen 42 years 45 weeks ago.
Re: xml attribute remove bug

>> val x =
>> val y = x % x.attributes.remove("class")
>>
>> y does not have the attribute "class" removed, y == x.
>>
> Looking at the code I figured out why. % will concatenate you attributes.
> And the x.attributes.remove("class") returns an empty list. Which
> concatenated with x.attributes == x.attributes.

Aha. The docs mention 'updates', which is a bit blurry as a
definition. Misleading in my case.

> y = x % new UnprefixedAttribute("class",null.asInstanceOf[String],
> x.attributes.remove("class"))
>
> Will remove it. You basically say class is null, and the concatenation will
> remove the parameter. Not very pretty, but works.

Not very pretty? :) It's horrible. There's so much noise in there.

I can't see your point about immutability, how was that any different
from operations on java.lang.String. It is all non destructive.

Not thoroughly tested, but here's one of the bits in my utils:

implicit def n2e(n: Node) = new {
def append(key: String, value: String) = {
if (n.isInstanceOf[Elem]) n.asInstanceOf[Elem] %
n.attributes.append(new UnprefixedAttribute(key, value, Null))
else n
}
def remove(key: String) = {
if (n.isInstanceOf[Elem]) n.asInstanceOf[Elem] % new
UnprefixedAttribute(key, null.asInstanceOf[String],
n.attributes.remove(key))
else n
}
}

Daniel: Yes, I wasn't aware of scala.xml.transform and actually
learned about it in one of your answers... here:
http://stackoverflow.com/questions/970675/scala-modifying-nested-element...

:)

So, in conclusion: This library/language feature has a long of potential but:
- needs to be easier to use, beyond the simple examples (i know, xml
namespace hell, but well, convention over configuration then)
- better documentation, an up to date wiki?

Francisco

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: xml attribute remove bug
Your example of java.lang.String is perfect. It's exactly like that. Unlike DOM.

On Fri, Dec 4, 2009 at 4:33 PM, francisco treacy <francisco.treacy@gmail.com> wrote:
>> val x = <div class="body"><a><b></b></a></div>
>> val y = x % x.attributes.remove("class")
>>
>> y does not have the attribute "class" removed, y == x.
>>
> Looking at the code I figured out why. % will concatenate you attributes.
> And the x.attributes.remove("class") returns an empty list. Which
> concatenated with x.attributes == x.attributes.

Aha. The docs mention 'updates', which is a bit blurry as a
definition. Misleading in my case.

> y = x % new UnprefixedAttribute("class",null.asInstanceOf[String],
> x.attributes.remove("class"))
>
> Will remove it. You basically say class is null, and the concatenation will
> remove the parameter. Not very pretty, but works.

Not very pretty? :)  It's horrible. There's so much noise in there.

I can't see your point about immutability, how was that any different
from operations on java.lang.String. It is all non destructive.

Not thoroughly tested, but here's one of the bits in my utils:

 implicit def n2e(n: Node) = new {
   def append(key: String, value: String) = {
     if (n.isInstanceOf[Elem]) n.asInstanceOf[Elem] %
n.attributes.append(new UnprefixedAttribute(key, value, Null))
     else n
   }
   def remove(key: String) = {
     if (n.isInstanceOf[Elem]) n.asInstanceOf[Elem] % new
UnprefixedAttribute(key, null.asInstanceOf[String],
n.attributes.remove(key))
     else n
   }
 }


Daniel: Yes, I wasn't aware of scala.xml.transform and actually
learned about it in one of your answers... here:
http://stackoverflow.com/questions/970675/scala-modifying-nested-elements-in-xml

:)

So, in conclusion: This library/language feature has a long of potential but:
 - needs to be easier to use, beyond the simple examples  (i know, xml
namespace hell, but well, convention over configuration then)
 - better documentation, an up to date wiki?

Francisco



--
Daniel C. Sobral

I travel to the future all the time.

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