Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.databases.ms-sqlserver > #1664 > unrolled thread

Get XML values from nodes and child nodes

Started bygiancarlo.francesconi@gmail.com
First post2014-02-10 02:20 -0800
Last post2014-03-11 05:28 -0700
Articles 3 — 3 participants

Back to article view | Back to comp.databases.ms-sqlserver


Contents

  Get XML values from nodes and child nodes giancarlo.francesconi@gmail.com - 2014-02-10 02:20 -0800
    Re: Get XML values from nodes and child nodes Erland Sommarskog <esquel@sommarskog.se> - 2014-02-10 23:45 +0100
    Re: Get XML values from nodes and child nodes Bal Govind <balgovindjss@gmail.com> - 2014-03-11 05:28 -0700

#1664 — Get XML values from nodes and child nodes

Fromgiancarlo.francesconi@gmail.com
Date2014-02-10 02:20 -0800
SubjectGet XML values from nodes and child nodes
Message-ID<0a2e63e1-d6c0-4493-b52b-2d60a50bf1b7@googlegroups.com>
Hi everybody. My XML file is:
<Locatore>
  <NumeroProgressivo>001</NumeroProgressivo>
  <CodiceFiscale>CSTNDA69P90H523R</CodiceFiscale>
  <PersoneFisiche>
	<Cognome>CAST</Cognome>
	<Nome>NADIR</Nome>
	<Sesso>F</Sesso>
	<DataNascita>10091979</DataNascita>
	<ComuneNascita>RONCA</ComuneNascita>
	<ProvinciaNascita>BL</ProvinciaNascita>
  </PersoneFisiche>
</Locatore>
<Locatore>
  <NumeroProgressivo>002</NumeroProgressivo>
  <CodiceFiscale>PRSGRI74L29F443L</CodiceFiscale>
  <PersoneFisiche>
	<Cognome>PERISSI</Cognome>
	<Nome>IGOR</Nome>
	<Sesso>M</Sesso>
	<DataNascita>29071970</DataNascita>
	<ComuneNascita>MONTE</ComuneNascita>
	<ProvinciaNascita>SA</ProvinciaNascita>
  </PersoneFisiche>
</Locatore>

I need to get in ONE RECORD, both node value and his child nodes values, like this
001 | CSTNDA69P90H523R | CAST    | NADIR | F | 10091979 | RONCA | BL
002 | PRSGRI74L29F443L | PERISSI | IGOR  | M | 29071970 | MONTE | SA
....

With this: 
SELECT 
X.valore.query('NumeroProgressivo').value('.', 'VARCHAR(20)') as NumeroProgressivo,
X.valore.query('CodiceFiscale').value('.', 'VARCHAR(16)') as CodiceFiscale

FROM Tbulk 
CROSS APPLY Tbulk.nodes('Fornitura/Documento/Soggetti/PrimoModulo/Locatore') AS X(valore);

I only get the first node values (NumeroProgressivo, CodiceFiscale), but not the child nodes (cognome, nome sesso...)

[toc] | [next] | [standalone]


#1670

FromErland Sommarskog <esquel@sommarskog.se>
Date2014-02-10 23:45 +0100
Message-ID<XnsA2D0F1A2835A1Yazorman@127.0.0.1>
In reply to#1664
 (giancarlo.francesconi@gmail.com) writes:
> I only get the first node values (NumeroProgressivo, CodiceFiscale), but
> not the child nodes (cognome, nome sesso...) 
> 


DECLARE @xml xml = 
'<Locatore>
  <NumeroProgressivo>001</NumeroProgressivo>
  <CodiceFiscale>CSTNDA69P90H523R</CodiceFiscale>
  <PersoneFisiche>
	<Cognome>CAST</Cognome>
	<Nome>NADIR</Nome>
	<Sesso>F</Sesso>
	<DataNascita>10091979</DataNascita>
	<ComuneNascita>RONCA</ComuneNascita>
	<ProvinciaNascita>BL</ProvinciaNascita>
  </PersoneFisiche>
