1

I'm experimenting with Kotlin JS before using it in a project. I'd like to parse a HTML doc using xpath specifications that the user is prompted to upload to the site.

    import kotlinx.browser.document

    val h1 = document.asDynamic().evaluate("//h1", getDocument())
    println("xpath h1=${h1}")
    return h1

In a browser console window, the println displays xpath h1=[object XPathResult]

Two questions:

  1. is …evaluate() a good approach for XPath parsing in Kotlin JS?

  2. where is XPathResult defined in Kotlin JS? Intellij doesn't offer to add an appropriate import.

Carl
  • 2,896
  • 2
  • 32
  • 50

1 Answers1

0
  1. asDynamic().evaluate is the common approach; however, there are custom wrappers [1] to make things more type-safe
  2. As stated in 'Use JavaScript code from Kotlin'

You can freely talk to JavaScript from Kotlin via dynamic types. If you want to use the full power of the Kotlin type system, you can create external declarations for JavaScript libraries which will be understood by the Kotlin compiler and the surrounding tooling. ...

To tell Kotlin that a certain declaration is written in pure JavaScript, you should mark it with the external modifier.

Here's an example of how this could look like when you import XPathResult as an external object in kotlin.

wp78de
  • 18,207
  • 7
  • 43
  • 71