Verifying data from xpath location having multiple data - karate

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>

Related

Karate: Match repeating element in xml

I'm trying to match a repeating element in a xml to karate schema.
XML message
* def xmlResponse =
"""
<Envelope>
<Header/>
<Body>
<Response>
<Customer>
<keys>
<primaryKey>1111111</primaryKey>
</keys>
<simplePay>false</simplePay>
</Customer>
<serviceGroupList>
<serviceGroup>
<name>XXXX</name>
<count>1</count>
<parentName>DDDDD</parentName>
<pendingCount>0</pendingCount>
<pendingHWSum>0.00</pendingHWSum>
</serviceGroup>
<serviceGroup>
<name>ZZZZZ</name>
<count>0</count>
<parentName/>
<pendingCount>3</pendingCount>
<pendingHWSum>399.00</pendingHWSum>
</serviceGroup>
</serviceGroupList>
</Response>
</Body>
</Envelope>
"""
I want to match each with following karate schema
Given def serviceGroupItem =
"""
<serviceGroup>
<name>##string</name>
<count>##string</count>
<parentName>##string</parentName>
<pendingCount>##string</pendingCount>
<pendingHWSum>##string</pendingHWSum>
</serviceGroup>
"""
This is how I tried
* xml serviceGroupListItems = get xmlResponse //serviceGroupList
* match each serviceGroupListItems == serviceGroupItem
But it doesn't work. Any idea how can I make it work
You have to match each serviceGroup.
* xml serviceGroupListItems = get xmlResponse //serviceGroupList
* match each serviceGroupListItems.serviceGroupList.serviceGroup == serviceGroupItem.serviceGroup

Read XML data in SQL

