piotrek
piotrek

Reputation: 14550

jxpath with collection as root

i have list of maps and want to get all maps with id = 2. when i wrap the list in a holder object, all works correctly (kotlin code):

class Holder(val value : Any)
val list = listOf(mapOf("id" to 2), mapOf("id" to 3))
val context = Holder(list)
val xpath = "value[id = 2]"
JXPathContext.newContext(context).iterate(xpath)

but when i try to get rid of the holder object:

val context = list
val xpath = "[id = 2]"

i get jxpath syntax error: Invalid XPath. how what's the correct syntax to work with collection as root object?

Upvotes: 0

Views: 144

Answers (2)

piotrek
piotrek

Reputation: 14550

found the answer. you can do it using /.[id=2] or .[id=2] syntax

Upvotes: 0

kjhughes
kjhughes

Reputation: 111621

XML requires that there be a single root element, so XPath provides no way to select multiple root elements.

If you want to use XML, you'll have to wrap your collection of elements with a single root element.

Upvotes: 1

Related Questions