Scala Getting XML from Additional Paths

I would like to process a document to get a value that can have more than one path. An ideal signature would look something like this:

def value(doc: Elem, potential_paths: List[something]): String

If it just processes the document looking at the head of potential_packages, if it is found, return it, otherwise continue with path_paths.drop (1).

With XPath, “something” will be a simple String list representing XPath statements. Since "\" is actually a NodeSeq function, it does not seem (apparently) representable separately from node. Anyway, what is a scala-shaped way to get closer to this?

+3
source share
1 answer

, child/child/child, :

def search(doc: NodeSeq, path: String) =
  path.split('/').foldLeft(doc)(_ \ _)

:

def value(doc: Elem, potential_paths: List[String]) =
  potential_paths.view.map(search(doc, _)).find(_.nonEmpty)

Scala 2.8. Scala 2.7 view projection _.nonEmpty !_.isEmpty.

/ , XPath . , / <a><b/></a> \ "a" , a .

+4

Source: https://habr.com/ru/post/1744782/


All Articles