0

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.

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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>)

Comments

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.