Tom Debus
Tom Debus

Reputation: 23

Extracting XML sub-tags from a clob in Oracle via SQL

I have the following CLOB XML Fragment in the DB attribute "object_body":

<object stamp="8fc630bd-f1f5-43ac-b9d2-3b9db9054c75_1481811010527" type="Aggregation" version="12">
  <property name="name" value="COA_Cash_Position" valueType="string"/>
  <property name="description" value="COA_Cash_Position" valueType="string"/>
  <property name="objectId" value="ef4296f1-6af6-4de8-83fe-fa01cf327d87" valueType="string"/>
  <property name="branchId" value="01ecc9ed-27f3-42cb-a44d-2cec221ec8fa" valueType="string"/>
  <property name="models" valueType="table">
    <object type="ModelBasedTask:modelEntry" version="3">
      <property name="dataModel" valueType="url">DataModel["ModifyModel[COA_Cash_Positions_BHS_Unadjusted].STAGING:X_NRS_BSL40K_BHS"{ModifyModel[ff81e5f2-2ef6-437a-b113-67f7c0dd5e53].cdf308fc-520d-4611-a8db-4e6863e216b8:9ffcd08e-732a-4270-b15f-f4775bcc69db}]</property>
      <property name="instanceSelectionRule" valueType="object">
        <object type="InstanceSelectionRule" version="3">
          <property name="instanceDateRule" valueType="object">
            <object type="InstanceSelectionRule:rule" version="3">
              <property name="type" value="EQUAL" valueType="string"/>
            </object>
          </property>
          <property name="instanceKeyRules" valueType="table"/>
        </object>
      </property>
    </object>
  </property>
</object>

Aim is to extract the value of the property tag with name=”description”
=> to produce “COA_Cash_Position”

Tried all variations of:

EXTRACTVALUE(xmltype(object.object_body), '/object/property[@name="description"].getStringVal()'),

If I loose the .getStringVal() the query works but obviously only provides empty results (as the property tag itself doesn't include any value).

Get mostly error messages the likes of:

ORA-31011: XML parsing failed ORA-19202: Error occurred in XML processing LPX-00601: Invalid token in: '/object/property[@name="description"]. getStringVal()'

What easily works is extracting the values between two xml tags but getting a specific property value (like here the "value" of the tag with name="description") seems to evade the XML extract possibilities.

Any help is warmly appreciated...

Upvotes: 2

Views: 11734

Answers (3)

chrisis
chrisis

Reputation: 1993

Assuming you mean the value of the attribute @value the path you want is

/object/property[@name='description']/@value

Upvotes: 1

Mr. Bhosale
Mr. Bhosale

Reputation: 3106

Your XML contains some errors. We can achieve your expected output using REGEXP in oracle. Check below query.

 SELECT REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_SUBSTR (object_body, 'description" value=".*.".v', 1), 'description" value="', '', 1, 1) ,'" v','',1,1)   
 FROM Table

Check Demo here.

Output IS: COA_Cash_Position

Upvotes: -1

MT0
MT0

Reputation: 168041

EXTRACTVALUE is deprecated in Oracle 12 - it is being replaced by XMLTABLE or XMLQUERY.

SELECT x.description
FROM   your_table t
       CROSS JOIN
       XMLTABLE(
         '//object/'
         PASSING XMLTYPE( t.your_clob_column )
         COLUMNS description VARCHAR2(4000) PATH './property[@name="description"]/@value'
       ) x;

or

SELECT XMLQUERY(
         '//object/property[@name="description"]/@value'
         PASSING XMLTYPE( your_clob_column )
         RETURNING CONTENT
       ).getStringVal()
FROM   your_table;

But if you do want to use EXTRACTVALUE you can do:

SELECT EXTRACTVALUE(
         XMLTYPE( your_clob_column ),
         '//object/property[@name="description"]/@value'
       )
FROM   your_table;

Upvotes: 5

Related Questions