1

Topic: XML parse XPath or XQuery. To pull out the data of the parent.

declare @x xml = ' <datapacket>
<dids did = "1"/>
<QType qtid = "1"/>
<QType qtid = "2"/>
</dids>
<dids did = "2"/>
<QType qtid = "1"/>
<QType qtid = "2"/>
</dids>
</datapacket> ';

I want to receive as a result

c.value (N ' (@did) [1] ', ' smallint ');
t.c.value (N ' (@qtid) [1] ', ' smallint ')
from @x.nodes (N '/datapacket/dids/QType ') t (c)

To receive qtid and did the parent. That interests certainly not that parent - after all the enclosure can be more.
Started to look from here: https://stackoverflow.com/questions/189 … -xml-value
But it was not set.
Thanks.

2

Re: XML parse XPath or XQuery. To pull out the data of the parent.

declare @x xml = ' <datapacket>
<dids did = "1">
<QType qtid = "1"/>
<QType qtid = "2"/>
</dids>
<dids did = "2">
<QType qtid = "1"/>
<QType qtid = "2"/>
</dids>
</datapacket> ';
select
t.c.value (' (@did) [1] ', ' smallint ') as did
,t.c.value (N ' (QType / qtid) [1] ', ' smallint ') as qtid1
,t.c.value (N ' (QType / qtid) [2] ', ' smallint ') as qtid2
from @x.nodes (/datapacket/dids ') t (c)

At you xml a curve

3

Re: XML parse XPath or XQuery. To pull out the data of the parent.

_ _;

select T.c.value (' @did ', ' int ') as did;
T2.c.value (' @qtid ', ' int ') as qtid
from @x.nodes (/datapacket/dids ') T (c)
cross apply T.c.nodes ('./QType ') T2 (c)

4

Re: XML parse XPath or XQuery. To pull out the data of the parent.

Konst_One wrote:

declare @x xml = ' <datapacket>
<dids did = "1">
<QType qtid = "1"/>
<QType qtid = "2"/>
</dids>
<dids did = "2">
<QType qtid = "1"/>
<QType qtid = "2"/>
</dids>
</datapacket> ';
select
t.c.value (' (@did) [1] ', ' smallint ') as did
,t.c.value (N ' (QType / qtid) [1] ', ' smallint ') as qtid1
,t.c.value (N ' (QType / qtid) [2] ', ' smallint ') as qtid2
from @x.nodes (/datapacket/dids ') t (c)

At you xml a curve

Namely than?
I thus do not need listing (N ' (QType / qtid) [2] - the arbitrary amount can be in any of levels. as always.

5

Re: XML parse XPath or XQuery. To pull out the data of the parent.

_ _;
You XML contains not the relational data.

6

Re: XML parse XPath or XQuery. To pull out the data of the parent.

[quote = _ Promeshan _] Namely than?
I thus do not need listing (N ' (QType / qtid) [2] - the arbitrary amount can be in any of levels. as always.

At you dids closed (cannot contain child members). And then, after prospective child members there is a closing dids which did not open.

declare @x xml = ' <datapacket>
<dids did = "1">
<QType qtid = "1"/>
<QType qtid = "2"/>
</dids>
<dids did = "2">
<QType qtid = "1"/>
<QType qtid = "2"/>
</dids>
</datapacket> ';
select t.c.value (' (@did) [1] ', ' smallint ') as did
,t1.c.value (N ' (@qtid) [1] ', ' smallint ') as qtid
from @x.nodes (/datapacket/dids ') t (c) outer apply t.c.nodes (' QType ') as t1 (c)