我曾在此前看到过以下例子,目标是返回包含具有值Y的id属性的所有节点X:
//find all nodes with an attribute "class" that contains the value "test"
val xml = XML.loadString( """<div>
<span class="test">hello</span>
<div class="test"><p>hello</p></div>
</div>""" )
def attributeEquals(name: String, value: String)(node: Node) =
{
node.attribute(name).filter(_==value).isDefined
}
val testResults = (xml \\ "_").filter(attributeEquals("class","test"))
//prints: ArrayBuffer(
//<span class="test">hello</span>,
//<div class="test"><p>hello</p></div>
//)
println("testResults: " + testResults)
我正在使用Scala 2.7,每次打印返回值时总是空的。有人可以帮忙吗? 抱歉如果我复制了另一个线程...但是认为如果我发布一个新的将更加可见?