Home > Mobile >  Trying to extract a namespace value from an xmltype
Trying to extract a namespace value from an xmltype

Time:06-11

my BLOB (converted to XMLType) contains the following :

<?xml version="1.0" encoding="UTF-8"?><S2SCTScf:SCTScfBlkCredTrf xsi:schemaLocation="urn:S2SCTScf:xsd:$SCTScfBlkCredTrf SCTScfBlkCredTrf.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:S2SCTScf="urn:S2SCTScf:xsd:$SCTScfBlkCredTrf"><S2SCTScf:SndgInst>EBAPFRPA</S2SCTScf:SndgInst><S2SCTScf:RcvgInst>NWBKGB2X</S2SCTScf:RcvgInst><S2SCTScf:SrvcId>SCT</S2SCTScf:SrvcId><S2SCTScf:TstCode>P</S2SCTScf:TstCode><S2SCTScf:FType>SCF</S2SCTScf:FType><S2SCTScf:FileRef>NSCT220610000729</S2SCTScf:FileRef><S2SCTScf:RoutingInd>IND</S2SCTScf:RoutingInd><S2SCTScf:FileBusDt>2022-06-10</S2SCTScf:FileBusDt><S2SCTScf:FileCycleNo>11</S2SCTScf:FileCycleNo><S2SCTScf:FIToFICstmrCdtTrf xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02"><GrpHdr><MsgId>NSCT2206100007290000000000000000001</MsgId><CreDtTm>2022-06-10T09:52:54</CreDtTand

I wish to use SQL to extract the namespace value :

<S2SCTScf:FIToFICstmrCdtTrf xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02">

How would I do that - I tried XMLQuery but couldn't get it to work.

Many thanks

CodePudding user response:

You can use FLOWR to delete the child elements and just return that single element:

SELECT XMLQUERY(
         'declare namespace S2SCTScf="urn:S2SCTScf:xsd:$SCTScfBlkCredTrf";
         copy $e := /S2SCTScf:SCTScfBlkCredTrf/S2SCTScf:FIToFICstmrCdtTrf
         modify (
           for $i in $e/*
             return delete node $i
         )
         return $e'
         PASSING value RETURNING CONTENT
       ).getStringVal() AS xmlns_element
FROM   table_name

Which, for the sample data:

CREATE TABLE table_name (value) AS
SELECT XMLTYPE('<?xml version="1.0" encoding="UTF-8"?>
<S2SCTScf:SCTScfBlkCredTrf xsi:schemaLocation="urn:S2SCTScf:xsd:$SCTScfBlkCredTrf SCTScfBlkCredTrf.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:S2SCTScf="urn:S2SCTScf:xsd:$SCTScfBlkCredTrf">
  <S2SCTScf:SndgInst>EBAPFRPA</S2SCTScf:SndgInst>
  <S2SCTScf:RcvgInst>NWBKGB2X</S2SCTScf:RcvgInst>
  <S2SCTScf:SrvcId>SCT</S2SCTScf:SrvcId>
  <S2SCTScf:TstCode>P</S2SCTScf:TstCode>
  <S2SCTScf:FType>SCF</S2SCTScf:FType>
  <S2SCTScf:FileRef>NSCT220610000729</S2SCTScf:FileRef>
  <S2SCTScf:RoutingInd>IND</S2SCTScf:RoutingInd>
  <S2SCTScf:FileBusDt>2022-06-10</S2SCTScf:FileBusDt>
  <S2SCTScf:FileCycleNo>11</S2SCTScf:FileCycleNo>
  <S2SCTScf:FIToFICstmrCdtTrf xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02">
    <GrpHdr>
      <MsgId>NSCT2206100007290000000000000000001</MsgId>
      <CreDtTm>2022-06-10T09:52:54</CreDtTm>
    </GrpHdr>
  </S2SCTScf:FIToFICstmrCdtTrf>
</S2SCTScf:SCTScfBlkCredTrf>') FROM DUAL;

Outputs:

XMLNS_ELEMENT
<S2SCTScf:FIToFICstmrCdtTrf xmlns:S2SCTScf="urn:S2SCTScf:xsd:$SCTScfBlkCredTrf" xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02"/>

db<>fiddle here

CodePudding user response:

As this and the XML in your previous question both have the GrpHdr node, and you were trying to the MsgId from it then, you might simply be trying to get the namespace so you can pass it back into another XMLQuery call.

If that is the case then you could ignore the namespaces and use wildcards:

with q1 (Tdata) as (
  SELECT XMLtype(transportdata, nls_charset_id('AL32UTF8')) from paymentinterchange
)
select XMLQuery('//*:GrpHdr/*:MsgId/text()'
  passing Tdata
  returning content).getstringval()
from q1

which would get the message ID from this XML or the previous example. I've changed the character set to AL32UTF8 to match this XML's UTF-8 declaration, which could be a problem if you have a mix of encodings in your data.

db<>fiddle with the old and new XML.

  • Related