Reputation: 31
I am new to Scala and trying to understand the capability of Scala with regards to xml processing using xpaths.
Going though Simple Xpath query in scala it appears that if I try to use an xpath to extract value from an xml like - elem \\ "/BCXYZ/abc/def" - using Scala, it wont work.
I can though use something like elem \\ "BCXYZ" \ "abc" \ "def" where elem is a scala.xml.Elem object (representing the xml from which I want to extract the xpath value).
Can someone confirm please.
Upvotes: 0
Views: 454
Reputation: 6178
I dislike the standard library's version of XML. If you're trying to execute XPath expressions, may I suggest kantan.xpath?
It lets you write things like:
import kantan.xpath.ops._
new java.io.File("input.xml").evalXPath[List[Int]]("//@id")
This will do what you expect: parse input.xml
as an XML document, look for all elements that have an id
attribute and extract that attribute as an Int
, storing all values in a List
.
Upvotes: 1
Reputation: 10904
Given an XML like
<BCXYZ>
<abc>
<def>
text
</def>
</abc>
</BCXYZ>
The query would work as stated
scala> elem \\ "BCXYZ" \ "abc" \ "def"
res20: scala.xml.NodeSeq =
NodeSeq(<def>
text
</def>)
Upvotes: 0
Reputation: 297265
As the accepted answer there says, Scala XML does not have XPath built-in. It has something that looks like XPath for very simple path extraction, and, yes, it won't work the way you want.
There are at least two other XML libraries for Scala, one of which has full XPath support.
Upvotes: 1