开发者

Using javax.xml.xpath. with scala.xml

开发者 https://www.devze.com 2023-03-31 10:13 出处:网络
Is it possible to use javax.xml.xpath.XPathExpressions with scala.xml.NodeSeqs? I\'d like an API that allows me to express something like:

Is it possible to use javax.xml.xpath.XPathExpressions with scala.xml.NodeSeqs?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消