1

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>
Joe
  • 575
  • 6
  • 24
  • You seem confused about XPath axes. By "successor elements" it seems you mean "child elements". And I can't see why you thought that preceding-sibling might be relevant to the problem. – Michael Kay Oct 12 '19 at 21:18

1 Answers1

2

This should get you to your desired output:

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

return 
(<item>
{$item}
</item>)
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
  • Or more succinctly, `{//deposit[not(amount le 300)]}` – Michael Kay Oct 12 '19 at 21:16
  • @MichaelKay - OP was asking for for an xquery expression, not just xpath, but - yes - less can definitely be more! – Jack Fleeting Oct 12 '19 at 23:19
  • 2
    XPath is a subset of XQuery, so an XPath solution is also an XQuery solution. But actually, the wrapper `` element means that my solution isn't pure XPath either. The notion that an XQuery solution has to be a FLWOR expression is one I try hard to dispel. – Michael Kay Oct 13 '19 at 08:02