Oracle clob读取字段参数的值

时间:2013-09-09 18:48:39

标签: xml oracle oracle11g clob extract-value

我有一个像下面的xml。 b4之后有许多节点,但在此之后它们无关紧要。

<Parent>
    <ServiceTag>sometag</ServiceTag>
    <Addl_Payloads>
    <Addl_PayloadCount>1</Addl_PayloadCount>
        <Addl_Payload>
            <Attrib Name="TYPE" Seq="1" Value="LICENSE"> </Attrib>
            <Attrib Name="ENTITLEMENT_ID" Value="sdfsfdsadfaasdfsadf1"></Attrib>
     </Addl_Payloads>
</Parent>

如何在Oracle 11g中构建查询,以便仅在Attrib name=ENTITLEMENT_ID时读取值。我试过这样的东西,但这给了我空值的列 基本上我想read = 'sdfsfdsadfaasdfsadf1'如果它存在

SELECT s.doc_id,
       extractValue(x.column_value, '/Parent/ServiceTag') as ST,
       extractValue(x.column_value, 
                   '/Parent/Addl_Payloads/Addl_Payload/Attrib[@Name="ENTITLEMENT_ID"]') as Value
 from LKMLOG.LKM_ORIG_ASB_MSG s
    , TABLE(
            XMLSequence(
                        xmltype( s.ASB_XML).extract(
                           '//Payload/PPIDInfoRequestMessage/PPIDData/Parent'
                            )
                       )  
         ) x
where  s.doc_id in (somevalues);

1 个答案:

答案 0 :(得分:0)

如果您想要提取,如果我理解正确,Value仅在节点的Name属性等于"ENTITLEMENT_ID"时,您可以按如下方式执行:

with t1(xml_col) as(
   select xmltype('
      <Addl_Payloads>
           <Addl_PayloadCount>1</Addl_PayloadCount>
           <Addl_Payload>
              <Attrib Name="TYPE" Seq="1" Value="LICENSE"> </Attrib>
              <Attrib Name="ENTITLEMENT_ID" Value="sdfsfdsadfaasdfsadf1"> </Attrib>
           </Addl_Payload>
       </Addl_Payloads>') from dual
  )
  select val
    from t1 t
    cross join xmltable('/Addl_Payloads/Addl_Payload/Attrib[@Name="ENTITLEMENT_ID"]'
                        passing t.xml_col
                        columns val varchar2(101) path '@Value')

结果:

 VAL
 -----------------------
 sdfsfdsadfaasdfsadf1

SqlFiddle Demo

注意:从Oracle 11gr2 XMLSequence函数开始不推荐使用 - 仍然是为了向后兼容。


附录

为了包含<ServiceTag>的值,可以通过以下方式更改上述查询:

with t1(xml_col) as(
   select xmltype('
      <Parent>
        <ServiceTag>sometag</ServiceTag>
        <Addl_Payloads>
             <Addl_PayloadCount>1</Addl_PayloadCount>
             <Addl_Payload>
                <Attrib Name="TYPE" Seq="1" Value="LICENSE"> </Attrib>
                <Attrib Name="ENTITLEMENT_ID" Value="sdfsfdsadfaasdfsadf1"> </Attrib>
                <Attrib Name="ENTITLEMENT_ID" Value="another_eaxmple"> </Attrib> 
             </Addl_Payload>
         </Addl_Payloads>
      </Parent>') from dual
  )
  select q.ST
       , s.val
    from t1 t
    left join xmltable('/Parent/ServiceTag'
                        passing t.xml_col
                        columns ST varchar2(101) path '.') q
     on (1=1)    
    left join xmltable('/Parent/Addl_Payloads/Addl_Payload/Attrib[@Name="ENTITLEMENT_ID"]'
                        passing t.xml_col
                        columns val varchar2(101) path '@Value') s
      on (1=1)

结果:

ST            Val 
--------------------------
sometag       sdfsfdsadfaasdfsadf1 
sometag       another_eaxmple 

SqlFiddle Demo