今天在存储过程里解析下面这份XML时遇到了一个小问题。
DECLARE @Xml VARCHAR(4000)
SET @Xml = '
<?xml version="1.0" encoding="gb2312"?>
<X>
<I>Hello</I>
<I>World</I>
</X>
'
DECLARE @XmlHandler INT
EXEC sp_xml_preparedocument @XmlHandler OUTPUT, @Xml
Select * FROM OPENXML(@XmlHandler, '/X', 2) WITH (I INT)
EXEC sp_xml_removedocument @XmlHandler
结果只出现一条记录
经分析,Xml解析器是从根下一级节点开始匹配的,修改如下
DECLARE @XmlHandler INT
EXEC sp_xml_preparedocument @XmlHandler OUTPUT, @Xml
Select * FROM OPENXML(@XmlHandler, '/X/I', 2) WITH (I INT '.')
EXEC sp_xml_removedocument @XmlHandler
问题解决
DECLARE @Xml VARCHAR(4000)
SET @Xml = '
<?xml version="1.0" encoding="gb2312"?>
<X>
<I>Hello</I>
<I>World</I>
</X>
'
DECLARE @XmlHandler INT
EXEC sp_xml_preparedocument @XmlHandler OUTPUT, @Xml
Select * FROM OPENXML(@XmlHandler, '/X', 2) WITH (I INT)
EXEC sp_xml_removedocument @XmlHandler
结果只出现一条记录
经分析,Xml解析器是从根下一级节点开始匹配的,修改如下
DECLARE @XmlHandler INT
EXEC sp_xml_preparedocument @XmlHandler OUTPUT, @Xml
Select * FROM OPENXML(@XmlHandler, '/X/I', 2) WITH (I INT '.')
EXEC sp_xml_removedocument @XmlHandler
问题解决