在Oracle数据库表列中提取XML

时间:2017-09-11 10:27:00

标签: sql xml oracle

我在Oracle数据库中有一个表'Users',其中包含'Values'列,其中包含XML数据,如下所示:

<Attributes>
  <Map>
    <entry key="first" value="Linda"/>
    <entry key="groups" value="Manager"/>
    <entry key="last" value="Davis"/>
    <entry key="locked" value="N"/>
    <entry key="status" value="A"/>
  </Map>
</Attributes>

我想提取它们:

Attributes_Values
=================
first = Linda
groups = Manager
last = Davis

我尝试了ExtractValue函数,但它返回错误,说“缺少右括号”。我的查询如下:

SELECT EXTRACTVALUE(Values, '/Attributes/Map/entry[@key='first']@value') "Attribute_Values" from 
Users;

2 个答案:

答案 0 :(得分:1)

引号而不是撇号。

[SkippableFact]
public void SomeTest()
{
    var canRunTest = CheckSomething();
    Skip.IfNot(canRunTest);

    // Normal test code
}

答案 1 :(得分:1)

SQL:

with t as (
select xmltype('<Attributes>
  <Map>
    <entry key="first" value="Linda"/>
    <entry key="groups" value="Manager"/>
    <entry key="last" value="Davis"/>
    <entry key="locked" value="N"/>
    <entry key="status" value="A"/>
  </Map>
</Attributes>') xml from dual
)

select dtl.extract('//@key').getStringVal() || ' = ' || dtl.extract('//@value').getStringVal() as "Attributes_Values"
from t s
    ,table(XMLSequence(s.xml.extract('Attributes/Map/entry'))) dtl

结果:

Attributes_Values
first = Linda
groups = Manager
last = Davis
locked = N
status = A