我有这个xml(为了便于阅读,删除了大部分内容)。我弄错了记录。每个CAS_Number
获取错误的化学名称。一个CAS_Number
表示一种化学物质。但是在这里,对于相同的CAS_number
,我有不同的化学名称。
这是我交叉申请中的问题,但我无法弄清楚。
<ArrayOfCatalogItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CatalogItem Version="1">
<Msds xmlns="http://3ecompany.com/webservices/catalogitemxml">
<ProductIdentifiers xmlns="http://3ecompany.com/webservices/catalogitemxml">
<Identifier>M007628</Identifier>
<Source>CPN</Source>
<FirstExportDate xsi:nil="true" />
<LastExportDate xsi:nil="true" />
<FlaggedForResend xsi:nil="true" />
</ProductIdentifiers>
<Ingredients>
<ChemicalName>Hexane</ChemicalName>
<Cas>000110-54-3</Cas>
<AvgPercent>20.000000</AvgPercent>
</Ingredients>
<Ingredients>
<ChemicalName>2-Propanone</ChemicalName>
<Cas>000067-64-1</Cas>
<AvgPercent>20.000000</AvgPercent>
</Ingredients>
<Ingredients>
<ChemicalName>Petroleum gases, liquefied, sweetened</ChemicalName>
<Cas>068476-86-8</Cas>
</Ingredients>
</Msds>
</CatalogItem>
</ArrayOfCatalogItem>
存储过程如下:
DECLARE @XmlTable TABLE (XMLDATA XML)
INSERT INTO @XmlTable(XMLData)
SELECT CONVERT(XML, BulkColumn) AS BulkColumn
FROM OPENROWSET(BULK 'C:\AA.Sample.File.LUS.Pilly-oneCI.xml', SINGLE_BLOB) AS x;
;WITH XMLNAMESPACES ('http://3ecompany.com/webservices/catalogitemxml' as CI)
SELECT
--CIVersion = CI.value('@Version', 'int'),
Identifier = PID.value('(.)\[1\]', 'varchar(9)'),
Product_Name = MSDSPN.value('(.)\[1\]','varchar(100)'),
CAS_Number = CAS.value('(.)\[1\]', 'varchar(20)'),
Chemical_Name = CN.value('(.)\[1\]', 'varchar(100)')
FROM
@XmlTable
CROSS APPLY
XMLData.nodes('/ArrayOfCatalogItem/CatalogItem') AS XT(CI)
OUTER APPLY
CI.nodes('CI:ProductIdentifiers/CI:Identifier') AS XT2(PID)
CROSS APPLY
CI.nodes('CI:Msds/CI:Ingredients/CI:Cas') AS XT18(CAS)
CROSS APPLY
CI.nodes('CI:Msds/CI:Ingredients/CI:ChemicalName') AS XT19(CN)
答案 0 :(得分:3)
试试这个:
;WITH XMLNAMESPACES ('http://3ecompany.com/webservices/catalogitemxml' as CI)
SELECT
Identifier = CI.value('(CI:ProductIdentifiers[1]/CI:Identifier)[1]', 'varchar(9)'),
CAS_Number = Ingred.value('(CI:Cas)[1]', 'varchar(20)'),
Chemical_Name = Ingred.value('(CI:ChemicalName)[1]', 'varchar(100)')
FROM
@XmlTable
CROSS APPLY
XMLData.nodes('/ArrayOfCatalogItem/CatalogItem/CI:Msds') AS XT(CI)
CROSS APPLY
CI.nodes('CI:Ingredients') AS XT18(Ingred)
我得到每个<Msds>
节点的XML片段列表,并从中获取ProductIdentifiers
信息。另外,从这个节点,我还获得了<Ingredients>
节点的子XML片段列表,并从中获取详细信息。
我的输出如下: