Replacing XML node key using dataprovider karate - karate

I am reading my xml structure from a file and replacing the values from the examples
def inputXml = read('classpath:xml/input.xml')
My xml structure is like below
<input>
<data>
<props>
<p n="AMOUNT">1000</p>
<p n="NAME">name</p>
<p n="{ACCOUNTTYPE}">purpose</p>
</props>
</data>
</input>
I am driving data from examples and setting/replacing the input xml as below
* set inputXml/input/data/props/p[#n='AMOUNT'] = <AMOUNT> -- Works fine
* set inputXml/input/data/props/p[#n='NAME'] = <NAME> -- Works fine
Examples:
|AMOUNT|NAME|ACCOUNTTYPE|PURPOSE|
|100|abc|BUSINESS|smallbusiness|
|2000|def|PERSONAL|home|
I want to update the "n" tag name ACCOUNTTYPE and corresponsding PURPOSE similar to above(both key and value). Is there a way to achieve this in Karate? Please suggest.
Is is possible to use '#(accountType)' and '#(purpose)' inside the xml file similar to json and run?

Is is possible to use '#(accountType)' and '#(purpose)' inside the xml file
Yes. Please read the docs: https://github.com/intuit/karate#embedded-expressions
And the examples: xml.feature
Given def user = <user><name>john</name></user>
And def lang = 'en'
When def session = <session><locale>#(lang)</locale><sessionUser>#(user)</sessionUser></session>

Related

Karate "match contains" for xml does not work like JSON [duplicate]

This question already has an answer here:
How to compare XML response with Json in Karate
(1 answer)
Closed 1 year ago.
I've noticed that "match contains" for checking if xml structure contains in another xml structure works differently than json counterpart. To give you an example :
* def expectedResult =
"""
<?xml version="1.0" encoding="UTF-8"?>
<response>
<id>1</id>
<id>2</id>
<id>3</id>
</response>
"""
* header Accept = xml
Given path 'some\path'
When method GET
Then status 200
## If response contains -
## <response><id>1</id></response>
## The following match will fail with -
## "actual and expected arrays are not the same size - 1:3"
And match response contains expectedResult
If it's a JSON structure, it will pass because 1 contains in the list of 1,2,3. But in XML, it's still checking array size. Is that how it suppose to work?
Update : This is not the same question as the other question. I'm trying to compare reference XML file that I have with XML structure being returned from the API. The question you linked to is comparing JSON reference file to XML response. So, that begs the question, in karate, do I have to convert my reference XML and response XML to json just to perform "contains"?
XML has some fundamental "shape" differences from JSON. I leave it to you to figure out, one of the challenges is the concept of a "root" tag, which JSON does not have. So they will not "match up" the way you may expect.
One tip - convert XML to JSON if in doubt. Here is an example:
* def expected =
"""
<?xml version="1.0" encoding="UTF-8"?>
<response>
<id>1</id>
<id>2</id>
<id>3</id>
</response>
"""
* json temp = $expected/response
* match temp.response.id == ['1', '2', '3']

Extract field from SQL XML

I've searched on this website and internet but can't seem to get anything to work for this simple request. I just want to display the word relating to the term e.g. using the xml column below TEST will be extracted.
The xml column is as below:
<A>
<resources id="1" resourceType="Template" term="TEST" version="1" />
</A>
I've tried using the following but it returns NULLS.
select
Res.value('(/A/term)[1]','nvarchar(50)') as 'Res'
from Con
where Res is not null
Thank you in advance.
Try it like this
select
Res.value('(/A/resources/#term)[1]','nvarchar(50)') as 'Res'
from Con
where Res is not null
term is an attribute (needs a # signum) within the element <resources> below <A>. Your xpath wans't reflecting this correctly...
Try this
SELECT T.tm.value('#term[1]','nvarchar(50)') as 'Res'
FROM Con
CROSS APPLY Res.nodes('/A/resources') as T(tm)

how to customize xml headers

I have a resultSet formed by:
LazyList<DeliveryLabel> deliveryLabels = DeliveryLabel.findBySQL(sql);
String wxml = deliveryLabels.toXml(true, true);
The xml generated start with:
<delivery_labels>
<delivery_label>
<data>
</delivery_label>
</delivery_labels>
how to generate only:
<labels>
<label>
<data>
</label>
</labels>
I´m can´t rename the class to Label.java, because it already exists in the project.
[]´s
Currently ActiveJDBC uses Inflections to pick names for XML tags. If you need custom tags or structure, you will have to implement a custom toXml() method.

SimpleXML save tag content as variable

OK I've been working with tag attributes up to now, but what if I want to save the actual contents of a tag as a variable, how would I do that?
For instance, in the example below, how would I save 'John' to a variable?
<person>
<name>John</name>
</person>
Thanks!
You're talking about SimpleXML in PHP?
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?><person><name>John</name></person>');
$john = $xml->name ;
echo $john ;
The reason we use $xml->name in our example rather than $xml->person->name is that SimpleXML will assume the root element (worth keeping in mind :). In a real example the XML would have a different root element, with perhaps several <person> elements, which you then could get by array notation, as in ;
$james = $xml->person[4]->name ;
A more powerful way is to use Xpath which is worth looking into for better dealing with complex XML ;
$john = $xml->xpath ( 'person/name' ) ;
Using PHP, you can do it in this way:-
<?php
$xmlstr = <<<XML
<person>
<name>John</name>
</person>
XML;
$xml = new SimpleXMLElement($xmlstr);
$name_person = $xml->name;
// If you are unsure about the node string, then it's best to write it as:-
$name_person = $xml->{'name'};
/**
* This above statement will take care if the node string contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.
*/
?>
More info is available here.
Hope it helps.

XML Import with "alternate" form or xml formatting

I have successfully imported an XML file parsing elements info table attributes using this xml data formating:
<PN>
<guid>aaaa</guid>
<dataInput>0</dataInput>
<deleted>false</deleted>
<customField1></customField1>
<customField2></customField2>
<customField3></customField3>
<description></description>
<name>name1></name>
<ccid>CC007814</ccid>
<productIds>bbbb</productIds>
</PN>
but it errors whwen I input an XML in this format:
<PN guid="aaaa"
deleted="false"
customField1=""
customField2=""
customField3=""
description=""
modified="2010-10-20T00:00:00.001"
created="2010-05-20T18:07:10.416"
name="name1"
ccid="CC006035"
productIds="bbbb"/>
Is this later form usable? Any help would be appreciated. Thanks.
It's usable, but you're looking at the difference between using tags (your first example) and attributes (your second example). Your processing is slightly different.