I want to query data from XML. I have managed to retrive data from another set of XML data but this are a bit problematic.
Bellow you see the data and the query that does not retrive any data.
DECLARE #xml XML
SET #xml=N'<DocumentXML>
<LoadApplicationResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Reaktor.Applikator.DTO">
<Application>
<EmbeddedProductList>
<EmbeddedProduct>
<Flag>false</Flag>
<CustomData>
<root xmlns="">
<Guaranteer ChangeTime="2012-04-28T08:50:07.5706054+02:00" ChangedBy="sven" OldValue="">
<Text>4</Text>
</Guaranteer>
<PercentGuarantee ChangeTime="2012-04-28T08:50:07.5706054+02:00" ChangedBy="sven" OldValue="">
<Number>100</Number>
</PercentGuarantee>
</root>
</CustomData>
<DataChangeTime>2014-04-28T08:50:07.5706054+02:00</DataChangeTime>
<ID>12</ID>
<FinanceSeparately>false</FinanceSeparately>
<Guid>5349efcd-457c-4423-b4bb-a28f97dd5e64</Guid>
<PluginData i:nil="true" />
<PriceCalcTime>2014-04-28T08:50:09.2580946+02:00</PriceCalcTime>
<Data>
<root xmlns="">
<root TableId="192">
<Generic.TypeCode>abba</Generic.TypeCode>
</root>
</root>
</Data>
</EmbeddedProduct>
<EmbeddedProduct>
<Flag>false</Flag>
<CustomData i:nil="true" />
<DataChangeTime>1954-10-03T00:00:00</DataChangeTime>
<ID>30</ID>
<FinanceSeparately>false</FinanceSeparately>
<Guid>d587b9b4-94df-4d9b-ba0d-2fdc62823a17</Guid>
<PluginData i:nil="true" />
<PriceCalcTime>2014-04-28T08:49:55.8831802+02:00</PriceCalcTime>
<Data>
<root xmlns="">
<root TableId="013">
<EmbProd.CMSPrice>0</EmbProd.CMSPrice>
<EmbProd.MonthFee Operator="DBLMUL" Target="CUSTOM.EPTermFee.ADD" Source="XPATH://PaySeries[1]/TermLength" DFValue="200">200</EmbProd.MonthFee>
</root>
<root TableId="759" GroupText="210" GroupText0="210">
<Flag>ink</Flag>
<Generic.TypeCode>fil</Generic.TypeCode>
</root>
</root>
</Data>
</EmbeddedProduct>
<EmbeddedProduct>
<Flag>false</Flag>
<CustomData>
<root xmlns="" />
</CustomData>
<DataChangeTime>2012-04-26T14:41:26.4232222+02:00</DataChangeTime>
<ID>16</ID>
<FinanceSeparately>false</FinanceSeparately>
<Guid>c2e2343f-a5d6-43c8-aa18-c43419d20165</Guid>
<PluginData i:nil="true" />
<PriceCalcTime>2014-04-28T08:49:55.8831802+02:00</PriceCalcTime>
<Data>
<root xmlns="">
<root TableId="102">
<EmbProd.MonthFee Operator="DBLMUL" Target="CUSTOM.EPTermFee.ADD" Source="XPATH://PaySeries[1]/TermLength" DFValue="300">300</EmbProd.MonthFee>
<EP.GenericCost Target="COST">114</EP.GenericCost>
</root>
<root TableId="102" GroupText="11" GroupText0="7">
<EP.TermCount Target="DBLMUL">13</EP.TermCount>
</root>
<root TableId="102" GroupText="210" GroupText0="210">
<Generic.TypeCode>frodinge</Generic.TypeCode>
</root>
</root>
</Data>
</EmbeddedProduct>
</EmbeddedProductList>
</Application>
</LoadApplicationResult>
</DocumentXML>'
SELECT tab.col.value('(Flag)[1]', 'nvarchar(max)') AS Flag
,tab.col.value('(Data/root/EmbProd.MonthFee)[1]', 'nvarchar(max)') AS Value
,tab.col.value('(ID)[1]', 'nvarchar(max)') AS Product
FROM #xml.nodes('/DocumentXML//LoadApplicationResult/Application/EmbeddedProductList/EmbeddedProduct') AS Tab(col)
The expected output should look like this:
+-------+-------+---------+
| Flag | Value | Product |
+-------+-------+---------+
| false | | 12 |
| false | 200 | 30 |
| true | 300 | 16 |
+-------+-------+---------+
You need to specify namespace
WITH XMLNAMESPACES ( 'http://schemas.datacontract.org/2004/07/Reaktor.Applikator.DTO' as x)
SELECT tab.col.value('(x:Flag)[1]', 'nvarchar(max)') AS Flag
,tab.col.value('(x:Data/root/root/EmbProd.MonthFee)[1]', 'nvarchar(max)') AS Value
,tab.col.value('(x:ID)[1]', 'nvarchar(max)') AS Product
FROM #xml.nodes('DocumentXML/x:LoadApplicationResult/x:Application/x:EmbeddedProductList/x:EmbeddedProduct') AS Tab(col);

how to delete empty namespace from child element in sql server

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)

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 , retrieving data

I have this XML:
<root>
<data name="lnkViewResultResource1.Text" xml:space="preserve">
<value>bekijk de resultaten</value>
</data>
<data name="lnkVoteResource1.Text" xml:space="preserve">
<value>stem</value>
</data>
<data name="number of results" xml:space="preserve">
<value>er waren reeds {0} stemmen op deze poll {1}</value>
</data>
</root>
I want to retrieve the "data" values, and for this I used:
Public Shared Function getlabels(ByVal filename As String) As Array
Dim labels = From l In XElement.Load(filename).Descendants("root").Elements("data") Select l
Return labels.ToArray
But, unfortunately, it is not working, it is not getting anything.
Any ideas?
Thanks in advance.
Alf.
You're loading the <root> XElement and asking it for descendants called root. Just get rid of the Descendants("root") call and it should be okay.
On the other hand, the query expression is pretty pointless... just use:
Return XElement.Load(filename).Elements("data").ToArray
Try
Dim labels = From l In XElement.Load(filename).Descendants("data") Select l