0

I have a table 'Users' with column 'Values' in a Oracle database which has XML data like so:

<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>

I wish to extract them like:

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

I tried ExtractValue function but it returns error saying 'missing right parenthesis'. My query below:

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

2 Answers 2

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

Result:

Attributes_Values
first = Linda
groups = Manager
last = Davis
locked = N
status = A
Sign up to request clarification or add additional context in comments.

Comments

1

Quotation mark instead of the apostrophe.

SELECT EXTRACTVALUE(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>'), '/Attributes/Map/entry[@key="first"]/@value') "Attribute_Values" from dual; 

2 Comments

Works fine! Was wondering if we can extract multiple values by passing their relevant key identifiers. E.g. @key="first", @key"last".
Yes but not with extractvalue. I'thik extractvalue is deprecatd. SELECT * from xmltable( '/Attributes/Map/entry[@key="first" or @key="last" ]/@value' passing 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>') columns value varchar(200) path '.' )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.