sp_xml_preparedocument 和 Namespace 的使用。我收到错误消息“XML 解析错误:引用未声明的命名空间前缀:'a'。” 在 SQL Server 2012 中

1 xml sql-server namespaces

请帮忙。阅读完所有 google 和 stackoverflow :) 我的大脑不再工作了。

我有以下 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
Run Code Online (Sandbox Code Playgroud)

har*_*r07 6

调用时需要指定命名空间前缀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"/>'
Run Code Online (Sandbox Code Playgroud)

并正确使用注册的前缀:

SELECT CountryLookupCode
FROM OPENXML(@hDoc, 'd:GetAssetWarrantyResponse/d:GetAssetWarrantyResult/a:Response/a:DellAsset')
WITH 
(
    CountryLookupCode   [nvarchar](20)  'a:CountryLookupCode'
)
Run Code Online (Sandbox Code Playgroud)