A Kotlin typesafe builder for xpath with helpful shortcuts for handling the pain points, such as case sensitivity, quote escaping and HTML-specific helper functions.
assertEquals(
"/descendant-or-self::node()/child::div[attribute::id = '1']/child::span",
xpath { any / div[{ attr("id") equal "1" }] / span }.toString()
)
this library includes several useful shortcuts to help with common patterns.
converts all text to lowercase, automatically handles quote escaping and trims any whitespace
assertEquals(
"/descendant-or-self::node()/child::*[translate(normalize-space(self::node()),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz') = concat('\"That',\"'\",'s mine\", he said.')]",
xpath {
anyNode[textIs("\"That's mine\", he said.")]
}.toString()
)
checks whether the element has the given class
assertEquals(
"/descendant-or-self::node()/child::*[contains(concat(' ',attribute::class,' '),' foo ')]",
xpath { anyNode[hasClass("foo")] }.toString()
)
most implementations of xpath still use xpath 1.0 (including all the modern browsers), so this library includes shims for some functions available in xpath 2+
note: there's only a couple shims in here at the moment. intending to add more in the future.
assertEquals(
"translate('Foo','ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz') = 'foo'",
expression { `lower-case`("Foo") equal "foo" }.toString()
)
in your build.gradle.kts
:
repositories {
maven("https://detachhead.github.io/maven")
}
kotlin {
sourceSets {
val commonMain by getting {
dependencies {
implementation("io.github.detachhead:kotlinxpath:$version")
}
}
}
}