4

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 xpath = XPathFactory.newInstance.newXPath.compile(
    """/this/that/theOther[@abc="123"]""")
val selectedNodes: NodeSeq = xml.applyXpath(xpath)

2 Answers 2

2

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>)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but sorry, that does not address the question: Is there an existing library that mixes javax.xml.xpath with scala.xml
You also had the second question: "Is there an alternative?". I answered the second one.
The answer to my revised question (because I didn't really care about the alternative) is "No, it is not possible". But have a tick anyway. :)
1

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.

1 Comment

Yes the latter. I only wanted to use xpaths with Scala. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.