</Locatore>
<Locatore>
  <NumeroProgressivo>002</NumeroProgressivo>
  <CodiceFiscale>PRSGRI74L29F443L</CodiceFiscale>
  <PersoneFisiche>
	<Cognome>PERISSI</Cognome>
	<Nome>IGOR</Nome>
	<Sesso>M</Sesso>
	<DataNascita>29071970</DataNascita>
	<ComuneNascita>MONTE</ComuneNascita>
	<ProvinciaNascita>SA</ProvinciaNascita>
  </PersoneFisiche>
</Locatore>'

SELECT 
X.valore.query('NumeroProgressivo').value('.', 'VARCHAR(20)') as 
NumeroProgressivo,
X.valore.query('CodiceFiscale').value('.', 'VARCHAR(16)') as CodiceFiscale ,
Y.valore.query('Cognome').value('.', 'VARCHAR(16)') as Cognome,
Y.valore.query('Nome').value('.', 'VARCHAR(16)') as Nome,
Y.valore.query('Sesso').value('.', 'VARCHAR(16)') as Sesso,
Y.valore.query('DataNascita').value('.', 'VARCHAR(8)') as DataNascita,
Y.valore.query('ComuneNascita').value('.', 'VARCHAR(16)') as ComuneNascita,
Y.valore.query('ProvinciaNascita').value('.', 'VARCHAR(16)') as 
ProvinciaNascita
FROM @xml.nodes('/Locatore') AS X(valore)
CROSS APPLY X.valore.nodes('PersoneFisiche') AS Y(valore)



-- 
Erland Sommarskog, Stockholm, esquel@sommarskog.se

[toc] | [prev] | [next] | [standalone]


#1717

FromBal Govind <balgovindjss@gmail.com>
Date2014-03-11 05:28 -0700
Message-ID<fa639451-b030-4e2e-ad2b-4d8c9976512c@googlegroups.com>
In reply to#1664
On Monday, February 10, 2014 3:50:41 PM UTC+5:30, giancarlo....@gmail.com wrote:
> Hi everybody. My XML file is:
> 
> <Locatore>
> 
>   <NumeroProgressivo>001</NumeroProgressivo>
> 
>   <CodiceFiscale>CSTNDA69P90H523R</CodiceFiscale>
> 
>   <PersoneFisiche>
> 
> 	<Cognome>CAST</Cognome>
> 
> 	<Nome>NADIR</Nome>
> 
> 	<Sesso>F</Sesso>
> 
> 	<DataNascita>10091979</DataNascita>
> 
> 	<ComuneNascita>RONCA</ComuneNascita>
> 
> 	<ProvinciaNascita>BL</ProvinciaNascita>
> 
>   </PersoneFisiche>
> 
> </Locatore>
> 
> <Locatore>
> 
>   <NumeroProgressivo>002</NumeroProgressivo>
> 
>   <CodiceFiscale>PRSGRI74L29F443L</CodiceFiscale>
> 
>   <PersoneFisiche>
> 
> 	<Cognome>PERISSI</Cognome>
> 
> 	<Nome>IGOR</Nome>
> 
> 	<Sesso>M</Sesso>
> 
> 	<DataNascita>29071970</DataNascita>
> 
> 	<ComuneNascita>MONTE</ComuneNascita>
> 
> 	<ProvinciaNascita>SA</ProvinciaNascita>
> 
>   </PersoneFisiche>
> 
> </Locatore>
> 
> 
> 
> I need to get in ONE RECORD, both node value and his child nodes values, like this
> 
> 001 | CSTNDA69P90H523R | CAST    | NADIR | F | 10091979 | RONCA | BL
> 
> 002 | PRSGRI74L29F443L | PERISSI | IGOR  | M | 29071970 | MONTE | SA
> 
> ....
> 
> 
> 
> With this: 
> 
> SELECT 
> 
> X.valore.query('NumeroProgressivo').value('.', 'VARCHAR(20)') as NumeroProgressivo,
> 
> X.valore.query('CodiceFiscale').value('.', 'VARCHAR(16)') as CodiceFiscale
> 
> 
> 
> FROM Tbulk 
> 
> CROSS APPLY Tbulk.nodes('Fornitura/Documento/Soggetti/PrimoModulo/Locatore') AS X(valore);
> 
> 
> 
> I only get the first node values (NumeroProgressivo, CodiceFiscale), but not the child nodes (cognome, nome sesso...)


You can do it using OpenXML query that is an easy approach i think :)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.databases.ms-sqlserver


csiph-web