flatmap xml Abschnitte und bringen die Elternattribute an Kinder?

Der folgende Code läuft in SQL server

declare @x xml = '<rx="x"> <sa="1" /> <sa="2" /> <sa="deleted" /> </r> <rx="y"> <sa="3" /> <sa="4" /> <sa="deleted" /> </r>' select @x.query(' for $r in /r for $s in $r[s/@a != "deleted"] let $z := $s (: Need to bring @x from <r> to <s> :) return <s> {$z} (: need to use $z in many places :) </s> ') 

kehrt zurück

 <s> <rx="x"> <sa="1" /> <sa="2" /> <sa="deleted" /> </r> </s> <s> <rx="y"> <sa="3" /> <sa="4" /> <sa="deleted" /> </r> </s> 

Allerdings sollten meine erwarteten Werte in $z sein

  <sa="1" x="x" /> <sa="2" x="x" /> <sa="3" x="y" /> <sa="4" x="y" /> 

Ich muss umsetzen

  1. Flatmap wird benötigt, um auf die verschiedenen XML-Abschnitte angewendet zu werden

    Bearbeiten: Kann mit for $s in $r/s[@a != "deleted"]

  2. Bringt das Attribut x von <r> zu <s> in der abschließenden Zuweisung von $z .

Update: Ich muss das folgende tun, das in SQL server nicht unterstützt wird?

 let $z := update insert attribute x {$r/@x} $s (: Need to bring @x from <r> to <s> :) 

 declare @x xml = '<rx="x"> <sa="1" /> <sa="2" /> <sa="deleted" /> </r> <rx="y"> <sa="3" /> <sa="4" /> <sa="deleted" /> </r>'; select @x.query(' <s> { for $r in /r for $s in $r/s[@a != "deleted"] (: Need to delete <s> which @a is "deleted" :) let $z := $s (: Need to bring @x from <r> to <s> :) return <sa="{$z/@a}" x="{$r/@x}" /> (: Some other code need to use $z:) } </s> '); 

Ausgabe:

 <s> <sa="1" x="x" /> <sa="2" x="x" /> <sa="3" x="y" /> <sa="4" x="y" /> </s>