title: Scala: modify a NodeSeq
body: <p>I have a NodeSeq like this: </p>
  
  <p><code>&lt;foo&gt;<br>
  &lt;baz&gt;&lt;bar key1="value1" key2="value2"&gt;foobar&lt;/bar&gt;&lt;/baz&gt;<br>
  Blah blah blah<br>
  &lt;bar key1="value3"&gt;barfoo&lt;/bar&gt;<br>
  &lt;/foo&gt;</code></p>
  
  <p>I want to add a new attribute to all <code>bar</code>s' attributes. I'm currently doing:</p>
  
  <pre><code>   val rule = new RewriteRule() {
       override def transform(node: Node): Seq[Node] = {
         node match {
            case Elem(prefix, "bar", attribs, scope, content@_*)  =&gt; Elem(prefix, "bar", attribs append Attribute(None, "newKey", Text("newValue"), scala.xml.Null) , scope, content:_*)
            case other =&gt; other
         }
       }
     }
  </code></pre>
  
  <p>But the problem is that it only works on 1 node. I want it to recursively work on all nodes, and if I call the transform inside a <code>for</code> loop, I can't replace them with new values since they become immutable. How can I solve this?</p>