tags:

views:

100

answers:

2

OK, this is stumping both me (rookie at Scala) as well as my colleague (more advanced at Scala). Scala 2.8.0. Here's a demonstration of the problem:

// 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>

So, two questions. One: wtf? Why is it like this? Two: I can't seem to find a way to convert a NodeSeq to an Elem, so that match will work. What's the right way to do this?

+5  A: 

A NodeSeq is a collection of Nodes, not a single node:

scala> (<a><b>1</b><b>2</b></a>) \ "b"
res0: scala.xml.NodeSeq = NodeSeq(<b>1</b>, <b>2</b>)

So you have to match on nodes:

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)

(Nodes tend to be Elems, so this works out okay. I don't know the reasoning behind the separation; I guess some nodes can have less associated with them than an Elem has.)

Rex Kerr
+4  A: 

The method \ returns a sequence of valid answers, not a single element. Here:

scala> val qq2 = <a><a1>A1</a1><bs><b>B1</b><c>C1</c><d>D1</d></bs></a>
qq2: scala.xml.Elem = <a><a1>A1</a1><bs><b>B1</b><c>C1</c><d>D1</d></bs></a>

scala> (qq2 \ "bs") match {case Seq(<bs>{x @ _*}</bs>) => //<-I added a Seq()
     |     for (xx <- x) println("matched " + xx) }
matched <b>B1</b>
matched <c>C1</c>
matched <d>D1</d>
Daniel
Thanks! This solution is more explicitly clear about the structures of the types, so you get the green check mark!
Harlan