2016-03-11 71 views
0

考慮:入門XML節點

$scala 
Welcome to Scala 2.12.0-M3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_51). 
Type in expressions for evaluation. Or try :help. 

scala> val ns = <foo><bar><baz/>Text</bar><bin/></foo> 
ns: scala.xml.Elem = <foo><bar><baz/>Text</bar><bin/></foo> 

爲什麼不ns \ "foo"返回第一個<foo>節點?

scala> ns \ "foo" 
res0: scala.xml.NodeSeq = NodeSeq() 

回答

2

因爲<foo>是在Elem您所呼叫的\方法。它不包含在其內部。

scala> val ns = <outer><foo><bar><baz/>Text</bar><bin/></foo></outer> 
ns: scala.xml.Elem = <outer><foo><bar><baz/>Text</bar><bin/></foo></outer> 

scala> ns \ "foo" 
res0: scala.xml.NodeSeq = NodeSeq(<foo><bar><baz/>Text</bar><bin/></foo>)