请帮忙。阅读完所有谷歌和stackoverflow :)我的大脑不再工作了。
我有以下TSQL(在SQL Server 2012上运行)
我无法弄清楚我需要在哪里声明我的a:namespace?我需要声明多少名称空间?
DECLARE @XML AS XML
DECLARE @hDoc AS INT
SELECT @XML = '<GetAssetWarrantyResponse xmlns="http://tempuri.org/">
<GetAssetWarrantyResult xmlns:a="http://schemas.datacontract.org/2004/07/Dell.AWR.Domain.Asset" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Faults />
<a:Response>
<a:DellAsset>
<a:AssetParts i:nil="true" />
<a:CountryLookupCode>5252</a:CountryLookupCode>
<a:CustomerNumber>645651</a:CustomerNumber>
</a:DellAsset>
</a:Response>
</GetAssetWarrantyResult>
</GetAssetWarrantyResponse>'
EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML
SELECT CountryLookupCode
FROM OPENXML(@hDoc, 'GetAssetWarrantyResponse/GetAssetWarrantyResult/a:Response/a:DellAsset')
WITH
(
CountryLookupCode [nvarchar](20) 'a:CountryLookupCode'
)
EXEC sp_xml_removedocument @hDoc
GO
答案 0 :(得分:3)
您需要在调用sp_xml_preparedocument
时指定名称空间前缀。在这种情况下,您拥有a
命名空间和默认命名空间(没有前缀的xmlns="...."
):
EXEC sp_xml_preparedocument
@hDoc OUTPUT
, @XML
, '<root xmlns:d="http://tempuri.org/" xmlns:a="http://schemas.datacontract.org/2004/07/Dell.AWR.Domain.Asset"/>'
并正确使用已注册的前缀:
SELECT CountryLookupCode
FROM OPENXML(@hDoc, 'd:GetAssetWarrantyResponse/d:GetAssetWarrantyResult/a:Response/a:DellAsset')
WITH
(
CountryLookupCode [nvarchar](20) 'a:CountryLookupCode'
)
答案 1 :(得分:0)
我在SQL Server中使用内置的本机XQuery 支持:
DECLARE @XML AS XML
SELECT @XML = '<GetAssetWarrantyResponse xmlns="http://tempuri.org/">
<GetAssetWarrantyResult xmlns:a="http://schemas.datacontract.org/2004/07/Dell.AWR.Domain.Asset" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Faults />
<a:Response>
<a:DellAsset>
<a:AssetParts i:nil="true" />
<a:CountryLookupCode>5252</a:CountryLookupCode>
<a:CustomerNumber>645651</a:CustomerNumber>
</a:DellAsset>
</a:Response>
</GetAssetWarrantyResult>
</GetAssetWarrantyResponse>'
;WITH XMLNAMESPACES(DEFAULT 'http://tempuri.org/', 'http://schemas.datacontract.org/2004/07/Dell.AWR.Domain.Asset' AS ns)
SELECT
@XML.value('(GetAssetWarrantyResponse/GetAssetWarrantyResult/ns:Response/ns:DellAsset/ns:CountryLookupCode)[1]', 'int')
就XML名称空间而言:您需要在XML文档中定义所有使用的,直到您想要从中获取数据