sp_xml_preparedocument 返回一個句柄,可用于訪問 XML 文檔的新創建的內部表示方式。該句柄在連接到 Microsoft® SQL Server™ 2000 期間保持有效,直到重置連接或執行 sp_xml_removedocument 使句柄無效為止。
說明 分析過的文檔存儲在 SQL Server 2000 的內部高速緩存中。MSXML 語法分析器使用 SQL Server 可用總內存的八分之一。若要避免內存不足,請運行 sp_xml_removedocument 以釋放內存。
sp_xml_preparedocument hdoc OUTPUT
[, xmltext]
[, xpath_namespaces]
hdoc
是新創建的文檔的句柄。hdoc 的數據類型為 integer。
[xmltext]
是原 XML 文檔。MSXML 語法分析器分析該 XML 文檔。xmltext 是 text 類型(char、nchar、varchar、nvarchar、text 或 ntext)的參數。默認值是 NULL,在這種情況下,將創建空 XML 文檔的內部表示法。
[xpath_namespaces]
指定 OPENXML 的行和列 XPath 表達式中所使用的命名空間聲明。默認值是 <root xmlns:mp="urn:schemas-microsoft-com:xml-metaprop">。
xpath_namespaces 通過符合語法規則的 XML 文檔的方式,為在 OPENXML 的 Xpath 表達式中使用的前綴提供命名空間 URI。xpath_namespaces 聲明前綴必須用于引用命名空間 urn:schemas-microsoft-com:xml-metaprop,該命名空間提供有關分析后的 XML 元素的元數據。盡管可以使用此方法為元屬性命名空間重新定義命名空間前綴,但此命名空間不會丟失。此前綴 mp 對 urn:schemas-microsoft-com:xml-metaprop 仍有效,即使 xpath_namespaces 不包含此類聲明。xpath_namespaces 是 text 類型(char、nchar、varchar、nvarchar、text 或 ntext)的參數。
0(成功)或 >0(失?。?/p>
執行權限默認授予 public 角色。
下例返回作為輸入提供的新創建的 XML 文檔內部表示法的句柄。在對 sp_xml_preparedocument 的調用中,使用了默認命名空間前綴映射。
DECLARE @hdoc int DECLARE @doc varchar(1000) SET @doc =' <ROOT> <Customer CustomerID="VINET" ContactName="Paul Henriot"> <Order CustomerID="VINET" EmployeeID="5" OrderDate="1996-07-04T00:00:00"> <OrderDetail OrderID="10248" ProductID="11" Quantity="12"/> <OrderDetail OrderID="10248" ProductID="42" Quantity="10"/> </Order> </Customer> <Customer CustomerID="LILAS" ContactName="Carlos Gonzlez"> <Order CustomerID="LILAS" EmployeeID="3" OrderDate="1996-08-16T00:00:00"> <OrderDetail OrderID="10283" ProductID="72" Quantity="3"/> </Order> </Customer> </ROOT>' --Create an internal representation of the XML document. EXEC sp_xml_preparedocument @hdoc OUTPUT, @doc -- Remove the internal representation. exec sp_xml_removedocument @hdoc
下例返回作為輸入提供的新創建的 XML 文檔內部表示法的句柄。存儲過程根據文檔中包含的 DTD 來驗證裝載的文檔。在對 sp_xml_preparedocument 的調用中,使用了默認命名空間前綴映射。
DECLARE @hdoc int DECLARE @doc varchar(2000) SET @doc = ' <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE root [<!ELEMENT root (Customers)*> <!ELEMENT Customers EMPTY> <!ATTLIST Customers CustomerID CDATA #IMPLIED ContactName CDATA #IMPLIED>]> <root> <Customers CustomerID="ALFKI" ContactName="Maria Anders"/> </root>' EXEC sp_xml_preparedocument @hdoc OUTPUT, @doc
下例返回作為輸入提供的新創建的 XML 文檔內部表示法的句柄。在對 sp_xml_preparedocument 的調用中,保留了元屬性命名空間映射的 mp 前綴,并將 xyz 映射前綴添加到了命名空間 urn:MyNamespace。
DECLARE @hdoc int DECLARE @doc varchar(1000) SET @doc =' <ROOT> <Customer CustomerID="VINET" ContactName="Paul Henriot"> <Order CustomerID="VINET" EmployeeID="5" OrderDate="1996-07-04T00:00:00"> <OrderDetail OrderID="10248" ProductID="11" Quantity="12"/> <OrderDetail OrderID="10248" ProductID="42" Quantity="10"/> </Order> </Customer> <Customer CustomerID="LILAS" ContactName="Carlos Gonzlez"> <Order CustomerID="LILAS" EmployeeID="3" OrderDate="1996-08-16T00:00:00"> <OrderDetail OrderID="10283" ProductID="72" Quantity="3"/> </Order> </Customer> </ROOT>' --Create an internal representation of the XML document. EXEC sp_xml_preparedocument @hdoc OUTPUT, @doc, '<root xmlns:xyz="run:MyNamespace"/>'