Sql server SQL Server 2016从XML提取信息
我已经阅读了关于同一主题的各种文章,但似乎无法找到XML文件中的数据元素 以下是我的XML的一个片段:Sql server SQL Server 2016从XML提取信息,sql-server,xml,sql-server-2016,Sql Server,Xml,Sql Server 2016,我已经阅读了关于同一主题的各种文章,但似乎无法找到XML文件中的数据元素 以下是我的XML的一个片段: <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: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:Status Code="39">Approved</ed:Status> <ed:LastUpdatedDate>2021-03-10T14:20:55+13:00</ed:LastUpdatedDate> <ed:Identifiers>
<ed:CertificateID>NZL2021/MEABC/26913T</ed:CertificateID>
<ed:TemplateID>ED1.6</ed:TemplateID> </ed:Identifiers> <ed:Exhausted>true</ed:Exhausted> <ed:AutoApproval>false</ed:AutoApproval> <ed:DepartureDate>2021-03-10</ed:DepartureDate> <ed:Parties>
<ed:ConsignorID>MEABC</ed:ConsignorID>
<ed:ConsigneeID>FLIGHT1</ed:ConsigneeID> </ed:Parties> <ed:Transport>
<ed:Ports>
<ed:LoadingPortID>NZTRG</ed:LoadingPortID>
</ed:Ports>
<ed:FinalDestination>OAKLAND, United States</ed:FinalDestination>
<ed:TransportMode>1</ed:TransportMode>
<ed:LocalCarrier>MDH2</ed:LocalCarrier>
<ed:CarrierName> Ever Given</ed:CarrierName>
<ed:ConveyanceReference>V1234</ed:ConveyanceReference> </ed:Transport> <ed:Remarks>
<ed:Remark>
<ed:RemarkType>Unofficial Information</ed:RemarkType>
<ed:RemarkValue>Vessel ETD - 19/03/21\nTARE WEIGHT - 2880 KGS</ed:RemarkValue>
</ed:Remark> </ed:Remarks> <ed:Products>
<ed:Product>
<ed:ProductItem>1</ed:ProductItem>
<ed:Exhausted>true</ed:Exhausted>
<ed:Origin>AO</ed:Origin>
<ed:Description>BONELESS BEEF RUMP CAP</ed:Description>
<ed:CommonName>Bovine</ed:CommonName>
<ed:EligibilityCountries>
<ed:EligibilityCountryID>US</ed:EligibilityCountryID>
</ed:EligibilityCountries>
<ed:IntendedUse>consumption</ed:IntendedUse>
<ed:GrossWeight unitCode="KGM">296.4</ed:GrossWeight>
<ed:NetWeight unitCode="KGM">271.6</ed:NetWeight>
<ed:Remarks>
<ed:Remark>
<ed:RemarkType>Product Statement</ed:RemarkType>
<ed:RemarkValue>Item No. 81625\nLabel Approval 2659305 & 91060858</ed:RemarkValue>
</ed:Remark>
</ed:Remarks>
<ed:Classifications>
<ed:Classification>
<ed:ClassificationType>Temperature</ed:ClassificationType>
<ed:ClassificationValue>chilled</ed:ClassificationValue>
</ed:Classification>
<ed:Classification>
<ed:ClassificationType>New Zealand Harmonised System Code</ed:ClassificationType>
<ed:ClassificationValue>020130</ed:ClassificationValue>
</ed:Classification>
<ed:Classification>
<ed:ClassificationType>Halal Product</ed:ClassificationType>
<ed:ClassificationValue>1</ed:ClassificationValue>
</ed:Classification>
</ed:Classifications>
<ed:Containers>
<ed:Container>
<ed:ID>CGMU3099999</ed:ID>
<ed:Seals>
<ed:ID>NZMPIXXXXX</ed:ID>
</ed:Seals>
</ed:Container>
</ed:Containers>
<ed:Packaging>
<ed:Package>
<ed:Quantity>29</ed:Quantity>
<ed:Type>CT</ed:Type>
<ed:Level>1</ed:Level>
<ed:ShippingMarks>
<ed:Name>MABC\n26913</ed:Name>
</ed:ShippingMarks>
</ed:Package>
</ed:Packaging>
<ed:Processes>
<ed:Process>
<ed:ProcessTypeCode>SLT</ed:ProcessTypeCode>
<ed:StartDate>2021-03-01</ed:StartDate>
<ed:EndDate>2021-03-01</ed:EndDate>
<ed:DateOverride>false</ed:DateOverride>
<ed:Premise>
<ed:ID>MEABC</ed:ID>
</ed:Premise>
</ed:Process>
<ed:Process>
<ed:ProcessTypeCode>PRO</ed:ProcessTypeCode>
<ed:StartDate>2021-03-02</ed:StartDate>
<ed:EndDate>2021-03-02</ed:EndDate>
<ed:DateOverride>false</ed:DateOverride>
<ed:Premise>
<ed:ID>MEABC</ed:ID>
</ed:Premise>
</ed:Process>
<ed:Process>
<ed:ProcessTypeCode>CST</ed:ProcessTypeCode>
<ed:StartDate>2021-03-02</ed:StartDate>
<ed:EndDate>2021-03-10</ed:EndDate>
<ed:DateOverride>false</ed:DateOverride>
<ed:Premise>
<ed:ID>MEABC</ed:ID>
</ed:Premise>
</ed:Process>
</ed:Processes>
</ed:Product>
</ed:Products>
</ed:Certificate>
我没有收到任何返回的结果
我使用OPENROWSET也得到了同样的结果
谁能告诉我如何访问此数据元素。您似乎对XML名称空间感到困惑。示例文档定义了两个命名空间URI:
ed
名称空间前缀,通过对其进行观察,该前缀似乎用于文档中的每个元素,因此也可能是默认名称空间/Certificate/Products/Product/ProductItem
元素的值,其操作如下:
使用xmlnamespace的(
违约'http://sancrt.mpi.govt.nz/ecert/2013/ed-submission-schema.xsd'
)
选择productItem.value(N'text()[1]',N'int')作为productItem
从@xml.nodes('/Certificate/Products/Product/ProductItem')开始,作为p(ProductItem);
在此基础上展开以选择更多的值,您可以看到此处使用的@
来访问元素的unitCode
属性:
使用xmlnamespace的(
违约'http://sancrt.mpi.govt.nz/ecert/2013/ed-submission-schema.xsd'
)
选择
product.value(N'(ProductItem/text())[1]',N'int')作为ProductItem,
product.value(N'(耗尽/text())[1]',N'bit')作为耗尽,
product.value(N'(Origin/text())[1]',N'nvarchar(2')作为原点,
product.value(N'(grosswight/text())[1]',N'decimal(19,1')作为grosswight,
产品价值(N'(grosswight/@unitCode)[1]',N'nvarchar(3)”作为grosswight单元代码
将@xml.nodes('/Certificate/Products/Product')作为p(Product);
从以上两个查询中可以清楚地看到,XPath查询中使用的名称空间前缀不必与XML文档中使用的前缀相同——重要的是名称空间URI本身。文档中的前缀用于将元素(有时是属性)链接到它们的命名空间URI,XPath中使用的前缀可以完全不同,只要它们引用正确的命名空间URI。e、 g.此查询返回与上面第二个示例相同的结果,尽管它们在源XML中是nosubmission
前缀:
使用xmlnamespace的(
'http://sancrt.mpi.govt.nz/ecert/2013/ed-multiple-submission-schema.xsd"作为多个,,
'http://sancrt.mpi.govt.nz/ecert/2013/ed-submission-schema.xsd"作为服从,
)
选择
product.value(N'(提交:ProductItem/text())[1]',N'int')作为ProductItem,
product.value(N'(提交:耗尽/text())[1]',N'bit')作为耗尽,
product.value(N'(提交:Origin/text())[1]',N'nvarchar(2)”作为来源,
product.value(N'(提交:GrossWight/text())[1]',N'decimal(19,1)')作为GrossWight,
产品价值(N'(提交:GrossWight/@unitCode)[1]',N'nvarchar(3)')作为GrossWightUnitCode,
产品价值(N'(提交:备注/提交:备注/提交:RemarkType/text())[1]',N'nvarchar(50)')作为项目注释
来自@xml.nodes('/submission:Certificate/submission:Products/submission:Product')的p(产品);
您似乎对XML名称空间感到困惑。示例文档定义了两个命名空间URI:
ed
名称空间前缀,通过对其进行观察,该前缀似乎用于文档中的每个元素,因此也可能是默认名称空间/Certificate/Products/Product/ProductItem
元素的值,其操作如下:
使用xmlnamespace的(
违约'http://sancrt.mpi.govt.nz/ecert/2013/ed-submission-schema.xsd'
)
选择productItem.value(N'text()[1]',N'int')作为productItem
从@xml.nodes('/Certificate/Products/Product/ProductItem')开始,作为p(ProductItem);
在此基础上展开以选择更多的值,您可以看到此处使用的@
来访问元素的unitCode
属性:
使用xmlnamespace的(
违约'http://sancrt.mpi.govt.nz/ecert/2013/ed-submission-schema.xsd'
)
选择
product.value(N'(ProductItem/text())[1]',N'int')作为ProductItem,
product.value(N'(耗尽/text())[1]',N'bit')作为耗尽,
product.value(N'(Origin/text())[1]',N'nvarchar(2')作为原点,
product.value(N'(grosswight/text())[1]',N'decimal(19,1')作为grosswight,
产品价值(N'(grosswight/@unitCode)[1]',N'nvarchar(3)”作为grosswight单元代码
将@xml.nodes('/Certificate/Products/Product')作为p(Product);
从以上两个查询中可以清楚地看到,XPath查询中使用的名称空间前缀不必与XML文档中使用的前缀相同——重要的是名称空间URI本身。文档中的前缀用于将元素(有时是属性)链接到它们的命名空间URI,XPath中使用的前缀可以完全不同,只要它们引用正确的命名空间URI。e、 g.此查询返回与上面第二个示例相同的结果,尽管它们在源XML中是nosubmission
前缀:
使用xmlnamespace的(
'http://sancrt.mpi.govt.nz/ecert/2013/ed-multiple-submission-schema.xsd"作为多个,,
'http://sancrt.mpi.govt.nz/ecert/2013/ed-submission-schema.xsd"作为服从,
)
选择
product.value(N'(提交:ProductItem/text())[1]',N'int')作为ProductItem,
product.value(N'(提交:耗尽/text())[1]',N'bit')作为耗尽,
product.value(N'(提交:Origin/text())[1]',N'nvarchar(2)”作为来源,
product.value(N'(提交:GrossWight/text())[1]',N'decimal(19,1)')作为GrossWight,
产品价值(N'(提交:GrossWight/@unitCode)[1]',N'nvarchar(3)')作为GrossWightUnitCode,
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 @xmlFileName VARCHAR(200) ='K:\Upload\CSNXML\WaybillXml.xml'
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)
EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML
DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX)
SELECT @xml = (SELECT xml_data from #XmlImportTest)
EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML
;WITH XMLNAMESPACES ('http://sancrt.mpi.govt.nz/ecert/2013/ed-multiple-submission-schema.xsd' AS ed)
SELECT
p.value(N'@ProductItem',N'nvarchar(10)') AS ProductItem
FROM
@xml.nodes('/Certificate')
AS A(p)
CROSS APPLY a.p.nodes(N'Products/Product') AS B(m);