Skip to content
Advertisement

How to split an XML string into parts with different keys

from XML column I wanna try get info.

Every client have more that one contract_key in XML, im using split_part and strpos for get needed information :

But problem is this function return the first match found:

ID Key amountR AVGamount
2253545 1 10000 50

this client in same XML have more Contract_key:

ID Key amountR AVGamount
2253545 5 70000 35
2253545 7 8000 70
2253545

My desired results, by ID get all keys from XML and what info after key until next key :

ID Key amountR AVGamount
2253545 1 10000 50
2253545 5 70000 35
2253545 7 8000 70
2253545

HOW xml looks like : Contract key = 1 and Contract key = 9

XML link

Desired results from this example:

ID Contract key OutstandingAmountR AverageMonthlyInstalmentAmount
2253545 1 12320.84 224.15
2253545 9 3914.13 74.96

Advertisement

Answer

There are a couple of errors in your xml sample data, first one there’s no opening tag for </othercontracts> and this tag has a bad formed namespace definition <BatchResponse xmlns="http://katana"> should be like <BatchResponse xmlns:ns3="http://katana"> (namespace=something) and that prevents document navigation.

Solving that https://codebeautify.org/xmlviewer/y22fbb8e2 you should treat xml data as such, not using text functions but xml fuctions, so using xmltable function is very simple to query your data, here an example

Having

Query should look like

Using xpaths to reach desired data.

And result

enter image description here

in documentation https://www.postgresql.org/docs/current/functions-xml.html

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement