Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

XMLTable - extract non-sibling but related XML information

Jason_(A_Non)Dec 12 2022

I'm trying to figure out how to implement a pure SQL solution for a client that is using Oracle 11.2.0.4. They are trying to track submission/error information for their business purposes. Eventually they want this SQL used within a reporting tools (Crystal, SQL Server Reporting Services, etc), hence the desire to have this done in SQL only.
Here's the test sample I've setup so far, then I'll explain

with tran_tbl as
(select xmltype('<MessageResponse>
    <Data>
        <Detail>
            <ID>1s</ID>
        </Detail>
        <Detail>
            <ID>2s</ID>
        </Detail>
    </Data>
    <Status>
        <OverallStatus>Success</OverallStatus>
    </Status>
</MessageResponse>') msg from dual union all
 select xmltype('<MessageResponse>
    <Data>
        <Detail>
            <ID>1p</ID>
        </Detail>
        <Detail>
            <ID>2p</ID>
        </Detail>
        <Detail>
            <ID>3p</ID>
        </Detail>
    </Data>
    <Status>
        <OverallStatus>Partial</OverallStatus>
        <Errors>
            <DetailID>2p</DetailID>
            <ErrorMsg>Wingnut</ErrorMsg>
        </Errors>
        <Errors>
            <DetailID>3p</DetailID>
            <ErrorMsg>Cornnut</ErrorMsg>
        </Errors>
    </Status>
</MessageResponse>') from dual)
-- Using a WITH table to keep it simple for you
select xt.dtl_id
  from tran_tbl tt
       inner join xmltable('MessageResponse/Data/Detail/ID'
                           passing tt.msg
                           columns
                           dtl_id   varchar2(10) path '.') xt on (1=1);

That outputs

DTL_ID
1s
2s
1p
2p
3p

What I desire to see is

DTL_ID   ErrMsg
1s
2s
1p
2p       Wingnut
3p       Cornnut

As you can see, it simply joins any information Status/Errors/ErrorMsg with the corresponding Data/Detail/ID entry.
The response message that is simulated via the WITH table contains information returned to the submitter. It includes identifiers for each detail submitted (Detail/ID) within the Data node. The Status node contains the results of the processing, both with an overall status and the first error that was encountered while processing the Detail.
I'm struggling with associating information under the Status node for a given DetailID back to the corresponding Detail/ID under Data when using Oracle 11.2.0.4. If this was 12.2 (?) or later, I know Oracle has expanded XMLTable processing so information outside the selected node can be accessed. For 11.2.0.4, this is probably a XMLQuery solution answer, for which I struggle with.
Suggestions/questions?

This post has been answered by Solomon Yakobson on Dec 13 2022
Jump to Answer
Comments
Post Details
Added on Dec 12 2022
8 comments
272 views