Before we can even think about applying XPath expressions to an XML document, we need to get our hands on that document
somehow. kantan.xpath extends most things that “can be turned into XML” with useful methods, such as the oft-used
evalXPath method. Among such things are:
Implementing our own
XmlSource for types that aren’t supported by default is fairly simple.
Reduced to its simplest expression, an
XmlSource[A] is essentially an
A => ParseResult[Node] - that
is, a function that takes an
A and turns it into a
Node, with the possibility of safe failure encoded in
import kantan.xpath._ implicit val node: XmlSource[Node] = XmlSource.from(ParseResult.success)
A more idiomatic way of writing new
XmlSource instances, however, is to adapt existing ones through
The most useful instance for such purpose is the one that exists for
InputSource, provided an implicit instance of
XmlParser is in scope.
XmlParser is a concept most people will never really need to concern themselves about - a reasonable default
implementation is always in scope. The only reason to write a new one would be to change XML parsing behaviour, as
nekohtml module does.
implicit def stringSource(implicit parser: XmlParser): XmlSource[String] = XmlSource[InputSource].contramap(s => new InputSource(new java.io.StringReader(s)))