0

Is it possible to do a search for a key words in an exist-db using xquery?

I've tried using

//foo//@val[. &= $param]

But this returns an error because this isn't supported with my version of exist-db (1.4.2)

What is the best way to do a search over a number of nodes?

<xml>
   <foo @val='test1'>
   <bar @val='test2'>
       <thunk @val='test3'/>
   </bar>
   </foo>

So with my example XML, how can I do

let $result :=

if //xml/foo[contains(@val,$param)] or
//xml/foo/bar[contains(@val,$param)] or
//xml/foo/bar/thunk[contains(@val,$param)]

return $result
user9418
  • 395
  • 2
  • 4
  • 13

1 Answers1

1

Either of these should work:

//foo//@val[contains(.,$param)]

//foo//@val[. eq $param]

However, there are obviously issues to consider when using contains() instead of equals. Also, if the paths will always be constrained as you describe in your example, and you are only checking to see if any of those are true (as opposed to actually getting all the elements), then this should be a faster and more efficient query:

((//xml/foo[@val eq $param])[1] or (//xml/foo/bar[@val eq $param])[1] or (//xml/foo/bar/thunk[@val eq $param])[1])

Untested, but the [1] should short-circuit the xpath evaluator after it gets its first result from the query, and the ORs should short-circuit the expression when any one of them returns a value.

wst
  • 11,681
  • 1
  • 24
  • 39