Xml node name Parsing using SQL - sql

I need to get the value of the tag "supervisor....." The problem is that those tag name are dynamic, they change for each person. I have tried using Extract value but it works only if you use a static tag name.
It is in Oracle environment.
Thanks for your help.
Ourson
XML Extract
<?xml version="1.0" encoding="UTF-8"?>
<TRANSACTION>
<TransCtx>
<supervisorCalcAttributes classType="Ht">
<SupervisorId10977 classType="s">Matt, Clinton</SupervisorId10977>
<SupervisorId4753 classType="s">Bob, Sponge</SupervisorId4753>
<FND_ENTERPRISE_ID classType="s">1</FND_ENTERPRISE_ID>
</supervisorCalcAttributes>
</TransCtx>
</TRANSACTION>
SQL Query
select
Extractvalue( xmltype('<root>'||txndata.data_cache||'</root>'),
'root/TRANSACTION[1]/TransCtx[1]/supervisorCalcAttributes[1]/**LineItem**[1]' ) as test
from hrc_txn_data txndata, hrc_txn_header txnhe
where txnhe.transaction_id=txndata.transaction_id

Related

Generate XML - Exclude nodes

Please see the SQL below:
select dbms_xmlgen.getxml('select incident_id FROM INCIDENTS where incident_id=600') xml
from dual
It returns:
<?xml version="1.0"?>
<ROWSET>
<ROW>
<INCIDENT_ID>600</INCIDENT_ID>
</ROW>
</ROWSET>
How can I exclude return this:
<SYSTEM URN="114644">
<INCIDENT_ID>600</INCIDENT_ID>
<SYSTEM>
I want to hardcode the first row and the third row of the XML.
Use extract function to get your desired output
EXTRACT (XML) is similar to the EXISTSNODE function.
It applies a VARCHAR2 XPath string and returns an XMLType instance containing an XML fragment. You can specify an absolute XPath_string with an initial slash or a relative XPath_string by omitting the initial slash.
If you omit the initial slash, the context of the relative path defaults to the root node...
also see similar question here
https://community.oracle.com/thread/1126429?tstart=0

XML parsing error in SQL Server stored procedure

I want to parse the below XML in a SQL Server stored procedure and update some tables based on this XML. I have implemented the same using OPENXML but now there is one more line added to the beginning of the XML, because of which am getting unexpected errors. Is it possible to somehow skip the first tag alone while parsing
Parsing code :
set #Lead= (select lead
from openxml(#DOCHANDLE,'/DBO.TBLLEADS',2) with (lead INT 'LEAD'))`
XML here:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LEADS>
<LEAD>6680299</LEAD>
<JOBNO>50919</JOBNO>
<BEGINDATE>4-04-2013</BEGINDATE>
<ENDDATE>04/14/2013</ENDDATE>
</LEADS>
Well, not the most elegant solution, but will get it back to working:
Before you prepare your XML document, run this statement on the variable containing the XML:
SET #XMLVariable = REPLACE(#XMLVariable, '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>', '')
Basically you're leveraging the REPLACE function to replace the unnecessary header string with nothing.

How to use <= inside an XML element?

I have a config.xml file that contains a SQL query. The query will end up getting read and performed in c#. However, I need to know how to write the query contained inside the XML element. The problem is that the query uses <= in a WHERE statement. The '<' part of the query causes the XML to think it is supposed to esacpe the element, I think. How can i include this basic select statement in the XML file? I noticed > works fine. Obviously, I could swap the order of the comparison, but I want to know how I can specifically include < please.
<?xml version="1.0" encoding="utf-8" ?>
<root>
<Connections>
<Connection>
<Source>Data Source=XXX-XXX; Initial Catalog=MyData;Integrated Security=True</Source>
<Query>Select * FROM Info WHERE EffectiveDate <= GETDATE() </Query>
</Connection>
</Connections>
</root>
Use a CDATA block. Then you wouldn't have to encode your query.
For example:
<Query><![CDATA[
Select * FROM Info WHERE EffectiveDate <= GETDATE()
]]></Query>
You could encode the < character in the config file < and decode it for use.

performing calculations with values in xml CLOB which has identical tags using sql

I've got a table (event_archive) and one of the columns(event_xml) has CLOB data in xml format as below. Is there a way to use SQL to summate the values of the "xx" tag? Please help as i'm completely baffled. Even simply extracting the values is a problem as there are 2 "xx" tags within the same root. Thanks in advance.
<?xml version="1.0" encoding="UTF-8"?>
<event type="CALCULATION">
<source_id>INTERNAL</source_id>
<source_participant/>
<source_role/>
<source_start_pos>1</source_start_pos>
<destination_participant/>
<destination_role/>
<event_id>123456</event_id>
<payload>
<cash_point reference="abc12345">
<adv_start>20120907</adv_start>
<adv_end>20120909</adv_end>
<conf>1234</conf>
<profile>3</profile>
<group>A</group>
<patterns>
<pattern id="00112">
<xx>143554.1</xx>
<yyy>96281.6</yyy>
<adv>875</adv>
</pattern>
<pattern id="00120">
<xx>227606.1</xx>
<yyy>97539.8</yyy>
<adv>18181</adv>
</pattern>
</patterns>
</cash_point>
</payload>
</event>
Different databases handle XML differently. There's no standard way of dealing with XML payloads via raw, standard SQL. So you'll need to look at your actual DB implementation to find out what support they have.

Extracting XML (as xml type) using xpath query in SQL

I'm trying to extract a chunk of XML (ie the whole xml of a node, not just content) using an xpath query in SQL. I can extract a single content field but am not sure how to do the above.
Say the xml is as follows
<head>
<instance>
<tag1>
<tag2>data</tag2>
<tag3>data</tag3>
</tag1>
</instance>
</head>
I would like to extract all of the xml inside tag1, and was hoping something like this would work in a SQL query:
Table.value('(/head/instance/tag1)[1]', 'varchar(max)') as "col"
Any help would be great.
Thanks
this should work:
Select Cast(Table.xmlcolumnname.query('/head/instance/tag1') as varchar(max)) 'col';
(its not checked! may contain typo..)