Hi, Take the below example:DECLARE @x xml SET @x='<foo:root xmlns:foo="http://www.foo/schema"> <foo:data1> <foo:part_number>1ASD4</foo:part_number> <foo:smr>smrcode</foo:smr> </foo:data1> <foo:data2> <foo:cage>09998</foo:cage> <foo:part_number>12234</foo:part_number> </foo:data2> <foo:data2> <foo:cage>04568</foo:cage> <foo:part_number>1ASD4</foo:part_number> </foo:data2> <foo:data2> <foo:cage>ASBB7</foo:cage> <foo:part_number>ZZZSSD4</foo:part_number> </foo:data2></foo:root>'SELECT @x.query('declare namespace foo="http://www.foo/schema"; (<root> {for $x in //foo:data2 return <doc> <pn>{$x/foo:part_number}</pn> <cage>{$x/foo:cage}</cage> <smr> {for $y in //foo:data1[foo:part_number = $x/foo:part_number] return data($y/foo:smr)} </smr> </doc>}</root>)')returns:<root> <doc> <pn> <foo:part_number xmlns:foo="http://www.foo/schema">12234</foo:part_number> </pn> <cage> <foo:cage xmlns:foo="http://www.foo/schema">09998</foo:cage> </cage> <smr /> </doc> <doc> <pn> <foo:part_number xmlns:foo="http://www.foo/schema">1ASD4</foo:part_number> </pn> <cage> <foo:cage xmlns:foo="http://www.foo/schema">04568</foo:cage> </cage> <smr>smrcode</smr> </doc> <doc> <pn> <foo:part_number xmlns:foo="http://www.foo/schema">ZZZSSD4</foo:part_number> </pn> <cage> <foo:cage xmlns:foo="http://www.foo/schema">ASBB7</foo:cage> </cage> <smr /> </doc></root>
My inner FLOWR $y (//foo:data1[foo:part_number = $x/foo:part_number]) searches the xml for a match in the foo:data1/foo:part_number. If there is a match, it prints the following-sibling foo:smr.Everything works, but takes a while with large documents. Is there a better way?