如何在SQL Server中从xml类字符串中提取数据

时间:2021-10-25 15:27:32

Consider below XBRL string (xbrl is an xml derived language for financial reporting)

考虑一下XBRL字符串(XBRL是用于财务报告的xml派生语言)

<xbrl>
  <ifrs-full:cash contextRef="2017" unitRef="USD">1000</ifrs-full:cash>
  <ifrs-full:investment contextRef="2017" unitRef="USD">500</ifrs-full:investment>
  <ifrs-full:inventory contextRef="2017" unitRef="USD">800</ifrs-full:inventory>
</xbrl>

How can I extract this information in SQL Server and get something like this:

如何在SQL Server中提取这些信息并得到如下信息:

Element             |ContextRef|UnitRef|Value
--------------------+----------+-------+-------
ifrs-full:cash      |2017      |USD    |1000
ifrs-ful:investment |2017      |USD    | 500
ifrs-ful:investment |2017      |USD    | 800

1 个解决方案

#1


4  

You first need to fix the invalid XML (done below with a replace). Then you can use Xpath functions on it.

首先需要修复无效的XML(下面用替换完成)。然后可以在上面使用Xpath函数。

DECLARE @xbrlString VARCHAR(MAX) = '<xbrl>
      <ifrs-full:cash contextRef="2017" unitRef="USD">1000</ifrs-full:cash>
      <ifrs-full:investment contextRef="2017" unitRef="USD">500</ifrs-full:investment>
      <ifrs-full:inventory contextRef="2017" unitRef="USD">800</ifrs-full:inventory>
    </xbrl>';

DECLARE @xbrlXml XMl = REPLACE(@xbrlString, 
                               '<xbrl>', 
                               '<xbrl xmlns:ifrs-full="http://dummy">');

SELECT Element = 'ifrs-full:' + n.n.value('local-name(.)', 'SYSNAME'),
       contextRef = n.n.value('@contextRef', 'INT'),
       UnitRef = n.n.value('@unitRef', 'CHAR(3)'),
       Value = n.n.value('text()[1]', 'INT')
FROM   @xbrlXml.nodes('xbrl/*[namespace-uri() = "http://dummy"]') AS n(n); 

returns

返回

+----------------------+------------+---------+-------+
|       Element        | contextRef | UnitRef | Value |
+----------------------+------------+---------+-------+
| ifrs-full:cash       |       2017 | USD     |  1000 |
| ifrs-full:investment |       2017 | USD     |   500 |
| ifrs-full:inventory  |       2017 | USD     |   800 |
+----------------------+------------+---------+-------+

#1


4  

You first need to fix the invalid XML (done below with a replace). Then you can use Xpath functions on it.

首先需要修复无效的XML(下面用替换完成)。然后可以在上面使用Xpath函数。

DECLARE @xbrlString VARCHAR(MAX) = '<xbrl>
      <ifrs-full:cash contextRef="2017" unitRef="USD">1000</ifrs-full:cash>
      <ifrs-full:investment contextRef="2017" unitRef="USD">500</ifrs-full:investment>
      <ifrs-full:inventory contextRef="2017" unitRef="USD">800</ifrs-full:inventory>
    </xbrl>';

DECLARE @xbrlXml XMl = REPLACE(@xbrlString, 
                               '<xbrl>', 
                               '<xbrl xmlns:ifrs-full="http://dummy">');

SELECT Element = 'ifrs-full:' + n.n.value('local-name(.)', 'SYSNAME'),
       contextRef = n.n.value('@contextRef', 'INT'),
       UnitRef = n.n.value('@unitRef', 'CHAR(3)'),
       Value = n.n.value('text()[1]', 'INT')
FROM   @xbrlXml.nodes('xbrl/*[namespace-uri() = "http://dummy"]') AS n(n); 

returns

返回

+----------------------+------------+---------+-------+
|       Element        | contextRef | UnitRef | Value |
+----------------------+------------+---------+-------+
| ifrs-full:cash       |       2017 | USD     |  1000 |
| ifrs-full:investment |       2017 | USD     |   500 |
| ifrs-full:inventory  |       2017 | USD     |   800 |
+----------------------+------------+---------+-------+