Skip to Main Content

Database Software

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!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Import UBL 2.1 xml

Daniyal AhmedDec 6 2021

BEGIN
DBMS_XMLSCHEMA.registerSchema(
SCHEMAURL => 'https://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-Invoice-2.1.xsd',
SCHEMADOC => bfilename('XMLDIR','/maindoc/UBL-Invoice-2.1.xsd')
);
END;
and i need to generate output based in UBL 2.1 invoice
any idea how to do it properly

Comments

Jason_(A_Non)

As far as I know, you have to build the XML yourself via methods such as XMLElement, XMLForest, XMLAgg, etc. Oracle did not have any way to automatically transform the results of a query into XML based upon a registered schema.

Daniyal Ahmed

Jason_(A_Non) as you suggested i tried to generate xml output/
my query :
SELECT XMLELEMENT("Invoice", XMLAttributes('xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"' AS "xmlns:xsi", 'xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"' as "xmlns:cac", 'xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"' as "xmlns:cbc", 'xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"' as "xmlns:ext" ),
XMLAGG(
XMLELEMENT("Invoice",
XMLFOREST(
rc.trx_number AS "cbc:ID",
rc.trx_date AS "cbc:IssueDate",
sum(tax_recoverable) AS "tax_recoverable",
sum(rcg.amount + nvl(tax_recoverable, 0)) AS "net_total"
)
)
)
) as resutl
--INTO l_xmltype
FROM
ra_customer_trx_all rc,
ra_customer_trx_lines_all rcl,
hz_cust_site_uses_all hzcs,
ra_cust_trx_line_gl_dist_all rcg,
gl_code_combinations cc,
HZ_CUST_ACCOUNTS RCA ,
ZX_PARTY_TAX_PROFILE ZPTP,
HZ_PARTIES HZP1
WHERE
rc.customer_trx_id = rcl.customer_trx_id
AND hzcs.site_use_id = rc.bill_to_site_use_id
AND cc.code_combination_id = rcg.code_combination_id
AND rcg.customer_trx_id = rc.customer_trx_id
AND rcl.customer_trx_line_id = rcg.customer_trx_line_id
and rc.SOLD_TO_CUSTOMER_ID=RCA.CUST_ACCOUNT_ID
and RCA.party_id=ZPTP.party_id
and ZPTP.party_id=HZP1.party_id
AND rcg.customer_trx_line_id IS NOT NULL
and line_type='LINE'
--AND rc.customer_trx_id = 8023
group by
rc.trx_number,
HZP1.PARTY_NAME,
'VAT # '||ZPTP.REP_REGISTRATION_NUMBER,
rc.trx_date ;

xml output:
<Invoice xmlns:xsi='xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"'
xmlns:cac='xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"'
xmlns:cbc='xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"'
xmlns:ext='xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"'>
<Invoice>
<cbc:ID xmlns:cbc='xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"'>10002</cbc:ID>
<cbc:IssueDate xmlns:cbc='xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"'>2019-12-31</cbc:IssueDate>
<tax_recoverable>4125</tax_recoverable>
<net_total>86625</net_total>
</Invoice>
</Invoice>
desire output

<Invoice xmlns:xsi='xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"'
xmlns:cac='xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"'
xmlns:cbc='xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"'
xmlns:ext='xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"'>
<Invoice>
<cbc:ID>10002</cbc:ID>
<cbc:IssueDate>2019-12-31</cbc:IssueDate>
<cbc:tax_recoverable>4125</cbc:tax_recoverable>
<cbc:net_total>86625</cbc:net_total>
</Invoice>
</Invoice>

Jason_(A_Non)

Well, this is part of your answer. I fixed your namespaces as they looked flat out incorrect. I also added the namespace prefix to the two missing nodes

WITH your_from_where_clause (trx_number, trx_date, tax_recoverable, amount) AS
 (SELECT 10002, date '2019-12-31', 4125, 82500 from dual)
SELECT XMLSERIALIZE(document
      XMLELEMENT("Invoice", 
                   XMLAttributes('urn:oasis:names:specification:ubl:schema:xsd:Invoice-2' AS "xmlns:xsi", 
                                 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2' as "xmlns:cac", 
                                 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' as "xmlns:cbc", 
                                 'urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2' as "xmlns:ext" ),
     XMLAGG(
      XMLELEMENT("Invoice",
       XMLFOREST(
        trx_number AS "cbc:ID",
        trx_date AS "cbc:IssueDate",
        sum(tax_recoverable) AS "cbc:tax_recoverable",
        sum(amount + nvl(tax_recoverable, 0)) AS "cbc:net_total"        
       )
      )
     ) 
    )
    )as resutl
FROM your_from_where_clause
group by 
 trx_number,
 trx_date ;

This version does throw the namespace definition onto every node. While technically valid in terms of XML, I do agree it is annoying. I think the solution is to run the XML through an XSLT to strip off the namespace declaration on all non-root nodes, but I can't seem to find some of those prior examples I know exist so I gave up on the hunt for now.
You have have better luck for this question over in the SQL & PL/SQL forum, simply because more eyes over there. https://community.oracle.com/tech/developers/categories/sql_and_pl_sql

1 - 3

Post Details

Added on Dec 6 2021
3 comments
1,374 views