SQL 中的 XML 命名空间混淆 [英] XML Namespace confusion in SQL
问题描述
我有一个需要修改的 XML 文件.
I have an XML file that I need to make some modifications to.
我可以在 SQL 中很好地加载文件,但是当我尝试读取某些节点时,我没有返回任何内容......也没有错误.我做错了什么?
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?
我的 XML 的开头是这样的:
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>
这是我加载和读取此文件的 SQL 语句以及我尝试返回一个节点.
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)
这什么都不返回.
我也试过这个:
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.
要点:
- 只有两个命名空间与所提供的 XML 摘录相关.之一它们是默认命名空间,因此不需要其前缀.第二命名空间需要ed"前缀.
- 现在在两个位置都有适当的 XPath 表达式,即
.nodes()
和.value()
XQuery 方法.
- Just two namespaces are relevant to the XML excerpt provided. One of them is a default namespace, so no need for its prefix. Second namespace needs 'ed' prefix.
- A proper XPath expressions are in place now in both locations, i.e.
.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);
输出
+---------------+------------+---------------+-------------+-------------+
| CertificateID | TemplateID | DepartureDate | ConsignorID | ConsigneeID |
+---------------+------------+---------------+-------------+-------------+
| 26920 | ED1 | 2021-03-10 | XX345 | FLIGHT1 |
+---------------+------------+---------------+-------------+-------------+
这篇关于SQL 中的 XML 命名空间混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!