How to cast a Scala Elem to a NodeSeq
<p>I am trying to write an XML RewriteRule that will wrap a child element inside a wrapper element in an effort to normalize a bunch of different xml files.  This does what I need it to, but I end up with an Elem and in order to override the <code>transform()</code> function, I need to return a NodeSeq.</p>

<pre><code>  /* WRAP NODES INSIDE PARENT NODE
   *
   * Some Nodes should be wrapped in an enclosing Node.  For example, some xml versions
   * of the article &lt;contentitem&gt; have &lt;category&gt; as a direct child, but category info  
   * should really have this structure:
   *   &lt;categories&gt;
   *     &lt;category&gt;some category name&lt;/category&gt;
   *   &lt;/categories&gt;
   */
  val wrapIndividualNodes = new RewriteRule {
    val nodesToWrap = List( ("category", "categories"), ("subcategory", "subcategories") )

    override def transform(n: Node): NodeSeq = {
      for ( (target, wrapper) &lt;- nodesToWrap ) {

        // copy, then delete, children
        val categoryNodes = (n \ target)
        val articleWithoutCategoryNodes = SharedRules.deleteChildren(n, target)

        // wrap children and add as new child
        val categories = &lt;categories&gt;{categoryNodes}&lt;/categories&gt;
        SharedRules.addChild(articleWithoutCategoryNodes, categories)

      }
    }
  }
</code></pre>

<p><strong>SharedRules.scala</strong></p>

<pre><code>  /**
    * Adds newChild as the last element in the parent node
    */
  def addChild(parent: Node, child: Node): Elem = {
    parent.copy(child = parent.child ++ child)
  }

  /**
    * Deletes all children with matching childLabel from parent
    */
  def deleteChildren(parent: Node, childLabel: String): Elem = {
    parent.copy( child = parent.child.filterNot(_.label == childLabel))
  }
</code></pre>