For the following xml
<Root>
<A><B>....</B></A>
<A><B>....</B><X>...</X></A>
I'm using the following xquery in SQL Server 2008R2. How to get the elements of A
without child X
?
select @xml.query('<r>...
{ for $a in //A return
<a>... {... $a[????] ...} .... (: use $a which doesn''t have child X only :)
{ for $x in $a/X return <x>... $x ...</x> } (: otherwise use //a/x :)
</a>
}</r>')
Update: Testable example:
declare @xml xml = '
<Root>
<A><B>123</B><C>***</C></A>
<A><B>456</B><C>***</C><X>abc</X></A>
</Root>'
select @xml.query('<r>
{ for $a in //A return
<a>{ $a/B/text() } (: use $a which doesn''t have child X only :)
{ for $x in $a/X return $x/text() } (: otherwise use //a/x :)
</a>
}</r>')
expected <r><a>123</a><a>abc</a></r>
instead of <r><a>123</a><a>456abc</a></r>
.