Refined module

kantan.xpath comes with a refined module that can be used by adding the following dependency to your build.sbt:

libraryDependencies += "com.nrinaudo" %% "kantan.xpath-refined" % "0.5.3"

You then need to import the corresponding package:

import kantan.xpath.refined._

And that’s pretty much it. You can now decode refined types directly.

Let’s first set our types up:

import eu.timepit.refined.api.Refined
import eu.timepit.refined.numeric.Positive
import kantan.xpath.implicits._

type PositiveInt = Int Refined Positive

We can then simply write the following:

"<foo><bar value='1'/></foo>".evalXPath[PositiveInt](xp"//bar/@value")
// res0: kantan.xpath.package.XPathResult[PositiveInt] = Right(value = 1)

And, for an error case:

"<foo><bar value='-1'/></foo>".evalXPath[PositiveInt](xp"//bar/@value")
// res1: kantan.xpath.package.XPathResult[PositiveInt] = Left(
//   value = TypeError(message = "Not acceptable: 'Predicate failed: (-1 > 0).'")
// )

Other tutorials: