Using javax.xml.xpath. with scala.xml
Is it possible to use javax.xml.xpath.XPathExpression
s with scala.xml.NodeSeq
s?
I'd like an API that allows me to express something like:
val xml = ...
val x开发者_StackOverflow中文版path = XPathFactory.newInstance.newXPath.compile(
"""/this/that/theOther[@abc="123"]""")
val selectedNodes: NodeSeq = xml.applyXpath(xpath)
Scala takes a functional approach to searching through XML. In same cases it's not as clear as XPath and takes some getting used to it. For example:
scala> val myXml = <books><book category="1">first</book><book category="2">second</book><book category="1">third</book></books>
myXml: scala.xml.Elem = <books><book category="1">first</book><book category="2">second</book><book category="1">third</book></books>
scala> (myXml \ "book").filter { node => (node \\ "@category").text == "1" }
res24: scala.xml.NodeSeq = NodeSeq(<book category="1">first</book>, <book category="1">third</book>)
Is it an absolute requirement that its Scala XML and javax.xml.xpath? Or that you'd rather use string based XPaths within Scala.
Scales Xml has added string based XPath usage (as well as the faster internal syntax) but I haven't yet decided if it makes sense to provide a javax.xml.xpath implementation.
If this is something useful for people I'd consider spending more time on implementing it.
精彩评论