sp_xml_preparedocument and use of the Namespace. I get an error "XML parsing error: Reference to undeclared namespace prefix: 'a'." in SQL Server 2012
sp_xml_preparedocument and use of the Namespace. I get an error "XML parsing error: Reference to undeclared namespace prefix: 'a'." in SQL Server 2012
请帮忙。看完所有 google 和 Whosebug :) 我的大脑不再工作了。
我有以下 TSQL(运行 在 SQL Server 2012 上)
我不知道我需要在哪里声明我的 a: 命名空间?我需要声明多少个命名空间?
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
调用时需要指定命名空间前缀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'
)
我会在 SQL 服务器中使用 内置的原生 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 文档中定义 所有使用的 直到您想要获得数据来自
请帮忙。看完所有 google 和 Whosebug :) 我的大脑不再工作了。
我有以下 TSQL(运行 在 SQL Server 2012 上)
我不知道我需要在哪里声明我的 a: 命名空间?我需要声明多少个命名空间?
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
调用时需要指定命名空间前缀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'
)
我会在 SQL 服务器中使用 内置的原生 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 文档中定义 所有使用的 直到您想要获得数据来自