有什么方法可以简化以下内容吗?或者用另一个函数减少样板代码?
scala> val ns = <foo><bar id="1"><tag>one</tag><tag>uno</tag></bar><bar id="2"><tag>two</tag><tag>dos</tag></bar></foo>
ns: scala.xml.Elem = <foo><bar id="1"><tag>one</tag><tag>uno</tag></bar><bar id="2"><tag>two</tag><tag>dos</tag></bar></foo>
scala> (ns \\ "bar" filterNot{_ \\ "@id" find { _.text == "1" } isEmpty}) \\ "tag"
res0: scala.xml.NodeSeq = NodeSeq(<tag>one</tag>, <tag>uno</tag>)
最佳答案
我只能找到一个小改进,find
/isEmpty
测试可以替换为 exists
:
(ns \\ "bar" filter { _ \\ "@id" exists (_.text == "1") }) \\ "tag"
澄清评论后编辑:
真是个好主意!试试这个尺寸:
import xml._
implicit def richNodeSeq(ns: NodeSeq) = new {
def \@(attribMatch: (String, String => Boolean)): NodeSeq =
ns filter { _ \\ ("@" + attribMatch._1) exists (s => attribMatch._2(s.text)) }
}
ns \\ "bar" \@ ("id", _ == "1") \\ "tag"
我使用谓词而不是硬编码属性值比较。
关于xml - Scala XML,获取父节点具有属性值匹配的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7574862/