无法使用 SQL/OPENXML 查询 XML 文档
Unable to Query XML Document with SQL/OPENXML
我想使用SQL查询以下XML-文件:
<?xml version="1.0" encoding="UTF-8"?>
<GL_MarketDocument xmlns="urn:iec62325.351:tc57wg16:451-6:generationloaddocument:3:0">
<mRID>2f6f8b82348440b1b121bca06311945d</mRID>
<time_Period.timeInterval>
<start>2020-03-02T23:00Z</start>
<end>2020-03-03T18:30Z</end>
</time_Period.timeInterval>
</GL_MarketDocument>
使用此代码我想获得“mRID”的值:
DECLARE @DocHandle int
DECLARE @XmlDocument varchar(MAX)
SELECT @XMLDocument=I
FROM OPENROWSET (BULK 'TP_10V1001C--00013H_ENTSOE-ETP__00a8f07d-95bd-4075-b1f7-3f54ce6162f3.xml', SINGLE_BLOB) as ImportFile(I)
EXEC sp_xml_preparedocument @DocHandle OUTPUT, @XmlDocument, N'<root xmlns:d="urn:iec62325.351:tc57wg16:451-6:generationloaddocument:3:0"/>' ;
SELECT *
FROM OPENXML (@DocHandle, N'/d:GL_MarketDocument')
WITH ([mRID] varchar(50))
EXEC sp_xml_removedocument @DocHandle
然而,结果是:
mRID
NULL
如何获取 mRID ('2f6f8b82348440b1b121bca06311945d') 的正确值而不是 NULL?
Microsoft 专有 OPENXML
及其伙伴 sp_xml_preparedocument
和 sp_xml_removedocument
的保留只是为了与过时的 SQL Server 2000 向后兼容。它们的使用减少到非常多少数边缘案例。
从SQLServer 2005开始,在处理XML数据类型时,最好使用基于w3c标准的XQuery语言。
你的 XML 有一个默认的命名空间,所以应该考虑到它。
SQL,来自一个变量
DECLARE @XMLDocument XML =
N'<GL_MarketDocument xmlns="urn:iec62325.351:tc57wg16:451-6:generationloaddocument:3:0">
<mRID>2f6f8b82348440b1b121bca06311945d</mRID>
<time_Period.timeInterval>
<start>2020-03-02T23:00Z</start>
<end>2020-03-03T18:30Z</end>
</time_Period.timeInterval>
</GL_MarketDocument>';
WITH XMLNAMESPACES (DEFAULT 'urn:iec62325.351:tc57wg16:451-6:generationloaddocument:3:0')
SELECT c.value('(mRID/text())[1]','NVARCHAR(100)') AS mRID
FROM @XMLDocument.nodes('/GL_MarketDocument') AS t(c);
SQL,直接来自XML文件
WITH XMLNAMESPACES (DEFAULT 'urn:iec62325.351:tc57wg16:451-6:generationloaddocument:3:0')
, rs (xmlData) AS
(
SELECT TRY_CAST(BulkColumn AS XML)
FROM OPENROWSET(BULK N'e:\Temp\TP_10V1001C--00013H_ENTSOE-ETP__00a8f07d-95bd-4075-b1f7-3f54ce6162f3.xml', SINGLE_BLOB) AS x
)
SELECT c.value('(mRID/text())[1]','NVARCHAR(100)') AS mRID
FROM rs
CROSS APPLY xmlData.nodes('/GL_MarketDocument') AS t(c);
输出
+----------------------------------+
| mRID |
+----------------------------------+
| 2f6f8b82348440b1b121bca06311945d |
+----------------------------------+
我想使用SQL查询以下XML-文件:
<?xml version="1.0" encoding="UTF-8"?>
<GL_MarketDocument xmlns="urn:iec62325.351:tc57wg16:451-6:generationloaddocument:3:0">
<mRID>2f6f8b82348440b1b121bca06311945d</mRID>
<time_Period.timeInterval>
<start>2020-03-02T23:00Z</start>
<end>2020-03-03T18:30Z</end>
</time_Period.timeInterval>
</GL_MarketDocument>
使用此代码我想获得“mRID”的值:
DECLARE @DocHandle int
DECLARE @XmlDocument varchar(MAX)
SELECT @XMLDocument=I
FROM OPENROWSET (BULK 'TP_10V1001C--00013H_ENTSOE-ETP__00a8f07d-95bd-4075-b1f7-3f54ce6162f3.xml', SINGLE_BLOB) as ImportFile(I)
EXEC sp_xml_preparedocument @DocHandle OUTPUT, @XmlDocument, N'<root xmlns:d="urn:iec62325.351:tc57wg16:451-6:generationloaddocument:3:0"/>' ;
SELECT *
FROM OPENXML (@DocHandle, N'/d:GL_MarketDocument')
WITH ([mRID] varchar(50))
EXEC sp_xml_removedocument @DocHandle
然而,结果是:
mRID |
---|
NULL |
如何获取 mRID ('2f6f8b82348440b1b121bca06311945d') 的正确值而不是 NULL?
Microsoft 专有 OPENXML
及其伙伴 sp_xml_preparedocument
和 sp_xml_removedocument
的保留只是为了与过时的 SQL Server 2000 向后兼容。它们的使用减少到非常多少数边缘案例。
从SQLServer 2005开始,在处理XML数据类型时,最好使用基于w3c标准的XQuery语言。
你的 XML 有一个默认的命名空间,所以应该考虑到它。
SQL,来自一个变量
DECLARE @XMLDocument XML =
N'<GL_MarketDocument xmlns="urn:iec62325.351:tc57wg16:451-6:generationloaddocument:3:0">
<mRID>2f6f8b82348440b1b121bca06311945d</mRID>
<time_Period.timeInterval>
<start>2020-03-02T23:00Z</start>
<end>2020-03-03T18:30Z</end>
</time_Period.timeInterval>
</GL_MarketDocument>';
WITH XMLNAMESPACES (DEFAULT 'urn:iec62325.351:tc57wg16:451-6:generationloaddocument:3:0')
SELECT c.value('(mRID/text())[1]','NVARCHAR(100)') AS mRID
FROM @XMLDocument.nodes('/GL_MarketDocument') AS t(c);
SQL,直接来自XML文件
WITH XMLNAMESPACES (DEFAULT 'urn:iec62325.351:tc57wg16:451-6:generationloaddocument:3:0')
, rs (xmlData) AS
(
SELECT TRY_CAST(BulkColumn AS XML)
FROM OPENROWSET(BULK N'e:\Temp\TP_10V1001C--00013H_ENTSOE-ETP__00a8f07d-95bd-4075-b1f7-3f54ce6162f3.xml', SINGLE_BLOB) AS x
)
SELECT c.value('(mRID/text())[1]','NVARCHAR(100)') AS mRID
FROM rs
CROSS APPLY xmlData.nodes('/GL_MarketDocument') AS t(c);
输出
+----------------------------------+
| mRID |
+----------------------------------+
| 2f6f8b82348440b1b121bca06311945d |
+----------------------------------+