Nancy Baker
Nancy Baker

Reputation: 35

xquery - following-sibling for a string

<w> hello </w> <w> world </w>

Hello, I would like to know how to retrieve the following word from an xml file using xquery. E.g. I have assigned 'hello' to $x, how do I return 'world'. Many thanks.

Upvotes: 2

Views: 2188

Answers (2)

Jens Erat
Jens Erat

Reputation: 38662

You're looking for the following-sibling axis.

let $document := <document>
  <w>foo</w>
  <w>bar</w>
  <w>hello</w>
  <w>world</w>
  <w>batz</w>
</document>
let $x := "hello"
return ($document//w[. eq $x]/following-sibling::w)[1]

Upvotes: 1

wst
wst

Reputation: 11773

There are a few ways to do it, and the best way is probably context-dependent. In one case you might just want to get the next element node:

$x/following-sibling::*[1]

Or for some reason you might want to count the blank text node in between those two elements:

$x/following-sibling::node()[2]

Upvotes: 1

Related Questions