how to delete empty namespace from child element in sql server - sql

I am trying to delete empty namespace from the child element. I tried with following code but its not deleting
SET #xDocTemp.modify('declare default element namespace "mynamepsace";
delete /worksh/Data/row[#xmlns=""]')
xml data:
<worksh xmlns="mynamespace">
<Data>
<row r="1" ht="18">
<row xmlns="" rl="39" spans="2">
<row xmlns="" rl="39" spans="2">
</Data>
<worksh>
Expected output
<worksh xmlns="mynamespace">
<Data>
<row rl="1" ht="18">
<row rl="39" spans="2">
<row rl="39" spans="2">
</Data>
<worksh>

not sure if it's possible with modify(), but you can just replace it like
set #xDocTemp = select cast(replace(cast(#xDocTemp as nvarchar(max)), ' xmlns=""', '') as xml)

Related

Verifying data from xpath location having multiple data

I have XML like below where I need to verify XML with lot of data with same names (index appended)
like below.
I don't want to give separate column for each name,mobile etc. So in Example I am passing sum,men,1212121212,682312;suj,men,2212121212,682312**;**suu,men,3212121212,682312 OR I can pass in JSON format
1. What is the best way to do this without using Java Code.
2. *def strVals=get response/Response/transaction/values
And print strVals
- Not printing any value
<Response>
<RequestID>1</RequestID>
<transaction>
<values>
<data name="firstName0">sum</data>
<data name="lastName0">men</data>
<data name="mobile0">1212121212</data>
<data name="zip0">682312</data>
<data name="firstName1">suj</data>
<data name="lastName1">men</data>
<data name="mobile1">2212121212</data>
<data name="zip1">682312</data>
<data name="firstName2">suu</data>
<data name="lastName2">men</data>
<data name="mobile2">3212121212</data>
<data name="zip2">682312</data>
<data name="firstName3">ssss</data>
<data name="lastName3">mmmmmmm</data>
<data name="mobile3">4212121212</data>
<data name="zip3">682312</data>
</values>
</transaction>
</Response>
I'll provide a hint on how to dynamically build this XML, the rest is up to you:
* def temp = <values></values>
* def data = [{first: 'John', value: 'foo'}, {first: 'Smith', value: 'bar'}]
* def fun =
"""
function(x, i){
var path = '/values/data[' + (i + 1) + ']';
karate.set('temp', path + '/#name', 'firstName' + i);
karate.set('temp', path, x.value);
}
"""
* karate.forEach(data, fun)
* print temp
Which will result in:
<values>
<data name="firstName0">foo</data>
<data name="firstName1">bar</data>
</values>

Find element or attribute value anywhere in XML

I am trying to find the value of an element / attribute regardless of where it exists in the XML.
XML:
<?xml version="1.0" encoding="UTF-8"?>
<cXML payloadID="12345677-12345567" timestamp="2017-07-26T09:11:05">
<Header>
<From>
<Credential domain="1212">
<Identity>01235 </Identity>
<SharedSecret/>
</Credential>
</From>
<To>
<Credential domain="1212">
<Identity>01234</Identity>
</Credential>
</To>
<Sender>
<UserAgent/>
<Credential domain="8989">
<Identity>10678</Identity>
<SharedSecret>Testing123</SharedSecret>
</Credential>
</Sender>
</Header>
<Request deploymentMode="Prod">
<ConfirmationRequest>
<ConfirmationHeader noticeDate="2017-07-26T09:11:05" operation="update" type="detail">
<Total>
<Money>0.00</Money>
</Total>
<Shipping>
<Description>Delivery</Description>
</Shipping>
<Comments>WO# generated</Comments>
</ConfirmationHeader>
<OrderReference orderDate="2017-07-25T15:22:11" orderID="123456780000">
<DocumentReference payloadID="5678-4567"/>
</OrderReference>
<ConfirmationItem quantity="1" lineNumber="1">
<ConfirmationStatus quantity="1" type="detail">
<ItemIn quantity="1">
<ItemID>
<SupplierPartID>R954-89</SupplierPartID>
</ItemID>
<ItemDetail>
<UnitPrice>
<Money currency="USD">0.00</Money>
</UnitPrice>
<Description>Test Descritpion 1</Description>
<UnitOfMeasure>QT</UnitOfMeasure>
</ItemDetail>
</ItemIn>
</ConfirmationStatus>
</ConfirmationItem>
<ConfirmationItem quantity="1" lineNumber="2">
<ConfirmationStatus quantity="1" type="detail">
<ItemIn quantity="1">
<ItemID>
<SupplierPartID>Y954-89</SupplierPartID>
</ItemID>
<ItemDetail>
<UnitPrice>
<Money currency="USD">0.00</Money>
</UnitPrice>
<Description>Test Descritpion 2</Description>
<UnitOfMeasure>QT</UnitOfMeasure>
</ItemDetail>
</ItemIn>
</ConfirmationStatus>
</ConfirmationItem>
</ConfirmationRequest>
</Request>
</cXML>
I want to get the value of the payloadID on the DocumentReference element. This is what I have tried so far:
BEGIN
Declare #Xml xml
Set #Xml = ('..The XML From Above..' as xml)
END
--no value comes back
Select c.value('(/*/DocumentReference/#payloadID)[0]','nvarchar(max)') from #Xml.nodes('//cXML') x(c)
--no value comes back
Select c.value('#payloadID','nvarchar(max)') from #Xml.nodes('/cXML/*/DocumentReference') x(c)
--check if element exists and it does
Select #Xml.exist('//DocumentReference');
I tried this in an xPath editor: //DocumentReference/#payloadID
This does work, but I am not sure what the equivalent syntax is in SQL
Calling .nodes() (like suggested in comment) is an unecessary overhead...
Better try it like this:
SELECT #XML.value('(//DocumentReference/#payloadID)[1]','nvarchar(max)')
And be aware, that XPath starts counting at 1. Your example with [0] cannot work...
--no value comes back
Select c.value('(/*/DocumentReference/#payloadID)[0]','nvarchar(max)') from...

How to do multiple loops through XML in PL/SQL

My XML looks like this
<data>
<row>
<id>1</id>
<name>John</name>
<name>Jack</name>
</row>
<row>
<id>2</id>
<name>Scott</name>
<name>Chuck</name>
<name>Kim</name>
</row>
</data>
I would like output:
->1
-->John
-->Jack
->2
-->Scott
-->Chuck
-->Kim
My current code looks like this:
DECLARE
X XMLTYPE := XMLTYPE('<?xml version="1.0" ?>
<data>
<row>
<id>1</id>
<name>John</name>
<name>Jack</name>
</row>
<row>
<id>2</id>
<name>Scott</name>
<name>Chuck</name>
<name>Kim</name>
</row>
</data>');
BEGIN
FOR R IN (SELECT EXTRACTVALUE(VALUE(P), '/row/id/text()') AS NAME
FROM TABLE(XMLSEQUENCE(EXTRACT(X, '//data/row'))) P)
LOOP
DBMS_OUTPUT.PUT_LINE('-->' || R.NAME);
END LOOP;
END;
I would need one more loop inside a row to loop through name tag, but I don't know how to do it.
A little help would be appreciated.
I figured it by myself:
DECLARE
X XMLTYPE := XMLTYPE('<?xml version="1.0" ?>
<data>
<row>
<id>1</id>
<promet>
<name>John</name>
<name>Jack</name>
</promet>
</row>
<row>
<id>2</id>
<promet>
<name>Scott</name>
<name>Chuck</name>
<name>Kim</name>
</promet>
</row>
</data>');
BEGIN
FOR R IN (SELECT EXTRACTVALUE(VALUE(P), '/row/id/text()') AS ID,
EXTRACT(VALUE(P), '/row/promet') AS PROMET
FROM TABLE(XMLSEQUENCE(EXTRACT(X, '//data/row'))) P)
LOOP
DBMS_OUTPUT.PUT_LINE('-->' || R.ID);
FOR R1 IN (SELECT EXTRACTVALUE(VALUE(T1), '/name/text()') AS NAME
FROM TABLE(XMLSEQUENCE(EXTRACT(R.PROMET, 'promet/name'))) T1)
LOOP
DBMS_OUTPUT.PUT_LINE('-->' || R1.NAME);
END LOOP;
END LOOP;
END;

Been trying to write an xquery code that converts a flat xml to 5-level hierarchy xml file

Would like to convert the following flax xml file to 5-level hierarchy xml structure using xquery, so far the all the xquery code i have written did not work.
<data>
<row>
<Year>1999</Year>
<Quarter>8</Quarter>
<Month>5</Month>
<Week>10</Week>
<Flight>6/11/1995</Flight>
<Un>WN</Un>
<Air>193</Air>
</row>
<data>
Out result i would like:
<data>
<row>
<Year>
<value>1999</value>
<Quarter>
<value>8</value>
<Month>
<value>5</value>
<Week>10</Week>
<Flight>6/11/1995</Flight>
<Un>WN</Un>
<Air>193</Air>
</Month>
</Quarter>
</Year>
</row>
<data>
It's unclear what XQuery processor you're using, or the exact schema of the data you will need to process, but here is an example of how to transform the data, assuming each row contains a unique set of entries:
let $data :=
<data>
<row>
<Year>1999</Year>
<Quarter>8</Quarter>
<Month>5</Month>
<Week>10</Week>
<Flight>6/11/1995</Flight>
<Un>WN</Un>
<Air>193</Air>
</row>
</data>
for $row in $data/row
return
element row {
element Year {
element value { $row/Year/data() },
element Quarter {
element value { $row/Quarter/data() },
element Month {
element value { $row/Month/data() },
$row/Week,
$row/Flight,
$row/Un,
$row/Air
}
}
}
}
If you want a single element for each year/quarter/month, use this code:
<data>
<row>{
for $year in //row/Year/data()
return
<Year>{
<value>{ $year }</value>,
for $quarter in //row[Year=$year]/Quarter/data()
return
<Quarter>{
<value>{ $quarter }</value>,
for $row in //row[Year=$year and Quarter=$quarter]
return
<Month>{
<value>{ Month/data() }</value>,
$row/*[not(local-name(.) = ('Month', 'Quarter', 'Year'))]
}</Month>
}</Quarter>
}</Year>
}</row>
</data>

Linq to XML Insert a new element into the XML for each node

I would like to insert a new element in the following XML for each instance of a data node
Here is the initial XML that I have:
<dataCollection totalCount="12" pageCount="1">
<data>
<date>2011-11-10T00:00:00.000-05:00</date>
<dataType>PRCP</dataType>
<station>GHCND:USW00014739</station>
<value>267</value>
<address>
<home>X</home>
</address>
</data>
<data>
<date>2011-11-10T00:00:00.000-05:00</date>
<dataType>PRCP</dataType>
<station>GHCND:USW00014739</station>
<value>267</value>
<address>
<home>X</home>
</address>
</data>
</dataCollection>
And this is the XML I am trying to achieve
<dataCollection totalCount="12" pageCount="1">
<data>
<date>2011-11-10T00:00:00.000-05:00</date>
<dataType>TMIN</dataType>
<station>GHCND:USW00014739</station>
<value>267</value>
<newValue>60</newValue>
<address>
<home>X</home>
</address>
</data>
<data>
<date>2011-11-10T00:00:00.000-05:00</date>
<dataType>TMAX</dataType>
<station>GHCND:USW00014739</station>
<value>270</value>
<newValue>62</newValue>
<address>
<home>X</home>
</address>
</data>
</dataCollection>
The XML data is used as a data source for a DataGrid using the following Linq.
Dim elements = (From daDsc In xdoc.Descendants("data") _
Select Data_Type = daDsc.Element("dataType").Value, _
Raw_Value = daDsc.Element("value").Value,
newValue = daDsc.Element("newValue"))
Writing this in VB.net, but answers in C# is OK. Thanks.
Try this:
foreach (var xe in xml.Descendants("data"))
{
xe.Element("value")
.AddAfterSelf(new XElement("newValue", 42));
}
If you wish to add newValue into your XML before processing, you could do something like this:
For Each element As XElement In xml.Elements
element.SetElementValue("newValue", "something")
Next
where xml is an XDocument, loaded from your web service.