Read XML attributes in postgres - sql

I am using XML first timer in Postgres and facing an issue.
I have below xml in a variable named XMLCONTENT
<?xml version="1.0" encoding="UTF-8"?>
<Actions>
<Action ActionID="90e0dbef-c23a-4fcd-bfa8-75d8bfa2c9e2" />
<Action ActionID="6a1998e1-70f1-4611-992a-7a27e2834c35" />
<Action ActionID="43dd9a91-c6d3-4980-b211-9b3780f04305" />
<Action ActionID="cdf01821-ac28-45a9-abf8-a7d7c9426518" />
<Action ActionID="e86fac8a-84e3-41ba-8bee-c7ffd1ac8ee5" />
<Action ActionID="a68dd878-ba1e-4fd9-b436-cdc15eccffb6" />
<Action ActionID="cd863a5a-83e9-489e-b24d-ff6638c5b190" />
<Action ActionID="720ba9c7-b797-4b2e-913e-11ac3ecd7b7f" />
<Action ActionID="b6b35d0d-938e-45d3-96d1-0c8ca3ad59f3" MessageID="42f40c3a-4426-4506-86c5-222fb03c2114" />
</Actions>
I want to extract details from this XML and I am using below query
Select
Unnest(xpath('//#ActionID',XMLCONTENT)) as ID,
Unnest(xpath('//#MessageID',XMLCONTENT)) as MessageID,
Unnest(xpath('//#Operator',XMLCONTENT)) as Operator
but i am getting wrong output as shown below
MessageID is linked with the wrong actionID. What is the correct way to traverse this XML?

