Output XML nodes by checking if all their successors elements nodes values are greater than a certain fixed value?

Viewed 183

I have the following document where I want to select all nodes with their associated IDs iff all their successor node elements are greater than 300.

<item>
    <deposit id="1">
        <amount>1200</amount>
        <amount>5000</amount>
        <amount>2300</amount>
    </deposit>
    <deposit id="2">
        <amount>300</amount>
        <amount>1500</amount>
        <amount>700</amount>
    </deposit>
    <deposit id="3">
        <amount>300</amount>
        <amount>500</amount>
        <amount>700</amount>
    </deposit>
    <deposit id="4">
        <amount>300</amount>
    </deposit>
    <deposit id="5">
        <amount>3500</amount>
    </deposit>
    <deposit id="6">
        <amount>1000</amount>
    </deposit>
</item>

I've tried to run the following XQuery chunk but it gave me same as the above document without any changes.

let $e := $depot/deposit
for $a in $e/amount
return if (data($a/preceding-sibling::amount)>"300") then $e else ()

I am expecting to have the following query result,

<item>
    <deposit id="1">
        <amount>1200</amount>
        <amount>5000</amount>
        <amount>2300</amount>
    </deposit>
    <deposit id="5">
        <amount>3500</amount>
    </deposit>
    <deposit id="6">
        <amount>1000</amount>
    </deposit>
</item>
1 Answers

This should get you to your desired output:

let $item := //deposit[not(amount[text()<=300])]

return 
(<item>
{$item}
</item>)
Related