2

I need to use some complex XPath queries on scala.

Is there a library that can execute XPath queries on Scala XML nodes?

Something like this:

val node: xml.Node = ...;
val result = TheXPathLibrary.query(node, "//a[2]/b[@type='c']/c");

I've tried with scala-xml-jaxen, but I failed to compile it. (I recently posted this issue.)


I am aware that you can achieve the same functionality of XPath queries in Scala without using XPath. However, this question is about finding out a library with allows to execute XPath queries.

David Portabella
  • 12,390
  • 27
  • 101
  • 182
  • 1
    See http://stackoverflow.com/questions/4228149/is-there-a-way-to-perform-a-xpath-string-query-using-scalas-xml-library?rq=1 This isn't an exact duplicate because it does not [directly] cover `xml.Node`. –  Feb 14 '13 at 16:12
  • that could be a good temporal workaround if there is a jdom from/to scala xml converter. do you know if that exists? – David Portabella Feb 14 '13 at 17:11
  • The "worst case" would be emitting XML (XML is *just text*) from xml.Node and then consuming the XML fragment/document in JDOM. Not very pretty, but heck, could work [well] in a pinch. –  Feb 14 '13 at 17:16
  • i did that, with xml element and list of xml elements, but then it failed with xml attributes, and it might fail with other cases. we can accept a slow solution as a workaround, but it should be a correct solution. – David Portabella Feb 15 '13 at 08:17
  • you may want to look at scales: https://github.com/chris-twiner/scalesXml (didn't use it by myself, but author claims XPath like support and better performance than built-in scala.xml) – dmitry Feb 15 '13 at 08:35
  • @DavidPortabella Emitting XML correctly is emitting XML correctly. It is not related to JDOM or reading it. While xml.Node stuff is far from a complete XML implementation, failure to emit valid XML - including attributes - would be a major bug. That is, do not blame Scala XML for this particular failure. –  Feb 15 '13 at 18:06
  • @pst, you can serialize in a xml file the result of a xpath that produces a xml element; however, if the xpath result is an attribute, you cannot serialize this result as a valid xml file. and so, the solution you proposed (to serialize and deserialize) does not work for all the possible xpath results. – David Portabella Feb 18 '13 at 21:58
  • @DavidPortabella Oh, I see. The initial comment was assuming serialization into, not out of. It would indeed require a special case in the latter form. (Although perhaps both would support serialization to the text representation of a *node*? But I have not tried as such.) –  Feb 18 '13 at 23:33

0 Answers0