NodeSeq匹配失败,但等效的Elem匹配成功 - 为什么?怎么修?

Har*_*lan 3 xml scala match

好吧,这对我(Scala的新秀)以及我的同事(在Scala更先进)都很不利.Scala 2.8.0.这是一个问题的演示:

// I've got a var with some XML in it
scala> qq2
res9: scala.xml.Elem = <a><a1>A1</a1><bs><b>B1</b><c>C1</c><d>D1</d></bs></a>

// I can extract sub-elements
scala> (qq2 \ "bs")
res10: scala.xml.NodeSeq = NodeSeq(<bs><b>B1</b><c>C1</c><d>D1</d></bs>)

// but if I try to match against this NodeSeq, it fails to match
scala> (qq2 \ "bs") match {case <bs>{x @ _*}</bs> => 
            for (xx <- x) println("matched " + xx) }      
scala.MatchError: <bs><b>B1</b><c>C1</c><d>D1</d></bs>
        at .<init>(<console>:7)
        at ...

// but if I just type in the XML directly, it works as expected
scala> <bs><b>B1</b><c>C1</c><d>D1</d></bs> match {
          case <bs>{x @ _*}</bs> => for (xx <- x) println("matched " + xx) }
matched <b>B1</b>
matched <c>C1</c>
matched <d>D1</d>

// presumably because it's of type Elem, not NodeSeq
scala> <bs><b>B1</b><c>C1</c><d>D1</d></bs>
res13: scala.xml.Elem = <bs><b>B1</b><c>C1</c><d>D1</d></bs>
Run Code Online (Sandbox Code Playgroud)

那么,有两个问题.一:wtf?为什么会这样?二:我似乎无法找到将NodeSeq转换为Elem的方法,因此匹配将起作用.这样做的正确方法是什么?

Rex*_*err 6

A NodeSeqNodes 的集合,而不是单个节点:

scala> (<a><b>1</b><b>2</b></a>) \ "b"
res0: scala.xml.NodeSeq = NodeSeq(<b>1</b>, <b>2</b>)
Run Code Online (Sandbox Code Playgroud)

所以你必须在节点上匹配:

scala> ((<a><b>1</b><b>2</b></a>) \ "b").map(_ match {
     |   case <b>{x}</b> => true
     |   case _ => false
     | })
res24: scala.collection.immutable.Seq[Boolean] = List(true, true)
Run Code Online (Sandbox Code Playgroud)

(节点往往是Elems,所以这很好.我不知道分离背后的原因;我猜一些节点可能与Elem相关的节点更少.)