So if I were in your position, I think what I'd really want to be writing is something like:
case elem: Elem => elem.copy(attributes=
for (attr <- elem.attributes) yield attr match {
case attr@Attribute("attr1", _, _) =>
attr.copy(value=attr.value.text.toInt * 2)
case attr@Attribute("attr2", _, _) =>
attr.copy(value=attr.value.text.toInt * -1)
case other => other
}
)
There are two reasons this won't work out of the box:
Attribute doesn't have a useful copy method, and MetaData yields an Iterable[MetaData] instead of a MetaData so even something as simple as elem.copy(attributes=elem.attributes.map(x => x)) will fail.To fix the first problem, we'll use an implicit to add a better copy method to Attribute:
implicit def addGoodCopyToAttribute(attr: Attribute) = new {
def goodcopy(key: String = attr.key, value: Any = attr.value): Attribute =
Attribute(attr.pre, key, Text(value.toString), attr.next)
}
It can't be named copy since a method with that name already exists, so we'll just call it goodcopy. (Also, if you're ever creating values that are Seq[Node] instead of things that should be converted to strings, you could be a little more careful with value, but for our current purposes it's not necessary.)
To fix the second problem, we'll use an implicit to explain how to create a MetaData from an Iterable[MetaData]:
implicit def iterableToMetaData(items: Iterable[MetaData]): MetaData = {
items match {
case Nil => Null
case head :: tail => head.copy(next=iterableToMetaData(tail))
}
}
Then you can write code pretty much like what I proposed at the beginning:
scala> val elem =
elem: scala.xml.Elem =
scala> elem.copy(attributes=
| for (attr <- elem.attributes) yield attr match {
| case attr@Attribute("attr1", _, _) =>
| attr.goodcopy(value=attr.value.text.toInt * 2)
| case attr@Attribute("attr2", _, _) =>
| attr.goodcopy(value=attr.value.text.toInt * -1)
| case other => other
| }
| )
res1: scala.xml.Elem =