Jordec
Jordec

Reputation: 1564

XMLTable doesn't get data from children in XML

I have this chunk of XML data that I need to use XMLTable on to get the bulletinWorkl:id and the bulletinWork/outOfServices/outOfService/document:destinationName of.

<?xml version="1.0" encoding="ISO-8859-1"?>
<cern:bulletinWork id="5307cedc-2208-3701-9b9d-e69664b1ef31">
  <cern:outOfServices>
    <cern:outOfService id="e95d491b-2876-3e08-901f-b0f79be86bfb">
      <cern:document destinationName="MonsA" type="S427"></cern:document>
    </cern:outOfService>
    <cern:outOfService id="fab04992-a33f-3a8c-ad16-29cd54fb93d6">
      <cern:document destinationName="MonsB" type="S427"></cern:document>
    </cern:outOfService>
  </cern:outOfServices>
</cern:bulletinWork>

I'm trying to run this query and I succesfully get back the ID attribute in bulletinWork, but the destinationName from bulletinWork/outOfServices/outOfService/document is empty.

select X.Id, X.DestinationName from S1589_XML_Bulletin, XMLTABLE(
    '$d/*:bulletinWork'
    PASSING XMLTYPE(XML_RAW) as "d"
    COLUMNS
        Id                  VARCHAR2(50) PATH   '@*:id',
        DestinationName     VARCHAR2(50) PATH   '*:outOfServices/*:outOfService/*:document/*:destinationName'
) AS X

Anybody sees what I'm doing wrong here? I need to get:

Id                                       DestinationName
-------------------------------------    ------------------
5307cedc-2208-3701-9b9d-e69664b1ef31     MonsA
5307cedc-2208-3701-9b9d-e69664b1ef31     MonsB

Upvotes: 0

Views: 209

Answers (1)

Arkadiusz Łukasiewicz
Arkadiusz Łukasiewicz

Reputation: 6346

 COLUMNS
        Id                  VARCHAR2(50) PATH   '@id',
        DestinationName     VARCHAR2(50) PATH   'string-join(distinct-values(*:outOfServices/*:outOfService/*:document/@destinationName),", ")'

You don't have to use namespace for unprefixed attributes. Their namespaces is defined be parent element.

In example xml there is multiple cern:outOfService this is reason why i'm using string-join and distinct-values

Update:

1) It's longer but more clear for me. The Join of two xml tables.

    select * from xmltable('*:bulletinWork' passing xmltype('<cern:bulletinWork id="5307cedc-2208-3701-9b9d-e69664b1ef31" xmlns:cern="aaa">
      <cern:outOfServices>
        <cern:outOfService id="e95d491b-2876-3e08-901f-b0f79be86bfb">
          <cern:document destinationName="MonsA" type="S427"></cern:document>
        </cern:outOfService>
        <cern:outOfService id="fab04992-a33f-3a8c-ad16-29cd54fb93d6">
          <cern:document destinationName="MonsB" type="S427"></cern:document>
        </cern:outOfService>
      </cern:outOfServices>
    </cern:bulletinWork>')
    COLUMNS
            Id                  VARCHAR2(50) PATH   '@id',
            outOfServices       xmltype   path '*:outOfServices'  
    ) t1 
    ,xmltable('*:outOfServices/*:outOfService' passing t1.outOfServices 
    COLUMNS DestinationName     VARCHAR2(50) PATH   '*:document/@destinationName')

2) Accessing parent node from child node.

select * from xmltable('*:bulletinWork/*:outOfServices/*:outOfService' passing xmltype('<cern:bulletinWork id="5307cedc-2208-3701-9b9d-e69664b1ef31" xmlns:cern="aaa">
  <cern:outOfServices>
    <cern:outOfService id="e95d491b-2876-3e08-901f-b0f79be86bfb">
      <cern:document destinationName="MonsA" type="S427"></cern:document>
    </cern:outOfService>
    <cern:outOfService id="fab04992-a33f-3a8c-ad16-29cd54fb93d6">
      <cern:document destinationName="MonsB" type="S427"></cern:document>
    </cern:outOfService>
  </cern:outOfServices>
</cern:bulletinWork>')
COLUMNS
        Id                  VARCHAR2(50) PATH   './../../@id', 
        DestinationName     VARCHAR2(50) PATH   '*:document/@destinationName'
)

Upvotes: 2

Related Questions