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 表达式中所使用的命名空间声明。默认值是 **
1<root xmlns:mp="urn:schemas-microsoft-com:xml-metaprop"> ** 。
2_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** )的参数。
3
4##### 返回代码值
5
60(成功)或 >0(失败)
7
8##### 权限
9
10执行权限默认授予 **public** 角色。
11
12##### 示例
13
14###### A. 为符合语法规则的 XML 文档准备内部表示方式
15
16下例返回作为输入提供的新创建的 XML 文档内部表示法的句柄。在对 **sp_xml_preparedocument** 的调用中,使用了默认命名空间前缀映射。
17
18
19 DECLARE @hdoc int
20 DECLARE @doc varchar(1000)
21 SET @doc ='
22 <root>
23<customer contactname="Paul Henriot" customerid="VINET">
24<order customerid="VINET" employeeid="5" orderdate="1996-07-04T00:00:00">
25<orderdetail orderid="10248" productid="11" quantity="12"></orderdetail>
26<orderdetail orderid="10248" productid="42" quantity="10"></orderdetail>
27</order>
28</customer>
29<customer contactname="Carlos Gonzlez" customerid="LILAS">
30<order customerid="LILAS" employeeid="3" orderdate="1996-08-16T00:00:00">
31<orderdetail orderid="10283" productid="72" quantity="3"></orderdetail>
32</order>
33</customer>
34</root>'
35 --Create an internal representation of the XML document.
36 EXEC sp_xml_preparedocument @hdoc OUTPUT, @doc
37 -- Remove the internal representation.
38 exec sp_xml_removedocument @hdoc
39
40
41###### B. 为带 DTD 的符合语法规则的 XML 文档准备内部表示方式
42
43下例返回作为输入提供的新创建的 XML 文档内部表示法的句柄。存储过程根据文档中包含的 DTD 来验证装载的文档。在对 **sp_xml_preparedocument** 的调用中,使用了默认命名空间前缀映射。
44
45
46 DECLARE @hdoc int
47 DECLARE @doc varchar(2000)
48 SET @doc = '
49 <?xml version="1.0" encoding="UTF-8" ?>
50<!DOCTYPE root
51 [<!ELEMENT root (Customers)*>
52
53<!--ELEMENT Customers EMPTY-->
54<!--ATTLIST Customers CustomerID CDATA #IMPLIED ContactName CDATA #IMPLIED-->]>
55 <root>
56<customers contactname="Maria Anders" customerid="ALFKI"></customers>
57</root>'
58
59 EXEC sp_xml_preparedocument @hdoc OUTPUT, @doc
60
61
62###### C. 指定命名空间 URI
63
64下例返回作为输入提供的新创建的 XML 文档内部表示法的句柄。在对 **sp_xml_preparedocument** 的调用中,保留了元属性命名空间映射的 **mp** 前缀,并将 **xyz** 映射前缀添加到了命名空间 **urn:MyNamespace** 。
65
66
67 DECLARE @hdoc int
68 DECLARE @doc varchar(1000)
69 SET @doc ='
70 <root>
71<customer contactname="Paul Henriot" customerid="VINET">
72<order customerid="VINET" employeeid="5" orderdate="1996-07-04T00:00:00">
73<orderdetail orderid="10248" productid="11" quantity="12"></orderdetail>
74<orderdetail orderid="10248" productid="42" quantity="10"></orderdetail>
75</order>
76</customer>
77<customer contactname="Carlos Gonzlez" customerid="LILAS">
78<order customerid="LILAS" employeeid="3" orderdate="1996-08-16T00:00:00">
79<orderdetail orderid="10283" productid="72" quantity="3"></orderdetail>
80</order>
81</customer>
82</root>'
83 --Create an internal representation of the XML document.
84 EXEC sp_xml_preparedocument @hdoc OUTPUT, @doc, '<root xmlns:xyz="run:MyNamespace"></root>'</root>