title: Using Scala pattern matching to extract XML elements with a certain name, regardless of content
body: <p>Given the following XML elements --</p>
  
  <pre><code>val nodes = List(
      &lt;foo/&gt;,
      &lt;bar/&gt;,
      &lt;baz/&gt;,
      &lt;bar&gt;qux&lt;/bar&gt;,
      &lt;bar quux="corge"/&gt;,
      &lt;bar quux="grauply"&gt;waldo&lt;/bar&gt;,
      &lt;bar quux="fred"&gt;&lt;/bar&gt;
  )
  </code></pre>
  
  <p>-- how do I construct a pattern that matches all <code>&lt;bar/&gt;</code>s? I've tried, for instance:</p>
  
  <pre><code>nodes flatMap (_ match {
    case b @ &lt;bar/&gt; =&gt; Some(b)
    case _ =&gt; None
  })
  </code></pre>
  
  <p>but this matches only the empties.</p>
  
  <pre><code>res17: List[scala.xml.Elem] = List(&lt;bar/&gt;, &lt;bar quux="corge"/&gt;, &lt;bar quux="fred"&gt;&lt;/bar&gt;)
  </code></pre>
  
  <p>And if I allow a placeholder for content:</p>
  
  <pre><code>nodes flatMap (_ match {
    case b @ &lt;bar&gt;{content}&lt;/bar&gt; =&gt; Some(b)
    case _ =&gt; None
  })
  </code></pre>
  
  <p>this matches only the <em>non-</em>empties.</p>
  
  <pre><code>res20: List[scala.xml.Elem] = List(&lt;bar&gt;qux&lt;/bar&gt;, &lt;bar quux="grauply"&gt;waldo&lt;/bar&gt;)
  </code></pre>
  
  <p>I could of course give up on XML literals and just write</p>
  
  <pre><code>nodes flatMap (_ match {
    case e: Elem if e.label == "bar" =&gt; Some(e)
    case _ =&gt; None
  })
  </code></pre>
  
  <p>but it seems like there must be a more clever way.</p>