简体   繁体   中英

How to parse XML input field for specifc value using Oracle Sql?

I am having difficulty in writing a query that selects data from table (card_type). I need to parse the xml attribute(EXT_PROPERTIES_XML) only where the Name tag has "PARTNER_CD" in it. Below is the sample for the xml attribute.

    <Properties>
         <Property>
               <Name>PARTNER_CD</Name>
               <Value>LOBT</Value>
         </Property>
         <Property>
               <Name>REJECT_AT_PUMP</Name>
               <Value>true</Value>
         </Property>
         <Property>
              <Name>MAX_LITRE</Name>
              <Value>75</Value></Property>
    </Properties>

    <Properties>
         <Property>
               <Name>PARTNER_CD</Name>
               <Value>PET1</Value>
         </Property>
         <Property>
               <Name>REJECT_AT_PUMP</Name>
               <Value>true</Value>
         </Property>
         <Property>
              <Name>MAX_LITRE</Name>
              <Value>75</Value></Property>
    </Properties>

Desired Output:

NAME                VALUE
-------------------------
PARTNER_CD          LOBT
PARTNER_CD          PET1

This is what I tried..

 Select xmltype.createxml(EXT_PROPERTIES_XML).extract('/Properties/Property/Name/text()').getStringVal() as  Name, 
xmltype.createxml(EXT_PROPERTIES_XML).extract('/Properties/Property/Value/text()').getStringVal() as Value
    from card_type

Output that I got was:

           Name                         Value
PARTNER_CDREJECT_AT_PUMPMAX_LITRE       LOBTtrue75

Any help is really appreciated. Thanks.

You can use an XMLQuery or XMLTable to extract the data:

select x.*
from card_type c
cross join xmltable('/Properties/Property'
  passing xmltype(ext_properties_xml)
  columns name varchar2(20) path 'Name',
    value varchar2(20) path 'Value'
) x
where name = 'PARTNER_CD';

NAME                 VALUE              
-------------------- --------------------
PARTNER_CD           LOBT                
PARTNER_CD           PET1                

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM