2
votes

I have an array and I want to fill it with strings taken from specific XML nodes, like in this pseudocode example:

let $array := array {}

for $child in $collection
where contains(data($child), "Hey")
do $array := array:append($array, data($child))

How would correct code look like to perform such an operation?

So if I have this XML

<root>
    <child>Hey</child>
    <child>Ho</child>
    <child>Hey Ho</child>
</root>

I expect the array to be

array ["Hey", "Hey Ho"]
1

1 Answers

5
votes

XQuery is a functional language. As such, variables cannot be reassigned once they have been declared.

The following code should do the trick:

array {
  for $child in $collection
  where contains(data($child/node1), "Hey")
  return $child/node2
}

Please note that the native XQuery data type for values is a sequence. Depending on your use case, maybe you don’t need arrays at all.