- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
xml attribute remove bug
Thu, 2009-12-03, 11:03
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
Fri, 2009-12-04, 18:27
#2
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:
--
Daniel C. Sobral
I travel to the future all the time.
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?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.
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.
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.
Fri, 2009-12-04, 19:37
#3
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
Fri, 2009-12-04, 21:07
#4
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:
--
Daniel C. Sobral
I travel to the future all the time.
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.
On Thu, Dec 3, 2009 at 8:02 AM, francisco treacy <francisco.treacy@gmail.com> wrote:
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.
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.
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