First the XML Logic.
DECLARE @XML XML =
'<cfdi:comprobante xmlns:cfdi="xxx.yyy.com" total="xxx.yyy.com" tax="xxx.yyy.com" xmlns:another="abc.com" amount="fff.net" contract="zzz.zzz" payment="ddd.com">
<cfdi:sender information="" moreinformation=""/>
<cfdi:receiver information="" moreinformation=""/>
<cfdi:addments>
<another:payment information="" moreinformation="">
<another:Movements date="" description="hello" amount="100.00" contract="10"/>
<another:Movements date="" description="hello2" amount="200.00" contract="20"/>
<another:Movements date="" description="bye" amount="300.00" contract="30"/>
<another:Movements date="" description="bye2" amount="400.00" contract="40"/>
</another:payment>
</cfdi:addments>
</cfdi:comprobante>';
SELECT
[Contract] = f2.Nd.value('(@contract)[1]', 'int'),
Amount = f2.Nd.value('(@amount)[1]', 'money'),
[Description] = f2.Nd.value('(@description)[1]', 'varchar(100)')
FROM (VALUES(@XML)) AS f(X)
CROSS APPLY f.X.nodes('//*:Movements') AS f2(Nd);
Returns:
Contract Amount Description
----------- ---------- -----------------
10 100.00 hello
20 200.00 hello2
30 300.00 bye
40 400.00 bye2
Against a table requires a small tweak:
DECLARE @sometable TABLE
(
SomeId INT IDENTITY,
SomeXML XML
);
INSERT @sometable VALUES(
'<cfdi:comprobante xmlns:cfdi="xxx.yyy.com" total="xxx.yyy.com" tax="xxx.yyy.com" xmlns:another="abc.com" amount="fff.net" contract="zzz.zzz" payment="ddd.com">
<cfdi:sender information="" moreinformation=""/>
<cfdi:receiver information="" moreinformation=""/>
<cfdi:addments>
<another:payment information="" moreinformation="">
<another:Movements date="" description="hello" amount="100.00" contract="10"/>
<another:Movements date="" description="hello2" amount="200.00" contract="20"/>
<another:Movements date="" description="bye" amount="300.00" contract="30"/>
<another:Movements date="" description="bye2" amount="400.00" contract="40"/>
</another:payment>
</cfdi:addments>
</cfdi:comprobante>'),
(
'<cfdi:comprobante xmlns:cfdi="xxx.yyy.com" total="xxx.yyy.com" tax="xxx.yyy.com" xmlns:another="abc.com" amount="fff.net" contract="zzz.zzz" payment="ddd.com">
<cfdi:sender information="" moreinformation=""/>
<cfdi:receiver information="" moreinformation=""/>
<cfdi:addments>
<another:payment information="" moreinformation="">
<another:Movements date="" description="Hi!" amount="400.00" contract="99"/>
<another:Movements date="" description="Sup" amount="1100.00" contract="50"/>
<another:Movements date="" description="Boooooo" amount="3100.00" contract="30"/>
</another:payment>
</cfdi:addments>
</cfdi:comprobante>');
Returns:
SomeId Contract Amount Description
-------- ----------- ------------ --------------------
1 10 100.00 hello
1 20 200.00 hello2
1 30 300.00 bye
1 40 400.00 bye2
2 99 400.00 Hi!
2 50 1100.00 Sup
2 30 3100.00 Boooooo
xmlns:cfdi- the others are probably somewhere earlier in the XML data)