Selecting the attribute value of given xmltype [message #535160] |
Mon, 12 December 2011 08:16 |
|
saipradyumn
Messages: 419 Registered: October 2011 Location: Hyderabad
|
Senior Member |
|
|
Hi ,
I need to get the value of an attribute for the given attribute name of xmltype. Please help me
DROP TABLE some_things ;
create table some_things (object_value CLOB);
insert into some_things values
('<?xml version="1.0" encoding="UTF-8"?>
<indexAttributes>
<attribute name="PENSION_APPLICATION.APPLICATION_ID" value="App123 "/>
<attribute name="PERSON.ALT_IDENTIFIER" value="9046095052"/>
<attribute name="PERSON.SSN" value="741852963 "/>
</indexAttributes>');
insert into some_things values
('<?xml version="1.0" encoding="UTF-8"?>
<indexAttributes>
<attribute name="PENSION_APPLICATION.APPLICATION_ID" value="App256 "/>
<attribute name="PERSON.ALT_IDENTIFIER" value="9046095052"/>
<attribute name="PERSON.SSN" value="859632147 "/>
</indexAttributes>');
select extractValue(sys.xmltype.createXML(object_value),
'/indexAttributes.PERSON.SSN') ssn ,
extractValue(sys.xmltype.createXML(object_value),
'/indexAttributes.PENSION_APPLICATION.APPLICATION_ID')APPLICATION_ID
from some_things;
select * from some_things;
Now i need to get the application id and the corresponding ssn number. I tried by using extractValue function. But it's not retuning any value
Thanks
Sai pradyumn
[Updated on: Mon, 12 December 2011 08:22] by Moderator Report message to a moderator
|
|
|
|
|
Re: Selecting the attribute value of given xmltype [message #535190 is a reply to message #535160] |
Mon, 12 December 2011 14:16 |
transfer
Messages: 53 Registered: August 2007 Location: transfer
|
Member |
|
|
Simply for information, starting in version 11G Oracle suggests using XMLTABLE for this, although Michel's solution is just fine. For anyone interested in an XMLTABLE variant:SELECT APPLICATION_ID, SSN
FROM SOME_THINGS, XMLTABLE (
'$X' PASSING xmltype(object_value, null, 1, 1) AS X
COLUMNS
APPLICATION_ID VARCHAR2(63) PATH '//attribute[@name="PENSION_APPLICATION.APPLICATION_ID"]/@value',
SSN INT PATH '//attribute[@name="PERSON.SSN"]/@value'
); I shamelessly copied the PATH expressions from Michel. Notice that XMLTABLE lets you define the datatype: SSN is converted to an integer, whereas EXTRACTVALUE returns VARCHAR2 unless you reference an XML schema.
|
|
|
|