I have an XML file that I need to make some modifications to.
I can load the file fine in SQL, but when I trying to read some of the nodes, I get nothing returned.. no errors either. What am I doing wrong?
The start of my XML goes like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<batch_request xmlns="http://sancrt.mpi.govt.nz/ecert/2013/batch-submission-schema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://sancrt.mpi.govt.nz/ecert/2013/batch-submission-schema.xsd batchsubmission- schema.xsd">
<raise_eligibility_document>
<ed:Certificate xmlns="http://sancrt.mpi.govt.nz/ecert/2013/ed-multiple-submission-schema.xsd" xmlns:ed="http://sancrt.mpi.govt.nz/ecert/2013/ed-submission-schema.xsd">
<ed:Identifiers>
<ed:CertificateID>26920</ed:CertificateID>
<ed:TemplateID>ED1</ed:TemplateID>
</ed:Identifiers>
<ed:DepartureDate>2021-03-10</ed:DepartureDate>
<ed:Parties>
<ed:ConsignorID>XX345</ed:ConsignorID>
<ed:ConsigneeID>FLIGHT1</ed:ConsigneeID>
</ed:Parties>
Here is my SQL statement to load and read this file and my attempt to return a node.
Declare @xmlFile VARCHAR(30) = 'TESTING'
if OBJECT_ID('tempdb..#XmlImportTest') is not null
drop table #XmlImportTest
CREATE TABLE #XmlImportTest(
xmlFileName VARCHAR(300) NOT NULL,
xml_data XML NOT NULL
);
declare @xmlpath varchar(200) = 'Location of file'
declare @xmlFileName varchar(200) = @xmlpath+@xmlFile+'.xml'
--– dynamic sql is just so we can use @xmlFileName variable in OPENROWSET
EXEC('INSERT INTO #XmlImportTest(xmlFileName, xml_data)
SELECT ''' + @xmlFileName + ''', xmlData
FROM(
SELECT *
FROM OPENROWSET (BULK ''' + @xmlFileName + ''', SINGLE_BLOB) AS XMLDATA
) AS FileImport (XMLDATA)
')
DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX)
select @xml = (SELECT xml_data from #XmlImportTest)
--select @xml
;with xmlnamespaces (
'http://sancrt.mpi.govt.nz/ecert/2013/ed-multiple-submission-schema.xsd' as multiple,
'http://sancrt.mpi.govt.nz/ecert/2013/ed-submission-schema.xsd' as submission
)
select
header.value(N'(submission:Identifiers/submission:CertificateID/text())[1]', N'nvarchar(20)') as ecert_number
from @xml.nodes('/submission:Certificate') as ci(header)
This returns nothing.
I have tried this as well:
from
@xml.nodes('/submission:batch_request/submission:raise_eligibility_document/submission:Certificate') as ci(header)
I have also tried to use the following for the submission name space
;with xmlnamespaces (
'http://sancrt.mpi.govt.nz/ecert/2013/ed-multiple-submission-schema.xsd' as multiple,
--'http://sancrt.mpi.govt.nz/ecert/2013/ed-submission-schema.xsd' as submission ,
'http://sancrt.mpi.govt.nz/ecert/2013/batch-submission-schema.xsd' as submission
)
I still have nothing.. where am I going wrong with this please?
Here is a full repro how to do it properly.
Points to mention:
.nodes()
and .value()
XQuery methods.SQL
DECLARE @xml XML =
N'<batch_request xmlns="http://sancrt.mpi.govt.nz/ecert/2013/batch-submission-schema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://sancrt.mpi.govt.nz/ecert/2013/batch-submission-schema.xsd batchsubmission- schema.xsd">
<raise_eligibility_document>
<ed:Certificate xmlns="http://sancrt.mpi.govt.nz/ecert/2013/ed-multiple-submission-schema.xsd"
xmlns:ed="http://sancrt.mpi.govt.nz/ecert/2013/ed-submission-schema.xsd">
<ed:Identifiers>
<ed:CertificateID>26920</ed:CertificateID>
<ed:TemplateID>ED1</ed:TemplateID>
</ed:Identifiers>
<ed:DepartureDate>2021-03-10</ed:DepartureDate>
<ed:Parties>
<ed:ConsignorID>XX345</ed:ConsignorID>
<ed:ConsigneeID>FLIGHT1</ed:ConsigneeID>
</ed:Parties>
</ed:Certificate>
</raise_eligibility_document>
</batch_request>';
WITH XMLNAMESPACES (DEFAULT 'http://sancrt.mpi.govt.nz/ecert/2013/batch-submission-schema.xsd'
, 'http://sancrt.mpi.govt.nz/ecert/2013/ed-submission-schema.xsd' AS ed)
SELECT c.value('(ed:Identifiers/ed:CertificateID/text())[1]', 'VARCHAR(30)') AS CertificateID
, c.value('(ed:Identifiers/ed:TemplateID/text())[1]', 'VARCHAR(30)') AS TemplateID
, c.value('(ed:DepartureDate/text())[1]', 'DATE') AS DepartureDate
, c.value('(ed:Parties/ed:ConsignorID/text())[1]', 'VARCHAR(30)') AS ConsignorID
, c.value('(ed:Parties/ed:ConsigneeID/text())[1]', 'VARCHAR(30)') AS ConsigneeID
FROM @xml.nodes('/batch_request/raise_eligibility_document/ed:Certificate') AS t(c);
Output
+---------------+------------+---------------+-------------+-------------+
| CertificateID | TemplateID | DepartureDate | ConsignorID | ConsigneeID |
+---------------+------------+---------------+-------------+-------------+
| 26920 | ED1 | 2021-03-10 | XX345 | FLIGHT1 |
+---------------+------------+---------------+-------------+-------------+