使用 OpenXML 粉碎 XML
Shredding XML in using OpenXML
我正在尝试粉碎以下 XML,但我无法使用 OPENXML 构造获得任何结果,而且我的输出看起来不正确。关于如何重写这个有什么建议吗?
<?xml version="1.0" encoding="UTF-8"?> <results
xmlns="https://crr.clm.ibmcloud.com/rs/query/1111/dataservice/ns"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="https://crr.clm.ibmcloud.com/rs/query/1111/dataservice/ns
https://crr.clm.ibmcloud.com/rs/query/1111/dataservice/xsd">
<result>
<a>a1</a>
<b>2</b>
<c>a1332</c>
<d>text.</d>
<e>Risk 2</e>
<f> </f>
<g>a123</g>
<h>1223324aaa</h>
<i>l1245</i>
<j>Complete</j>
<k>Not yet reported</k> </result>
请注意以下是我正在使用的代码片段
DECLARE @xml XML;
DECLARE @idoc INT;
SELECT @xml = CONVERT(XML, cast(results AS VARCHAR(MAX)), 2) FROM stg.requirements;
EXEC sys.sp_xml_preparedocument @idoc OUTPUT
,@xml
,'<results xmlns="https://crr.clm.ibmcloud.com/rs/query/1111/dataservice/ns" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:schemaLocation="https://crr.clm.ibmcloud.com/rs/query/1111/dataservice/ns https://crr.clm.ibmcloud.com/rs/query/1111/dataservice/xsd"/>';
SELECT *
FROM
OPENXML(@idoc, '/*', 1)
WITH ()
EXEC sys.sp_xml_removedocument @idoc;
--SELECT * FROM #temp
DROP TABLE IF EXISTS #temp
有几点需要指出。
(1) 您的 XML 格式不正确,所以我必须修复它。
(2) 从SQL Server 2005开始,使用XQuery语言,基于w3c标准,处理XML数据类型。保留 Microsoft 专有的 OPENXML
及其伙伴 sp_xml_preparedocument
和 sp_xml_removedocument
只是为了向后兼容已过时的 SQL Server 2000。这就是为什么使用 .nodes()
(3) 应始终考虑命名空间。
(4) .value()
方法中正确的 SQL 服务器数据类型。
SQL
DECLARE @xml XML = '<?xml version="1.0" encoding="UTF-8"?>
<results xmlns="https://crr.clm.ibmcloud.com/rs/query/1111/dataservice/ns"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="https://crr.clm.ibmcloud.com/rs/query/1111/dataservice/ns https://crr.clm.ibmcloud.com/rs/query/1111/dataservice/xsd">
<result>
<a>a1</a>
<b>2</b>
<c>2020-02-15</c>
<d>text.</d>
</result>
<result>
<a>a7</a>
<b>25</b>
<c>2020-01-25</c>
<d>Another text</d>
</result>
</results>';
;WITH xmlnamespaces (DEFAULT 'https://crr.clm.ibmcloud.com/rs/query/1111/dataservice/ns')
SELECT c.value('(a/text())[1]', 'VARCHAR(10)') AS a
, c.value('(b/text())[1]', 'INT') AS b
, c.value('(c/text())[1]', 'DATE') AS c
, c.value('(d/text())[1]', 'VARCHAR(30)') AS d
FROM @xml.nodes('/results/result') AS t(c);
Output
+----+----+------------+--------------+
| a | b | c | d |
+----+----+------------+--------------+
| a1 | 2 | 2020-02-15 | text. |
| a7 | 25 | 2020-01-25 | Another text |
+----+----+------------+--------------+
我正在尝试粉碎以下 XML,但我无法使用 OPENXML 构造获得任何结果,而且我的输出看起来不正确。关于如何重写这个有什么建议吗?
<?xml version="1.0" encoding="UTF-8"?> <results
xmlns="https://crr.clm.ibmcloud.com/rs/query/1111/dataservice/ns"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="https://crr.clm.ibmcloud.com/rs/query/1111/dataservice/ns
https://crr.clm.ibmcloud.com/rs/query/1111/dataservice/xsd">
<result>
<a>a1</a>
<b>2</b>
<c>a1332</c>
<d>text.</d>
<e>Risk 2</e>
<f> </f>
<g>a123</g>
<h>1223324aaa</h>
<i>l1245</i>
<j>Complete</j>
<k>Not yet reported</k> </result>
请注意以下是我正在使用的代码片段
DECLARE @xml XML;
DECLARE @idoc INT;
SELECT @xml = CONVERT(XML, cast(results AS VARCHAR(MAX)), 2) FROM stg.requirements;
EXEC sys.sp_xml_preparedocument @idoc OUTPUT
,@xml
,'<results xmlns="https://crr.clm.ibmcloud.com/rs/query/1111/dataservice/ns" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:schemaLocation="https://crr.clm.ibmcloud.com/rs/query/1111/dataservice/ns https://crr.clm.ibmcloud.com/rs/query/1111/dataservice/xsd"/>';
SELECT *
FROM
OPENXML(@idoc, '/*', 1)
WITH ()
EXEC sys.sp_xml_removedocument @idoc;
--SELECT * FROM #temp
DROP TABLE IF EXISTS #temp
有几点需要指出。
(1) 您的 XML 格式不正确,所以我必须修复它。
(2) 从SQL Server 2005开始,使用XQuery语言,基于w3c标准,处理XML数据类型。保留 Microsoft 专有的 OPENXML
及其伙伴 sp_xml_preparedocument
和 sp_xml_removedocument
只是为了向后兼容已过时的 SQL Server 2000。这就是为什么使用 .nodes()
(3) 应始终考虑命名空间。
(4) .value()
方法中正确的 SQL 服务器数据类型。
SQL
DECLARE @xml XML = '<?xml version="1.0" encoding="UTF-8"?>
<results xmlns="https://crr.clm.ibmcloud.com/rs/query/1111/dataservice/ns"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="https://crr.clm.ibmcloud.com/rs/query/1111/dataservice/ns https://crr.clm.ibmcloud.com/rs/query/1111/dataservice/xsd">
<result>
<a>a1</a>
<b>2</b>
<c>2020-02-15</c>
<d>text.</d>
</result>
<result>
<a>a7</a>
<b>25</b>
<c>2020-01-25</c>
<d>Another text</d>
</result>
</results>';
;WITH xmlnamespaces (DEFAULT 'https://crr.clm.ibmcloud.com/rs/query/1111/dataservice/ns')
SELECT c.value('(a/text())[1]', 'VARCHAR(10)') AS a
, c.value('(b/text())[1]', 'INT') AS b
, c.value('(c/text())[1]', 'DATE') AS c
, c.value('(d/text())[1]', 'VARCHAR(30)') AS d
FROM @xml.nodes('/results/result') AS t(c);
Output
+----+----+------------+--------------+
| a | b | c | d |
+----+----+------------+--------------+
| a1 | 2 | 2020-02-15 | text. |
| a7 | 25 | 2020-01-25 | Another text |
+----+----+------------+--------------+