Solved

XML Query - Oracle

Posted on 2014-09-15
2
218 Views
Last Modified: 2014-10-02
Hi All, a while back I was helped on here with an XML query. The Query works but I am having trouble querying out a new object (signature URL)


The query returns all the required information, except not on the URL in signatureURL.


This is the Query.
SELECT DISTINCT 
       c.taxid,
       CASE WHEN c.taxid != '0' OR LENGTH(c.id) = 5 THEN c.taxid ELSE REGEXP_SUBSTR(c.id, '^[^- ]+') END chain,
       CASE WHEN LENGTH(c.id) = 5 THEN c.id ELSE REGEXP_SUBSTR(c.id, '[^- ]+$') END customer,
       CASE WHEN c.taxid is null then '0' else c.taxid end as taxid,
       c.mtime,
       o.owner,
       o.signature,
       o.sku,
       o.qty
  FROM xml_data_tab t,
       XMLTABLE(
           '//customer'
           PASSING t.xml_data
           COLUMNS cust_seq FOR ORDINALITY,
                   id VARCHAR2(50) PATH '/customer/id',
                   taxid VARCHAR2(50) PATH '/customer/taxID',
                   mtime VARCHAR2(50) PATH '/customer/billTo/mtime'
       ) c,
       XMLTABLE(
           '//lines'
           PASSING t.xml_data
           COLUMNS line_seq FOR ORDINALITY, node XMLTYPE PATH '.'
       ) l,
       XMLTABLE(
           '//object'
           PASSING l.node
           COLUMNS 
                   signature VARCHAR2(100) PATH '/object/signatureURL',
                   owner VARCHAR2(50) PATH '/object/owner',
                   sku VARCHAR2(50) PATH '/object/sku',
                   qty VARCHAR2(50) PATH '/object/qty'
       ) o
 WHERE  l.line_seq = c.cust_seq

Open in new window


This is the XML
<object type="hash">
<otherVersions type="list"/>
<percentDiscount type="null"/>
<entityType>SalesOrder</entityType>
<ownerName>John test</ownerName>
<mtime>2014-09-15T16:12:06Z</mtime>
<owner>johnt@test.com</owner>
<promoDetails type="null"/>
<shippingMethod>FEDEX</shippingMethod>
<id>#1140021</id>
<category>/api/v3/order_categories/4960</category>
<realVersionID type="null"/>
<paymentTerms>PREPAID</paymentTerms>
<uuid>da6ca3b0-7dcf-46b8-9ee7-c462fead60f4</uuid>
<ctimeOnServer>2014-09-15T16:12:06Z</ctimeOnServer>
<freeShipping type="boolean">False</freeShipping>
<realVersion type="null"/>
<startShipDate type="null"/>
<versionType type="null"/>
<mtimeOnServer>2014-09-15T16:12:31Z</mtimeOnServer>
<shipPartial type="boolean">True</shipPartial>
<userGroup>/api/v3/user_groups/2</userGroup>
<totalAmount>131.88</totalAmount>
<subtotalAmount>131.88</subtotalAmount>
<status>Open</status>
<customer type="hash">...</customer>
<shipTo type="hash">...</shipTo>
<defaultWarehouse>/api/v3/warehouses/158611</defaultWarehouse>
<sourceType>seller</sourceType>
<billTo type="hash">...</billTo>
<creditCard type="null"/>
<lastExported type="null"/>
<cdate>2014-09-15</cdate>
<photoURLs type="list"/>
<versionInfo type="null"/>
<signatureURL>
https://app.test.com/tempFile/b81f73fd-e09d-4786-9459-7976d49d7bb4-signature-1140021-1cfb47a5cd88f397e70b9e46b1d6422b08800f3b.png
</signatureURL>
<shippedStatus>Not Shipped</shippedStatus>
<priceLists type="list"/>
<totalOffDiscount type="null"/>
<ctime>2014-09-15T16:11:39Z</ctime>
<notes/>
<promoCode type="null"/>
<lines type="list">...</lines>
<cancelDate type="null"/>
<objID type="integer">1140021</objID>
<lastEdited>2014-09-15T16:12:31Z</lastEdited>
<customerPO type="null"/>
<realVersionUUID type="null"/>
<resource_uri>/api/v3/orders/1140021</resource_uri>
</object>

Open in new window

0
Comment
Question by:FutureDBA-
  • 2
2 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 40323643
Your objects aren't children of the l.node, they are at the parent, so you need to pass in the whole document

change
     PASSING l.node
to
     PASSING t.xml_data
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 40323648
alternatively, remove the "o" xmltable, and combine with the customer extractions

   SELECT DISTINCT
          c.taxid,
          CASE
              WHEN c.taxid != '0' OR LENGTH(c.id) = 5 THEN c.taxid
              ELSE REGEXP_SUBSTR(c.id, '^[^- ]+')
          END
              chain,
          CASE WHEN LENGTH(c.id) = 5 THEN c.id ELSE REGEXP_SUBSTR(c.id, '[^- ]+$') END customer,
          CASE WHEN c.taxid IS NULL THEN '0' ELSE c.taxid END AS taxid,
          c.mtime,
          c.owner,
          c.signature,
          c.sku,
          c.qty
     FROM xml_data_tab t,
          XMLTABLE(
              '/object'
              PASSING t.xml_data
              COLUMNS cust_seq FOR ORDINALITY,
                      id VARCHAR2(50) PATH '/object/customer/id',
                      taxid VARCHAR2(50) PATH '/object/customer/taxID',
                      mtime VARCHAR2(50) PATH '/object/customer/billTo/mtime',
                      signature VARCHAR2(100) PATH '/object/signatureURL',
                      owner VARCHAR2(50) PATH '/object/owner',
                      sku VARCHAR2(50) PATH '/object/sku',
                      qty VARCHAR2(50) PATH '/object/qty'
          ) c,
          XMLTABLE(
              '//lines'
              PASSING t.xml_data
              COLUMNS line_seq FOR ORDINALITY, node XMLTYPE PATH '.'
          ) l
         
    WHERE l.line_seq = c.cust_seq
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that useā€¦
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question