L’approccio originale presenta alcuni problemi:
FROM OPENXMLè obsoleto.- Il namespace predefinito dichiarato nell’XML va dichiarato anche nella query.
- L’XPath era errato:
/Return/ReturnData/IRS990ScheduleHIRS990ScheduleH/deve diventare/Return/ReturnData/IRS990ScheduleH/.
Conviene usare i metodi XQuery moderni. Prima leggi l’XML in una variabile:
DECLARE @x xml;
SELECT @x = R
FROM OPENROWSET(BULK 'C:\Users\USER\990\Example.xml', SINGLE_BLOB) AS ReturnData(R);Poi dichiara i namespace e usa .nodes() e .value():
WITH XMLNAMESPACES(
DEFAULT 'https://www.irs.gov/efile',
'https://www.w3.org/2001/XMLSchema-instance' AS xsi
)
SELECT ct.value('(FinancialAssistancePolicyInd)[1]', 'int') AS FinancialAssistancePolicyInd,
ct.value('(FPGReferenceDiscountedCareInd)[1]', 'int') AS FPGReferenceDiscountedCareInd
FROM @x.nodes('/Return/ReturnData/IRS990ScheduleH') AS A(ct);