Parsing XML with XMLTable n Oracle - sql

I have one xml which holds customer and order details. I am parsing it using XMLTable. I think I am giving the correct XPath but I am getting no output.
Here is what I have tried. Hoping for assistance.
**DECLARE
l_raw_xml CLOB:= '<?xml version="1.0" encoding="utf-8"?>
<Root
xmlns="http://www.adventure-works.com">
<Customers>
<Customer CustomerID="GREAL">
<CompanyName>Great Lakes Food Market</CompanyName>
<ContactName>Howard Snyder</ContactName>
<ContactTitle>Marketing Manager</ContactTitle>
<Phone>(503) 555-7555</Phone>
<FullAddress>
<Address>2732 Baker Blvd.</Address>
<City>Eugene</City>
<Region>OR</Region>
<PostalCode>97403</PostalCode>
<Country>USA</Country>
</FullAddress>
</Customer>
</Customers>
<Orders>
<Order>
<CustomerID>LETSS</CustomerID>
<EmployeeID>6</EmployeeID>
<OrderDate>1997-11-10T00:00:00</OrderDate>
<RequiredDate>1997-12-08T00:00:00</RequiredDate>
<ShipInfo ShippedDate="1997-11-21T00:00:00">
<ShipVia>2</ShipVia>
<Freight>45.97</Freight>
<ShipName>Let Stop N Shop</ShipName>
<ShipAddress>87 Polk St. Suite 5</ShipAddress>
<ShipCity>San Francisco</ShipCity>
<ShipRegion>CA</ShipRegion>
<ShipPostalCode>94117</ShipPostalCode>
<ShipCountry>USA</ShipCountry>
</ShipInfo>
</Order>
</Orders>
</Root>';
l_xml_type XMLTYPE:=XMLTYPE.createXML(l_raw_xml);
cursor c_make_xml_object
is
SELECT x.*,
y.*
FROM XMLTABLE('/Root/Customers/Customer'
PASSING l_xml_type
COLUMNS
CustomerID VARCHAR2(255) PATH '#CustomerID',
CompanyName VARCHAR2(255) PATH 'CompanyName',
FullAddress XMLTYPE PATH 'FullAddress') x,
XMLTABLE('/FullAddress'
PASSING x.FullAddress
COLUMNS
Address VARCHAR2(255) PATH 'Address',
City VARCHAR2(255) PATH 'City') y ;
BEGIN
FOR rec IN c_make_xml_object
loop
DBMS_OUTPUT.PUT_LINE(rec.CustomerID);
DBMS_OUTPUT.PUT_LINE(rec.CompanyName);
END LOOP;
END;
/**
Do I have to account for the namespace? I had added namespace code but it did not work/

Change to this to include a default namespace:
FROM XMLTABLE(xmlnamespaces(default 'http://www.adventure-works.com'),
'/Root/Customers/Customer' ....
....
XMLTABLE(xmlnamespaces(default 'http://www.adventure-works.com'), '/FullAddress'
.....

Yes, a proper namespace definition is needed within the SQL query such as below :
SELECT x.*, y.*
FROM XML_TAB t,
XMLTABLE(XMLnamespaces('http://www.adventure-works.com' as "ns"),
'ns:Root/ns:Customers/ns:Customer'
PASSING t.xml_data
COLUMNS
CustomerID VARCHAR2(255) PATH '#CustomerID',
CompanyName VARCHAR2(255) PATH 'ns:CompanyName',
FullAddress XMLTYPE PATH 'ns:FullAddress'
) x,
XMLTABLE(XMLnamespaces('http://www.adventure-works.com' as "ns"),
'ns:FullAddress'
PASSING x.FullAddress
COLUMNS
Address VARCHAR2(255) PATH 'ns:Address',
City VARCHAR2(255) PATH 'ns:City') y ;
Demo with SQL
Demo with PL/SQL

Related

XML PARSING ON ORACLE (HELP :/)

I have xml data like this;
<NctsYYSResponse xmlns="http://http://www.w3.org/2001/XMLSchema-instance">
<Root>
<Response xmlns="">
<RefID>11084672</RefID>
<Guid>b2231713</Guid>
<Durum>Thank You!</Durum>
</Response>
</Root>
</NctsYYSResponse>
I tried this sql codes but i could not get result;
1- SELECT EXTRACTVALUE (XMLType(xml_respond) , '/NctsYYSResponse/Root/Response/Guid') guid from table1
2-SELECT x.GUID,x.STATUS FROM table1,XMLTABLE(
'/NctsYYSResponse/Root/Response'
PASSING XMLTYPE(xml_respond)
COLUMNS
"GUID" VARCHAR2(20) PATH 'Guid',
"STATUS" VARCHAR2(100) PATH 'Durum') x ;
3-select x.GUID from table1, xmltable(
xmlnamespaces ('http://http://www.w3.org/2001/XMLSchema-instance' as "a" ,
default ''), '/NctsYYSResponse/Root/a:Response'
passing XMLType(xml_respond)
columns
"GUID" VARCHAR2(20) path 'Guid'
) x ;
Return null at all of them
If i change NctsYYSResponse 's type xmlns to xmlns:xsi , i can get result. But i need to get result when it is xmlns . Please help me :/
You were very close with your last attempt; you just applied the a namespace to the wrong elements:
select x.guid, x.status
from table1
cross apply xmltable (
xmlnamespaces (
'http://http://www.w3.org/2001/XMLSchema-instance' as "a",
default ''
),
'/a:NctsYYSResponse/a:Root/Response'
passing XMLType(xml_respond)
columns
guid VARCHAR2(20) path 'Guid',
status VARCHAR2(100) PATH 'Durum'
) x;
GUID STATUS
-------------------- ---------------
b2231713 Thank You!
db<>fiddle

import xml to oracle database table

I have this sample xml my requirement is to parse it and fetch values from it's various nodes and insert them into one of oracle table.
<?xml version="1.0" encoding="UTF-8"?>
<Reporting>
<Selection>69</Selection>
<MonthEndDate>9/30/2016</MonthEndDate>
<Email>
<Name>abc</Name>
<Address>abc#gmail.com</Address>
</Email>
<Request>
<Port_id_list>
<Port_id>1901</Port_id>
<Port_id>1902</Port_id>
<Port_id>1903</Port_id>
</Port_id_list>
</Request>
</Reporting>
How can we do this?
Please help.
EDITED
For example:
WITH my_data AS
(SELECT xmltype('<?xml version="1.0" encoding="UTF-8"?>
<Reporting>
<Selection>69</Selection>
<MonthEndDate>9/30/2016</MonthEndDate>
<Email>
<Name>abc</Name>
<Address>abc#gmail.com</Address>
</Email>
<Request>
<Port_id_list>
<Port_id>1901</Port_id>
<Port_id>1902</Port_id>
<Port_id>1903</Port_id>
</Port_id_list>
</Request>
</Reporting>') my_xml
FROM dual)
SELECT extractValue(md.my_xml, 'Reporting/Selection/text()') selection_id,
extractValue(value(port_ids), 'Port_id/text()') port_id
FROM my_data md,
TABLE(XMLSequence (md.my_xml.extract ('Reporting/Request/Port_id_list/Port_id'))) port_ids;
If your XML has node with children create one-to-many with TABLE and XMLSequence.
You should preferably use XMLTABLE as the extractValue was deprecated
Here an example selecting teh ports with the (denormalized) parten attributes.
I added also posr sequence to preserve order of the ports.
WITH t AS
(SELECT xmltype('<?xml version="1.0" encoding="UTF-8"?>
<Reporting>
<Selection>69</Selection>
<MonthEndDate>9/30/2016</MonthEndDate>
<Email>
<Name>abc</Name>
<Address>abc#gmail.com</Address>
</Email>
<Request>
<Port_id_list>
<Port_id>1901</Port_id>
<Port_id>1902</Port_id>
<Port_id>1903</Port_id>
</Port_id_list>
</Request>
</Reporting>') xml
FROM dual)
select
x.Selection,x.MonthEndDate,x.emailName, x.emailAddress,
o.port_seq, o.port_id
from t,
XMLTable(
'for $i in /Reporting
return $i'
passing t.xml
columns
Selection varchar2(30) path 'Selection',
MonthEndDate varchar2(30) path 'MonthEndDate',
emailName varchar2(30) path 'Email/Name',
emailAddress varchar2(30) path 'Email/Address',
Port_id_list XMLType path '//Port_id'
) x,
XMLTable(
'./Port_id'
passing (x.Port_id_list)
columns
port_seq for ordinality,
port_id varchar2(30) path '/Port_id'
) o
;
SELECTION MONTHENDDATE EMAILNAME EMAILADDRESS PORT_SEQ PORT_ID
------------------------------ ------------------------------ ------------------------------ ------------------------------ ---------- ------------------------------
69 9/30/2016 abc abc#gmail.com 1 1901
69 9/30/2016 abc abc#gmail.com 2 1902
69 9/30/2016 abc abc#gmail.com 3 1903

Check length of column in XMLTable

This is my XML in an XMLTYPE column:
<customers>
<customer>
<name>abc</name>
<surname>abc</surname>
<address>abc</address>
</customer>
<customer>
<name>abc</name>
<surname>abc</surname>
<address>abc</address>
</customer>
</customers>
I want that if the length of a field exceeds the allowed length, 15 charcaters, then use a default value instead.
In the guide for DB2 I found this example:
select ...from .., XMLTABLE
('$INFO/customerinfo*' passing
columns
city varchar(16) path a'addr/city(if(string-lenght(.)<=16) then . else "Error!")'
) ..
How can I do it in Oracle? This is my current XMLTABLE attempt:
select ..XMLTABLE
('customers/*' passing ..
columns
...
"address" varchar(15) path 'indirizzo/(if(lenght(.)<=15) then . else "Error!")'
) data;
But that gets:
ORA-19237. unable to resolve call to function -fn:length
You have typos and inconsistent naming so it's hard to tell exactly what you're really doing to get that error, but it works if you use the string-length() function from your DB2 example, rather than length() (or lenght()):
"address" varchar2(15) path 'address/(if(string-length(.)<=15) then . else "Error!")'
With modified data to trigger the error behaviour:
select *
from XMLTABLE ('customers/*'
passing xmltype('<customers>
<customer>
<name>abc</name>
<surname>abc</surname>
<address>abc def ghi jkl mno</address>
</customer>
<customer>
<name>abc</name>
<surname>abc</surname>
<address>abc</address>
</customer>
</customers>')
columns
"address" varchar(15) path 'address/(if(string-length(.)<=15) then . else "Error!")'
) data;
address
---------------
Error!
abc

Extracting a node where xmlns is set to blank

I'm having difficulties extracting the value from certain nodes in an XML structure using XMLTABLE. Below query works perfectly when you remove the xmlns="" attribute from the SubListItem nodes. And as you can see, the XML already has a default namespace. I honestly have no clue how I can deal with this "blanking out" of the namespace on certain nodes like this.
For further clarification, the creation of this XML is not within my control and is provided by a third-party. I've also changed the names of the nodes and the content from the delivered files while preserving the structure of the XML.
SELECT f.airline, f.flightnumber, fl.gate
FROM xmltable(
xmlnamespaces(
default 'http://some/name.space',
'http://www.w3.org/2001/XMLSchema' as "xsd",
'http://www.w3.org/2001/XMLSchema-instance' as "xsi"
),
'Body/Flight'
passing xmltype(
'<?xml version="1.0" encoding="utf-16"?>
<Body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://some/name.space">
<Sender>
<System>ConnectionManagement</System>
</Sender>
<Flight>
<Airline>ABC</Airline>
<Number>1234</Number>
<SubList>
<SubListItem xmlns="">
<Gate>X</Gate>
</SubListItem>
<SubListItem xmlns="">
<Gate>Y</Gate>
</SubListItem>
<SubListItem xmlns="">
<Gate>Z</Gate>
</SubListItem>
</SubList>
</Flight>
</Body>'
)
columns airline varchar2(100) path 'Airline'
, flightNumber VARCHAR2(5) path 'Number'
, subList XMLTYPE path 'SubList'
) f
, xmltable (
xmlnamespaces( default 'http://some/name.space'),
'/SubList/SubListItem'
passing f.subList
columns gate varchar2(5) path 'Gate'
) fl
;
How can I target the Gate node when the XML looks like this?
Leave the default namespace alone in the second XMLTable, and specify a named namespace for the path you do have:
...
, xmltable (
xmlnamespaces( 'http://some/name.space' as "ns"),
'/ns:SubList/SubListItem'
passing f.subList
columns gate varchar2(5) path 'Gate'
) fl
;
AIRLINE FLIGH GATE
---------- ----- -----
ABC 1234 X
ABC 1234 Y
ABC 1234 Z
The SubList still has to match that, but as the child nodes don't the default is incorrect the way you have it. If you remove the xmlns="" as you mentioned in the question then that inherits the namespace from its parent, so your default works. With that override to no-namespace you can't use a default.

convert XML to table in oracle

I have a XML in the below format, store in column of XMLType in oracle database
<a>
<c>1</c>
<c>2</c>
</a>
I need to convert this in table format as
c
1
2
Any idea how to do this using SQL?
with data as
(select '<a><c>1</c><c>2</c></a>' xmlval
from dual)
( select c
from data d,
xmltable('/a/*' passing xmltype(d.xmlval)
columns
c varchar2(254) path '/c'
))
There is answer in this url.
For instance user daggett did this:
select *
FROM XMLTABLE('/person/row'
PASSING
xmltype('
<person>
<row>
<name>Tom</name>
<Address>
<State>California</State>
<City>Los angeles</City>
</Address>
</row>
<row>
<name>Jim</name>
<Address>
<State>California</State>
<City>Los angeles</City>
</Address>
</row>
</person>
')
COLUMNS
--describe columns and path to them:
name varchar2(20) PATH './name',
state varchar2(20) PATH './Address/State',
city varchar2(20) PATH './Address/City'
) xmlt
;
Thanks, it's work.
pISLEMLER CLOB;
select *
FROM XMLTABLE('/mydata/UCRET'
PASSING
xmltype('<mydata>'||pISLEMLER||'</mydata>
')
COLUMNS
--describe columns and path to them:
F_M_IS_ID varchar2(20) PATH './FMISID',
ISKONTO varchar2(20) PATH 'ISK'
) xmlt;