考慮在 SQL Server 上運行以下 SQL。這兩個塊之間的唯一區別是一個具有默認命名空間集。為什么默認命名空間的決議失敗?
我意識到還有其他方法可以在 sql server 中決議 xml。我只是想明確地理解這兩個例子中的奇怪之處。任何幫助表示贊賞。
DECLARE @idoc INT, @doc VARCHAR(1000);
SET @doc ='
<ROOT xmlns="Test" xmlns:B="anotherTest" >
<Customers CustomerID="VINET" ContactName="Paul Henriot">
<Orders CustomerID="VINET" EmployeeID="5" OrderDate=
"1996-07-04T00:00:00">
<Order_x0020_Details OrderID="10248" ProductID="11" Quantity="12"/>
<Order_x0020_Details OrderID="10248" ProductID="42" Quantity="10"/>
</Orders>
</Customers>
<Customers CustomerID="LILAS" ContactName="Carlos Gonzlez">
<Orders CustomerID="LILAS" EmployeeID="3" OrderDate=
"1996-08-16T00:00:00">
<Order_x0020_Details OrderID="10283" ProductID="72" Quantity="3"/>
</Orders>
</Customers>
</ROOT>';
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc;
-- SELECT statement as written returns 0 records.
SELECT *
FROM OPENXML (@idoc, '/ROOT/Customers')
EXEC sp_xml_removedocument @idoc;
GO
DECLARE @idoc INT, @doc VARCHAR(1000);
SET @doc ='
<ROOT xmlns:A="Test" xmlns:B="anotherTest" >
<Customers CustomerID="VINET" ContactName="Paul Henriot">
<Orders CustomerID="VINET" EmployeeID="5" OrderDate=
"1996-07-04T00:00:00">
<Order_x0020_Details OrderID="10248" ProductID="11" Quantity="12"/>
<Order_x0020_Details OrderID="10248" ProductID="42" Quantity="10"/>
</Orders>
</Customers>
<Customers CustomerID="LILAS" ContactName="Carlos Gonzlez">
<Orders CustomerID="LILAS" EmployeeID="3" OrderDate=
"1996-08-16T00:00:00">
<Order_x0020_Details OrderID="10283" ProductID="72" Quantity="3"/>
</Orders>
</Customers>
</ROOT>';
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc;
-- SELECT statement that uses the OPENXML rowset provider.
SELECT *
FROM OPENXML (@idoc, '/ROOT/Customers')
EXEC sp_xml_removedocument @idoc;
GO
相關鏈接: Proc sp_xml_preparedocument 檔案
SQL Server 2014
uj5u.com熱心網友回復:
第一個檔案有一個默認命名空間,因此名稱、ROOT 和客戶位于命名空間“Test”中,而 XPath 運算式“/ROOT/Customers”與它們不匹配。您需要為“Test”引入一個命名空間別名,以便在 XPath 運算式中使用。
為此,您可以通過提供一個帶有 alaised 命名空間宣告的虛擬 XML 檔案作為 sp_xml_preparedocument 的第三個引數,如下所示:
DECLARE @idoc INT, @doc VARCHAR(1000);
SET @doc ='
<ROOT xmlns="Test" xmlns:B="anotherTest" >
<Customers CustomerID="VINET" ContactName="Paul Henriot">
<Orders CustomerID="VINET" EmployeeID="5" OrderDate=
"1996-07-04T00:00:00">
<Order_x0020_Details OrderID="10248" ProductID="11" Quantity="12"/>
<Order_x0020_Details OrderID="10248" ProductID="42" Quantity="10"/>
</Orders>
</Customers>
<Customers CustomerID="LILAS" ContactName="Carlos Gonzlez">
<Orders CustomerID="LILAS" EmployeeID="3" OrderDate=
"1996-08-16T00:00:00">
<Order_x0020_Details OrderID="10283" ProductID="72" Quantity="3"/>
</Orders>
</Customers>
</ROOT>';
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc, N'<dummy xmlns:d="Test"/>';
-- SELECT statement that uses the OPENXML rowset provider.
SELECT *
FROM OPENXML (@idoc, '/d:ROOT/d:Customers')
EXEC sp_xml_removedocument @idoc;
GO
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/427386.html
