user148114
user148114

Reputation:

How to return the first result from XQuery

So I have an XQuery that looks something like this:

for $i in /*:rootElement
where $i/*:field = "test"
return $i

This query returns a lot of results, but I only really need one. How can I return just the first item in the result sequence?

Upvotes: 5

Views: 12686

Answers (1)

Pavel Minaev
Pavel Minaev

Reputation: 101615

(for $i in /*:rootElement where $i/*:field = "test" return $i)[1]

On a side note, a better way to write this is to use step predicates:

/*:rootElement[*:field = "test"][1]

Upvotes: 11

Related Questions