gpt4 book ai didi

sql-server - 在 SQL Server 2008 中使用 xmlnamespaces 读取带有命名空间的 XML 文件

转载 作者:数据小太阳 更新时间:2023-10-29 02:33:54 26 4
gpt4 key购买 nike

经过几天的调整,我仍然无法做到这一点。我正在尝试读取一个包含大量命名空间的 xml 文件,将特定的节点值插入到不同的表中。

XML

<ArrayOfCatalogItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CatalogItem Version="1">
<Container xmlns="http://3ecompany.com/webservices/catalogitemxml">
<ContainerType>Unknown</ContainerType>
<MarkedForRetail xsi:nil="true" />
</Container>
<Documents CultureCode="en" Elink="https://3eonline.com/ImageServer/ImageViewer.aspx?id=3Q%2ffAR8ne%2fvPh6syVnSymkS%2bBDo8OjmbVocxRCMEgeEuoz0WYqIq%2bpG3%2b3wu9B2vvARePPfTWFBb0hg91%2fRYfNzA43I%2baZTLYlibHjHcCDI%3d" Format="Msds" DocumentType="Sds" xmlns="http://3ecompany.com/webservices/catalogitemxml" />
<Documents CultureCode="en" Elink="https://3eonline.com/ImageServer/ImageViewer.aspx?id=3Q%2ffAR8ne%2fvPh6syVnSymniD9dyO5cXo%2bPmAACkW7RMmVjYMZVxizRxXLlqcjbNGgyhjsG5gzhZK9bibPB5EPg%3d%3d" Format="ClientAttachment" DocumentType="Attachment" xmlns="http://3ecompany.com/webservices/catalogitemxml" />
<IsHazardous xmlns="http://3ecompany.com/webservices/catalogitemxml">true</IsHazardous>
<ManufacturerName xmlns="http://3ecompany.com/webservices/catalogitemxml">Sigma-Aldrich</ManufacturerName>
<Msds xmlns="http://3ecompany.com/webservices/catalogitemxml">
<Elink>https://3eonline.com/ImageServer/ImageViewer.aspx?id=3Q%2ffAR8ne%2fvPh6syVnSymqIuIP5CInA01ZbaRQ9r18HUOi1FRQqntYtr58dWAm4wO3rdUO%2bO6MamuvwN7v7fbA%3d%3d</Elink>
<FireCodeClassification>
<MsdsId>8342624</MsdsId>
<Properties>
<PhysicalState>Liquid</PhysicalState>
<BoilingPoint>
<Minimum xsi:nil="true" />
<Range>EqualTo</Range>
<Units>Celsius</Units>
<Value>217.0000</Value>
</BoilingPoint>
</Properties>
<TransportationClassificationCompleted xsi:nil="true" />
<WasteCompleted xsi:nil="true" />
<ExtendedSds>false</ExtendedSds>
<TransportationExceptionClassificationCompleted xsi:nil="true" />
<BestAvailable>false</BestAvailable>
</Msds>
<ProductIdentifiers xmlns="http://3ecompany.com/webservices/catalogitemxml">
<Identifier>4197644</Identifier>
<FlaggedForResend xsi:nil="true" />
</ProductIdentifiers>
<ProductName xmlns="http://3ecompany.com/webservices/catalogitemxml">(3-Aminopropyl)triethoxyeilane - A3648</ProductName>
<ProductUid xmlns="http://3ecompany.com/webservices/catalogitemxml">4d3dc6df9cf24cc6b3ab2b6be8373858</ProductUid>
<IsDeactivated xmlns="http://3ecompany.com/webservices/catalogitemxml">false</IsDeactivated>
<DeactivatedDate xsi:nil="true" xmlns="http://3ecompany.com/webservices/catalogitemxml" />
</CatalogItem>
</ArrayOfCatalogItem>

我需要从这个 xml 中获取几个值(为了便于阅读,我删除了大部分 xml)。我编写了示例查询以仅获取标识符,而我得到的只是标识符列的“Null”。我想获取 ContainerType、MsdsID、PhysicalState、Minimum、

查询

DECLARE @XmlTable TABLE (XMLDATA XML)

INSERT INTO @XmlTable(XMLData)
SELECT CONVERT(XML, BulkColumn) AS BulkColumn
FROM OPENROWSET(BULK 'C:\AAEWRXML.xml', SINGLE_BLOB) AS x;

;WITH XMLNAMESPACES (Default 'http://www.w3.org/2001/XMLSchema','http://3ecompany.com/webservices/catalogitemxml' as CI)
SELECT
Identifier = XmlData.value('(ArrayOfCatalogItem/CatalogItem/ProductIdentifiers/CI:Identifier)[1]', 'varchar(10)'),
From
@XmlTable

最佳答案

使用这个:

;WITH XMLNAMESPACES ('http://3ecompany.com/webservices/catalogitemxml' as CI)
SELECT
CIVersion = CI.value('@Version', 'int'),
Identifier = PID.value('(.)[1]', 'varchar(10)')
FROM
@XmlTable
CROSS APPLY
XMLData.nodes('/ArrayOfCatalogItem/CatalogItem') AS XT(CI)
CROSS APPLY
CI.nodes('CI:ProductIdentifiers/CI:Identifier') AS XT2(PID)

没有默认 XML 命名空间,您需要应用 CI: <ProductIdentifiers> 的前缀以及 <Identifier>节点。此外,您的示例 XML 缺少结束符 </FireCodeClassification>标签....

关于sql-server - 在 SQL Server 2008 中使用 xmlnamespaces 读取带有命名空间的 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38972698/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com