The reason your query is not working is the use of unnest() in the select list: each unnest() call adds a new row to the result.
You need to use unnest in the from clause to create one row for each <Action> element:
with data (xmlcontent) as (
values ('
<Actions>
<Action ActionID="90e0dbef-c23a-4fcd-bfa8-75d8bfa2c9e2" />
<Action ActionID="6a1998e1-70f1-4611-992a-7a27e2834c35" />
<Action ActionID="43dd9a91-c6d3-4980-b211-9b3780f04305" />
<Action ActionID="cdf01821-ac28-45a9-abf8-a7d7c9426518" />
<Action ActionID="e86fac8a-84e3-41ba-8bee-c7ffd1ac8ee5" />
<Action ActionID="a68dd878-ba1e-4fd9-b436-cdc15eccffb6" />
<Action ActionID="cd863a5a-83e9-489e-b24d-ff6638c5b190" />
<Action ActionID="720ba9c7-b797-4b2e-913e-11ac3ecd7b7f" />
<Action ActionID="b6b35d0d-938e-45d3-96d1-0c8ca3ad59f3"
MessageID="42f40c3a-4426-4506-86c5-222fb03c2114" />
</Actions>'::xml)
)
select (xpath('//#ActionID', xt.action))[1] as id,
(xpath('//#MessageID', xt.action))[1] as message_id
from data
cross join unnest(xpath('/Actions/Action', xmlcontent)) as xt(action);
Returns:
id | message_id
-------------------------------------+-------------------------------------
90e0dbef-c23a-4fcd-bfa8-75d8bfa2c9e2 |
6a1998e1-70f1-4611-992a-7a27e2834c35 |
43dd9a91-c6d3-4980-b211-9b3780f04305 |
cdf01821-ac28-45a9-abf8-a7d7c9426518 |
e86fac8a-84e3-41ba-8bee-c7ffd1ac8ee5 |
a68dd878-ba1e-4fd9-b436-cdc15eccffb6 |
cd863a5a-83e9-489e-b24d-ff6638c5b190 |
720ba9c7-b797-4b2e-913e-11ac3ecd7b7f |
b6b35d0d-938e-45d3-96d1-0c8ca3ad59f3 | 42f40c3a-4426-4506-86c5-222fb03c2114
In the select list, you know that each '//#ActionID' only returns a single element, so there is no need to use unnest at that level any more.
Online example: https://rextester.com/MWBCEN37238
If you were using Postgres 10 or later, this would be a bit simpler with XMLTABLE:
select xt.*
from data
cross join xmltable ('/Actions/Action'
passing xmlcontent
columns id uuid path '#ActionID',
message_id uuid path '#MessageID'
) as xt;
Online example: https://dbfiddle.uk/?rdbms=postgres_10&fiddle=1e70be54c25a42db5ebff9a996423920

Related

SQL string value

Have a large XML file stored in a field within a table, many values have already been extracted and stored in the table, but I'm looking to capture (2) additional: account type = "current" status="X" and account type = "former" status ="Y". In the partial output below there is no former account type so I need a strategy for missing as well.
<ncf_report xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://cp.com/rules/client">
<admin>
<product_reference>12345678901234</product_reference>
<report_type>XXXXXXX</report_type>
<status>XXXXXXXX</status>
<ownership>XXXXXXX</ownership>
<report_code>1234</report_code>
<report_description>XXXXXXXXXXXXXXXXX</report_description>
<purpose>XXXXXXXX</purpose>
<date_request_ordered>mm/dd/yyyy</date_request_ordered>
<date_request_received>mm/dd/yyyy</date_request_received>
<date_request_completed>mm/dd/yyyy</date_request_completed>
<time_report_processed>01234</time_report_processed>
<multiple_scores_ordered>false</multiple_scores_ordered>
<vendor name="XXXXXXXXXXXXX" address="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" />
<report>
<sequence>0000000000</sequence>
<count>0000000000</count>
</report>
</admin>
<report>
<alerts_scoring>
<scoring>
<score status="XXXXXXXXXX">
<model_label>XXXXXXXXXXXXXXXXX</model_label>
<score>123</score>
<rating_state>XX</rating_state>
<classification>XXXXXXXXXXXXXXXXX</classification>
<reason_codes>
<code>05</code>
<description>XXXXXXXXXXXXXXXXX</description>
</reason_codes>
<reason_codes>
<code>04</code>
<description>XXXXXXXXXXXXXXXXX</description>
</reason_codes>
<reason_codes>
<code>10</code>
<description>XXXXXXXXXXXXXXXXX</description>
</reason_codes>
<reason_codes>
<code>27</code>
<description>XXXXXXXXXXXXXXXXX</description>
</reason_codes>
</score>
</scoring>
<general>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</general>
<general>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</general>
<general>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</general>
</alerts_scoring>
<vendor_dataset>
<subjects>
<subject type="Primary" relationship_to_data="Subject">
<name type="Report Subject">
<first>XXXX</first>
<middle>X</middle>
<last>XXXX</last>
</name>
<name type="Alias">
<first>XXXXXXXXXX</first>
<last>XXXXXXXX</last>
</name>
<birth_date>mm/dd/yyyy</birth_date>
<ssn>999999999</ssn>
<address type="residence" ref="1" />
<address type="former" ref="2" />
<address type="former" ref="3" />
</subject>
</subjects>
<addresses>
<address id="1">
<house>1234</house>
<street1>sample</street1>
<city>sample</city>
<state>XX</state>
<postalcode>12345</postalcode>
<zip4>1234</zip4>
<date_first_at_address>mm/dd/yyyy</date_first_at_address>
<date_last_at_address>mm/dd/yyyy</date_last_at_address>
</address>
<address id="X">
<house>1234</house>
<street1>XXXXXXXXX</street1>
<city>XXXXXXXXX</city>
<state>XX</state>
<postalcode>12345</postalcode>
<zip4>1234</zip4>
<date_first_at_address>mm/dd/yyyy</date_first_at_address>
<date_last_at_address>mm/dd/yyyy</date_last_at_address>
</address>
</addresses>
</vendor_dataset>
<summary>
<date_oldest_trade>mm/dd/yyyy</date_oldest_trade>
<date_latest_trade>mm/dd/yyyy</date_latest_trade>
<date_latest_activity>mm/dd/yyyy</date_latest_activity>
<includes_bankruptcies flag="false" />
<includes_other_records public_records="false" collection="false" consumer_statement="false" />
<credit_range high="12345" low="123" number_trade_lines="123" />
<account_status_counters>
<!-- here --> <account type="current" description="Pays Account as Agreed" status="1">12</account>
</account_status_counters>
<account_summaries>
<account type="Open-ended">
<number_accounts>0</number_accounts>
<total_owed>0</total_owed>
<total_past_due>0</total_past_due>
<high_amount>0</high_amount>
</account>
<account type="Revolving">
<number_accounts>00</number_accounts>
<total_owed>1234</total_owed>
<total_past_due>0</total_past_due>
<high_amount>12345</high_amount>
</account>
<account type="Installment">
<number_accounts>00</number_accounts>
<total_owed>12345</total_owed>
<total_past_due>0</total_past_due>
<high_amount>123456</high_amount>
</account>
</account_summaries>
<inquiry_history count="0" />
</summary>
<employment_history>
<employment_primary_subject>
<job entry="current" indirectly_verified="false">
<employer>
<name>XXXXXXXXXXX</name>
</employer>
</job>
<job entry="first_former" indirectly_verified="false">
<employer>
<name>XXXXXXXX</name>
<city>XXXXXXX</city>
<state>XX</state>
</employer>
</job>
</employment_primary_subject>
</employment_history>
<trade_account_activity>
<credit_trades>
<credit_trade automated_tape_supplier="false">
<reporting_member>
<number>1234X1234</number>
<name>XXX/1234</name>
</reporting_member>
<account>
<type>XXXXXXXXX</type>
<terms>XXX</terms>
<months_reviewed>00</months_reviewed>
<designator>XXXXXXXX(XXXXX)</designator>
</account>
<date_reported>mm/dd/yyyy</date_reported>
<date_opened>mm/dd/yyyy</date_opened>
<date_last_activity>mm/dd/yyyy</date_last_activity>
<current_rate>XXXXXXXXXXXXXXXXX</current_rate>
<highest_amount>1234</highest_amount>
<balance_amount>00</balance_amount>
<past_due_amount>00</past_due_amount>
<messages>
<message code="XX">XXXXXXXXXXXXXX</message>
<message code="XX">XXXXXXXXXXXXXX</message>
</messages>
</credit_trade>
</account>
<date_reported>mm/dd/yyyy</date_reported>
<date_opened>mm/dd/yyyy</date_opened>
<date_last_activity>mm/dd/yyyy</date_last_activity>
<current_rate>XXXXXXXXXXXXXXXXXXXXXX</current_rate>
<highest_amount>123456</highest_amount>
<balance_amount>123456</balance_amount>
<past_due_amount>0</past_due_amount>
<messages>
<message code="XX">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</message>
</messages>
</credit_trade>
</credit_trades>
</trade_account_activity>
<inquiry_history>
<inquiry date="mm/dd/yyyy" name="XXXXXXXXXXXXXXXXXXXXXXX" member="12345X1234" />
<inquiry date="mm/dd/yyyy" name="XXXXXXXXXXXXXXXXXXXXXXX" member="12345Y1234" />
<inquiry date="mm/dd/yyyy" name="CXXXXXXXXXXXXXXXXXXXXXXX" member="12345Z1234" />
<inquiry date="mm/dd/yyyy" name="XXXXXXXXXXXXXXXXXXXXXXX & X" member="12345W1234" />
<inquiry date="mm/dd/yyyy" name="XXXXXXXXXXXXXXXXXXXXXXX" member="12345V1234" />
<inquiry date="mm/dd/yyyy" name="XXXXXXXXXXXXXXXXXXXXXXX" member="12345U1234" />
<inquiry date="mm/dd/yyyy" name="XXXXXXXXXXXXXXXXXXXXXXX" member="12345T1234" />
</inquiry_history>
</report>
</ncf_report>
I'm looking to extract the X value from account type = "current" status="X" and and Y value if an account type = "former" exists. In this case the value 1. added to XML to highlight area of interest. I started by pairing down the data set into a temp table.
select id,
LEFT(SUBSTRING(CreditscoreXML,charindex('<account type="current"',CreditscoreXML),charindex('</account>',CreditscoreXML)),charindex('">',SUBSTRING(CreditscoreXML,charindex('<account type="current"',CreditscoreXML),charindex('</account>',CreditscoreXML)))) [Current_Status]
select
Current_Status, --just so I see output is correct in temp table
substring(Current_Status, charindex('status="',
Current_Status)+8,len(Current_Status)-charindex('status',Current_Status)) [Current_Status]
from #TempCurrent
From here I further tried to refine the text search. Trying to figure out how to eliminate the " after 1 or a better solution to extract both current and former status, former can be missing, need this grouped by Id.
Current Output
Current_Worse_Score Current_Worse_Score Former_Worse
Original Text 1"
Rather than manipulating string data try using the built-in XML functions in SQL Server to make your life easier. For example:
create table dbo.Foo (
id int not null,
bar xml not null
);
insert dbo.Foo (id, bar) values (47, N'<ncf_report xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://cp.com/rules/client">
<!-- the rest of your xml... -->
</ncf_report>')
;with xmlnamespaces(default 'http://cp.com/rules/client')
select
id,
x.a.value(N'#description', N'nvarchar(50)') as [Description],
x.a.value(N'#status', N'nvarchar(50)') as [Status],
x.a.value(N'.', N'nvarchar(50)') as [Account]
from dbo.Foo
cross apply bar.nodes(N'/ncf_report/report/summary/account_status_counters/account[#type="current"]') x(a)
Which yields the result...
id Description Status Account
47 Pays Account as Agreed 1 12
You can use the built-in XML data type methods to query required values from an XML instance which is stored as XMLType column.
DECLARE #X XML;
SET #X = '<ncf_report xmlns:xsd="http://www.w3.org/2001/XMLSchema" ....>'; -- provided XML instance
CREATE TABLE NCFREPORT
(
ncfreportcol XML NOT NULL
);
INSERT INTO ncfreport (ncfreportcol) values (#X); -- inserting the XML instance stored temporarily in above variable X
WITH xmlnamespaces ('http://cp.com/rules/client' as NR)
SELECT T.acc.value('(#status)[1]', 'int') AS Status,
T.acc.value('(#type)[1]', 'varchar(20)') AS AccType,
T.acc.value('(text())[1]', 'int') AS Acc
FROM ncfreport cross apply ncfreport.ncfreportcol.nodes ('/NR:ncf_report/NR:report/NR:summary/NR:account_status_counters/NR:account') as t(acc);
This will result in the following output:
Status AccType Acc
1 current 12
It will produce one row in the output for each account if you have multiple account tags defined in the XML instance. I also noticed that there are missing opening or closing tags in the above XML fragment. It would be a good idea to also have a look at validating the XML before entering into the table. Please have a look at various XML data type methods here - https://learn.microsoft.com/en-us/sql/t-sql/xml/xml-data-type-methods?view=sql-server-ver15

Sql server xpath conditionnal query

I have a table where one field called 'configuration' is type of XML:
<configuration>
<element value="john" />
<element value="kevin" />
<element value="lisa" />
<element value="david" />
<element value="mike" />
</configuration>
What I would like to do, is to retrieve all the table records or at least count the table records that have a field 'configuration' containing at least one 'element' attribute containing a 'value' attribute equals to 'lisa'.
What I have for the moment, is a query that can retrieve the 'value' attribute of the specified 'element' position, for example:
select Configuration.value('(/configuration/element/#value)[0]', 'nvarchar(max)') // returns me 'john'
select Configuration.value('(/configuration/element/#value)[1]', 'nvarchar(max)') // returns me 'kevin'
This is a bit of pseudo-SQL in the absence of a dataset, and a bit of guesswork; in that I (think) you simply want to return the rows(?) where there is a the node configuration/element has the value property 'lisa'. IF my guess is right, then something like this will work (you'll ened to replace object names in Braces({}):
SELECT {Columns}
FROM [{Your Table}] YT
WHERE EXISTS (SELECT 1
FROM [{Your Table}] E
CROSS APPLY E.[{Your XML Column}].nodes('configuration/element') C(E)
WHERE E.[{Your ID Column}] = YT.[{Your ID Column}]
AND C.E.value('./#value','varchar(50)') = 'lisa');
Example:
WITH VTE AS(
SELECT 1 AS ID,
CONVERT(xml,'<configuration>
<element value="john" />
<element value="kevin" />
<element value="lisa" />
<element value="david" />
<element value="mike" />
</configuration>') AS XMlCol
UNION ALL
SELECT 2 AS ID,
CONVERT(xml,'<configuration>
<element value="craig" />
<element value="donald" />
<element value="jenny" />
<element value="jayne" />
</configuration>') AS XMlCol)
SELECT *
FROM VTE YT
WHERE EXISTS (SELECT 1
FROM VTE E
CROSS APPLY E.XMlCol.nodes('configuration/element') C(E)
WHERE E.ID = YT.ID
AND C.E.value('./#value','varchar(50)') = 'lisa');
This only returns the row with an ID of 1.
Please try the below,
declare #xml as xml
set #xml='<configuration>
<element value="john" />
<element value="kevin" />
<element value="lisa" />
<element value="david" />
<element value="mike" />
</configuration>'
SELECT T.c.value('./#value','nvarchar(250)' ) As element
FROM #xml.nodes('//element') AS T(c)

Stripping data from xml in SQL Server

One of my tables with xml datatype has the following xml information:
<RequestMetaData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MetaData Type="DocImport">
<Keywords>
<Key Name="Zone" Value="MIO" />
<Key Name="ClassificationStrategy" Value="NeedClassification" />
<Key Name="Folder" Value="0456e6ca" />
</Keywords>
</MetaData>
<MetaData Type="SourceResponse">
<Keywords>
<Key Name="NotificationResponse_20180427-150426" Value="Received successful response from Source" />
</Keywords>
</MetaData>
</RequestMetaData>
I need to write an SQL query to fetch the value of Classification strategy based on key name.
I have added the xml in a variable #xml and used the following code. It is returning NULL.
select A.b.value('ClassificationStrategy[1]', 'VARCHAR(30)') AS CS
FROM #xml.nodes('/RequestMetaData/MetaData/Keywords') AS A(b)
Can someone please help me with this.
You can read your XML in various ways. Use a simple .value() with an XPath/XQuery expression to retrieve a single value, use .query to retrieve a part of the XML or use .nodes() to return repeated elements as derived table:
DECLARE #xml XML=
N'<RequestMetaData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MetaData Type="DocImport">
<Keywords>
<Key Name="Zone" Value="MIO" />
<Key Name="ClassificationStrategy" Value="NeedClassification" />
<Key Name="Folder" Value="0456e6ca" />
</Keywords>
</MetaData>
<MetaData Type="SourceResponse">
<Keywords>
<Key Name="NotificationResponse_20180427-150426" Value="Received successful response from Source" />
</Keywords>
</MetaData>
</RequestMetaData>';
--Read the whole lot
SELECT md.value('#Type','nvarchar(max)') AS MetaDataType
,k.value('#Name','nvarchar(max)') AS KeyName
,k.value('#Value','nvarchar(max)') AS KeyValue
FROM #xml.nodes('/RequestMetaData/MetaData') A(md)
OUTER APPLY md.nodes('Keywords/Key') B(k);
--Get one key's value by name (anywhere in the doc)
DECLARE #keyName VARCHAR(100)='ClassificationStrategy';
SELECT #xml.value('(//Key[#Name=sql:variable("#keyName")]/#Value)[1]','nvarchar(max)');
--Use the meta data type as additional filter (if key names are not unique per doc)
DECLARE #kName VARCHAR(100)='ClassificationStrategy';
DECLARE #mdType VARCHAR(100)='DocImport';
SELECT #xml.value('(/RequestMetaData
/MetaData[#Type=sql:variable("#mdType")]
/Keywords
/Key[#Name=sql:variable("#kName")]
/#Value)[1]','nvarchar(max)');

Representation of xmlcast result with delimeters in Pl/SQL

I am a bit new to PL/SQL, I have a following problem:
I have xml with following structure(b.response):
<a>
<b>
...
<ruleList>
<number>2</number>
<rule>
<name>test1</name>
</rule>
<rule>
<name>test2</name>
</rule>
</ruleList>
</b>
</a>
I use the following script to parse needed values from XML:
SELECT
xmlcast(xmlquery('/a/b/ruleList/number' passing b.response returning content)as varchar2(1000)) rules_number,
xmlcast(xmlquery('/a/b/ruleList/rule[*]/name'passing b.response returning content)as varchar2(1000)) values
FROM test b
And have following result:
rules_number | values
2 | test1test2
My question is how can I enter delimeters between names using xmlcast? So the result should be following:
rules_number | values
2 | test1, test2
SELECT
xmlcast(xmlquery('string-join(/a/b/ruleList/number/text(),",")' passing b.response returning content)as varchar2(1000)) rules_number,
xmlcast(xmlquery('string-join(/a/b/ruleList/rule[*]/name/text(),",")' passing b.response returning content)as varchar2(1000)) values
FROM test b
/text() is extracting value of node not full node
string-join - join string sequence using delimiter
Better choice in your case is xmltable.
select * from xmltable( '/a/b/ruleList' passing xmltype( '<a>
<b>
<ruleList>
<number>2</number>
<rule>
<name>test1</name>
</rule>
<rule>
<name>test2</name>
</rule>
</ruleList>
</b>
</a>')
columns
rules_number varchar2(1000) path 'string-join(number/text(),",")',
"values" varchar2(1000) path 'string-join(rule/name/text(),",")'
);

SQL FOR XML multilevel from one pivoted table

I've been trying to use FOR XML without success to do the following.
Source table:
Country | ID | 1950 | 1955
-----------------------------------------------------
Country 1 | 1 | 2.43 | 2.55
Country 2 | 2 | 4.54 | 42.15
Desired output:
<locations>
<location>
<loc name='Country 1' id='1' />
<dub>
<data year='1950' value='2.43' />
<data year='1955' value='2.55' />
</dub>
</location>
<location>
<loc name='Country 2' id='2' />
<dub>
<data year='1950' value='4.54' />
<data year='1955' value='42.15' />
</dub>
</location>
</locations>
Will it be necessary to unpivot for the dub element? I wanted the simplest SQL query possible.
I think FOR XML is too difficult to use. You should be able to specify the hierarchy just using simple XPath on column names but it won't accept, for example, [dub/data/#year=1955/#value] as the name of the column [1950].
SQL Fiddle
MS SQL Server 2012 Schema Setup:
create table YourTable
(
Country varchar(20),
ID int,
[1950] numeric(5,2),
[1955] numeric(5,2)
)
insert into YourTable values
('Country 1', 1, 2.43, 2.55),
('Country 2', 2, 4.54, 42.15)
Query 1:
select T.Country as 'loc/#name',
T.ID as 'loc/#id',
(
select 1950 as 'data/#year',
T.[1950] as 'data/#value',
null,
1955 as 'data/#year',
T.[1955] as 'data/#value'
for xml path(''), type
) as dub
from YourTable as T
for xml path('location'), root('locations'), type
Results:
<locations>
<location>
<loc name="Country 1" id="1" />
<dub>
<data year="1950" value="2.43" />
<data year="1955" value="2.55" />
</dub>
</location>
<location>
<loc name="Country 2" id="2" />
<dub>
<data year="1950" value="4.54" />
<data year="1955" value="42.15" />
</dub>
</location>
</locations>