performing calculations with values in xml CLOB which has identical tags using sql - 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.

Related

Extracting Text Values from XML in SQL

I'm working with SQL data hosted by a 3rd party, and am trying to pull some specific information for reporting. However, some of what I need to parse out is in XML format, and I'm getting stuck. I'm looking for the syntax to pull the text= values only from the XML code.
I believe the code should something like this, but most of the examples I can find online involve simpler XML hierarchy's than what I'm dealing with.
<[columnnameXML].value('(/RelatedValueListBO/Items/RelatedValueListBOItem/text())[1]','varchar(max)')>
Fails to pull any results. I've tried declaring the spacenames as well, but again...I only ever end up with NULL values pulled.
Example XML I'm dealing with:
<RelatedValueListBO xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://tempuri.org/RelatedValueListBOSchema.xsd">
<Items>
<RelatedValueListBOItem groupKey="Response1" text="Response1" selected="true" />
<RelatedValueListBOItem groupKey="Response2" text="Response2" selected="true" />
<RelatedValueListBOItem groupKey="Response3" text="Response3" selected="true" />
</Items>
</RelatedValueListBO>
Ideally I'd like to pull response1; response2; response3 into a single column. Allowing for the fact that multiple responses may exist. I believe I'm getting stuck with the basic code I've been trying due to the namespaces associated to RelatedValueListBO and the fact that what I want is grouped in groupKey, text, and selected, instead of the value I want just being under the Items node.
You have the namespaces defined in your XML, so you need to define them in the XQuery too.
Fast and dirty method is to replace all namespaces with a "*":
SELECT #x.value('(/*:RelatedValueListBO/*:Items/*:RelatedValueListBOItem/#text)[1]','varchar(max)')
To get all responses in a single column you can use:
SELECT
Item.Col.value('./#text','varchar(max)') X
FROM #x.nodes('/*:RelatedValueListBO/*:Items/*:RelatedValueListBOItem') AS Item(Col)
If you need a better performance, you may need to define namespaces properly.
You can use something like this to extract the value of "text" in the first node of RelatedValueListBOItem
SELECT extractvalue(value(rs), '//RelatedValueListBOItem[1]/#text')
FROM TABLE (xmlsequence(extract(sys.xmltype('<RelatedValueListBO>
<Items>
<RelatedValueListBOItem groupKey="Response1" text="Response1"
selected="true" />
<RelatedValueListBOItem groupKey="Response2" text="Response2"
selected="true" />
<RelatedValueListBOItem groupKey="Response3" text="Response3"
selected="true" />
</Items>
</RelatedValueListBO>'),'/RelatedValueListBO/Items'))) rs;

Storing XML into Postgres

I have an XML document that needs to get stored in an SQL db (Postgres).
I've already seen how that's done, but I have a question: do I just create a single table with a xml field and place the whole document there? This is a document about movies and so (movies, actors...) that has information to be later retrieved.
I've never worked with XML in databases, so I'm a little confused.
Here's an example of my XML:
<?xml version="1.0" encoding="UTF-8"?>
<cinema xmlns="movies"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="movies file:/C:/Users/Fabio/git/LAPD/movies.xsd">
<persons>
<person id="P1">
<name>Samuel L. Jackson</name>
<birth>1948-12-21</birth>
</person>
<person id="P2">
<name>Leonardo Di Caprio</name>
<birth>1974-11-11</birth>
</person>
<person id="P3">
<name>Quentin Tarantino</name>
<birth>1963-03-27</birth>
</person>
</persons>
<movies>
<movie id="M1">
<title>Pulp Fiction</title>
<length>154</length>
<year>1994</year>
<description>The lives of two mob hit men,
a boxer, a gangster's wife, and a pair
of diner bandits intertwine in four tales of violence and redemption</description>
<crew>
<director ref="P3"/>
<writer ref="P3"/>
</crew>
<cast>
<actor ref="P1"/>
</cast>
<rate>
<imdb>8.9</imdb>
<rottentomatoes>9</rottentomatoes>
<moviedb>7.8</moviedb>
<average>8.57</average>
</rate>
<numOscars>1</numOscars>
</movie>
<movie id="M2">
<title>Django Unchained</title>
<length>165</length>
<year>2012</year>
<description>With the help of a German bounty hunter,
a freed slave sets out to rescue his wife
from a brutal Mississippi plantation owner.</description>
<crew>
<director ref="P3"/>
<writer ref="P3"/>
</crew>
<cast>
<actor ref="P1"/>
<actor ref="P2"/>
</cast>
<rate>
<imdb>8.5</imdb>
<rottentomatoes>8</rottentomatoes>
<moviedb>7.4</moviedb>
<average>7.97</average>
</rate>
<numOscars>2</numOscars>
</movie>
</movies>
You can store a whole XML document as value in a single xml column or you can extract data and store it in a more or less normalized form.
Which is better, depends on all the details of your application that are unknown to us.
Here is a related answer discussing pros and cons of storing document types vs. db normalization:
Does JSONB make PostgreSQL arrays useless?
Save XML as a text column of DB so that you can also apply equality operator easily. You may find some error on insertion for " or ' so try to replace them with other characters like ~ or `, both.

Xml node name Parsing using 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

Parsing using NSXMLParser

I am being returned some XML from a web service. Basically, the xml looks like this:
<response>
<data>
More XML here but the it's escaped by XML entities
</data>
</response>
so, as you can see, I have xml that is valid, but the stuff inside data tag is escaped with XML entities. what's the best (most efficient) way for me to feed this into the parser?
What I am doing right now is, when I get the data from web service, I convert it into NSString....then replace the "XML escaped entities" with real ones.....then convert it back into NSData...then feed it into the parser. This doesn't seem like a very good solution so I was wondering if there's a better way to do it?
Thanks.
Alright, here's the xml that I am getting:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Header><ActivityId CorrelationId="d39007b5-ee69-41c7-a61d-831b456f9ea3" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">aa88d1cd-253c-48d1-abeb-62a880bea806</ActivityId></s:Header><s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><LoginResponse xmlns="http://MSS"><LoginResult><LoginInformation>
<User>
<UserID>612</UserID>
<UserName>Demo User</UserName>
<Email>mssdev#mss-mail.com</Email>
<CompanyID>17034</CompanyID>
<CompanyName>PlanET Demonstration Agency</CompanyName>
</User>
</LoginInformation></LoginResult></LoginResponse></s:Body></s:Envelope>
As you can see, everything in is escaped.
That's kind of horrifying. Why don't you just take the contents of that tag (e.g. the data with the entities) and just pass that through another NSXMLParser? The first parser will have decoded the entities, and the second parser will be presented with the decoded version of the contents of that tag.

Import Xml nodes as Xml column with SSIS

I'm trying to use the Xml Source to shred an XML source file however I do not want the entire document shredded into tables. Rather I want to import the xml Nodes into rows of Xml.
a simplified example would be to import the document below into a table called "people" with a column called "person" of type "xml". When looking at the XmlSource --- it seem that it suited to shredding the source xml, into multiple records --- not quite what I'm looking for.
Any suggestions?
<people>
<person>
<name>
<first>Fred</first>
<last>Flintstone</last>
</name>
<address>
<line1>123 Bedrock Way</line>
<city>Drumheller</city>
</address>
</person>
<person>
<!-- more of the same -->
</person>
</people>
I didn't think that SSIS 2005 supported the XML datatype at all. I suppose it "supports" it as DT_NTEXT.
In any case, you can't use the XML Source for this purpose. You would have to write your own. That's not actually as hard as it sounds. Base it on the examples in Books Online. The processing would consist of moving to the first child node, then calling XmlReader.ReadSubTree to return a new XmlReader over just the next <person/> element. Then use your favorite XML API to read the entire <person/>, convert the resulting XML to a string, and pass it along down the pipeline. Repeat for all <person/> nodes.
Could you perhaps change your xml output so that the content of person is seen as a string? Use escape chars for the <>.
You could use a script task to parse it as well, I'd imagine.