How to combine value from XML with a String? - mule

I have a listener that receives a XML payload. In the following transform I would like to combine a string and a value this XML, but it breaks
This is the XML I receive:
<?xml version="1.0" encoding="UTF-8"?>
<INVOIC02>
<IDOC BEGIN="1">
<RESULT>12345</RESULT>
</IDOC>
</INVOIC02>
This is my transformation:
%dw 2.0
output text/plain
---
"Result:" ++ (payload.INVOICE.IDOC.RESULT)
Apparently I access the payload wrong, I guess. The error message looks like this:
You called the function 'Value Selector' with these arguments:
1: String ("\n\n ...)
2: Name ("INVOICE")
But it expects one of these combinations:
(Array, Name)
...
Any idea what I'm doing wrong?

The problem is that the xml doesn't have the mimetype set. As a workaround set the payload with value payload and set also the mimetype to xml

Related

Convert file to JSON in Mule 4

I extracted a file that comes in a zip and looks similar to this:
Payload =
<?xml version="1.0" encoding="UTF-8"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><dimension ref="A1"/><sheetViews><sheetView workbookViewId="0" tabSelected="true"/></sheetViews><sheetFormatPr defaultRowHeight="15.0"/><sheetData>
<row r="1">
<c r="A1" t="inlineStr"><is><t>First</t></is></c><c r="B1" t="inlineStr"><is><t>Middle</t></is></c><c r="C1" t="inlineStr"><is><t>Last</t></is></c><c r="D1" t="inlineStr"><is><t>SSN</t></is></c><c r="E1" t="inlineStr"><is><t>Street</t></is></c><c r="F1" t="inlineStr"><is><t>MailingState</t></is></c><c r="G1" t="inlineStr"><is><t>Code</t></is></c><c r="H1" t="inlineStr"><is><t>MailingCountry</t></is></c><c r="I1" t="inlineStr"><is><t>Birthdate</t></is></c><c r="J1" t="inlineStr"><is><t>name</t></is></c><c r="K1"
<row r="2">
<c r="A2" t="inlineStr"><is><t>William</t></is></c><c r="A5" t="inlineStr"><is><t></t></is></c><c r="B2" t="inlineStr"><is><t>William</t></is></c><c r="D2" t="inlineStr"><is><t>123456798</t></is></c><c r="E2" t="inlineStr"><is><t>Test</t></is></c><c r="F2" t="inlineStr"><is><t>XX</t></is></c><c r="G2" t="inlineStr"><is><t>12345</t></is></c><c r="H2" t="inlineStr"><is><t></t></is></c><c r="I2" t="inlineStr"><is><t>1992-13-11T04:00:00</t></is></c><c r="J2"
mediaType = application/java; charset=UTF-8
How can I convert that file to something that I can manipulate better, for example JSON.
Mule Runtime version 4.4.0 EE
Use the read() function to parse it as the XML file the content appears to be, then transform to whatever you want.
Example DataWeave transformation:
output application/json
---
read(payload, "application/xml")
Note that the payload seems truncated. If it is really XML above script will work. Also I recommend to perform any transformation before outputting to JSON, to avoid performance issues. If you are doing more transformations later in the flow is better to output to application/java and convert to JSON in the last transformation.

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']

How to create a message containing xml prolog declaration

I am trying to create some dynamic xml messages which will be sent to a service which expects xml. Said service also needs the xml prolog to be specified.
As I want to do a lot of manipulation on the xml I am reading it in a native xml type, however it seems that karate loses the xml prolog information during the conversion (on 0.9.5 and 0.9.6.RC3)
Feature: Example
# https://www.w3.org/TR/xml/#sec-prolog-dtd
Scenario: Example
Given def nativeXML =
"""
<?xml version="1.0" encoding="UTF-8" ?>
<greeting>Hello world</greeting>
"""
When string nativeToString = nativeXML
* print nativeToString
Then assert nativeToString.contains("encoding")
Is there a way to preserve the xml prolog?
Code to reproduce this can be found here: https://github.com/KostasKgr/karate-issues/blob/xml_prolog_to_string/src/test/java/examples/example.feature
Kind regards
Right now there is no other way but to append it manually, which IMO is simple enough. Karate removes it by default during a string conversion, because it gets in the way during Node operations.
Given def temp =
"""
<?xml version="1.0" encoding="UTF-8" ?>
<greeting>Hello world</greeting>
"""
* string body = temp
* def body = '<?xml version="1.0" encoding="UTF-8" ?>' + body
* print body
Then assert body.contains("encoding")

exctractvalue from xmltype / soap oracle

i need to get "ineedthis" value from field
REQUEST_INFO:
...
<s:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<X-dynaTrace xmlns="http://ns.dynatrace.com/wcf" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">FW3;-1003312095;1;-56375709;115092;0;975784079;78</X-dynaTrace>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<storeContract xmlns="xxx/integration">
<storeRequest>
<contract>
<contractSeries>ineedthis</contractSeries>
select extractvalue(XMLType(sap.REQUEST_INFO),'s/s/storeContract/storeRequest/contract/contractSeries')
from sap
cant get the value
You're trying to extract the path
s/s/storeContract/storeRequest/contract/contractSeries
but your SOAP response doesn't have any nodes called s; it has nodes called Envelope, Header and Body in the namespace s. So you potentially want the path:
/s:Envelope/s:Body/storeContract/storeRequest/contract/contractSeries
which on its own gets an LPX-00601: Invalid token error because it doesn't know what s: is. You can supply the namespaces with the third argument:
select extractvalue(XMLType(sap.request_info),
'/s:Envelope/s:Body/storeContract/storeRequest/contract/contractSeries',
'xmlns="xxx/integration" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"'
) as contractseries
from sap;
or the lazy way is to wildcard the namespace and only identify the final node you want:
select extractvalue(XMLType(sap.request_info),'//*:contractSeries') as contractseries
from sap;
But extractvaue is deprecated so it's better to use XMLQuery - still being lazy:
select XMLQuery('//*:contractSeries/text()'
passing XMLType(sap.request_info)
returning content) as contractseries
from sap;
or with explicit namespaces:
select XMLQuery('
declare default element namespace "xxx/integration";
declare namespace s="http://schemas.xmlsoap.org/soap/envelope/";
/s:Envelope/s:Body/storeContract/storeRequest/contract/contractSeries/text()'
passing XMLType(sap.request_info)
returning content) as contractseries
from sap;
CONTRACTSERIES
------------------------------
ineedthis
db<>fiddle

Mule: DataMapper - CSV to XML Conversion , Final XML is not formatted with root element

I'm new to mule, working with Matamapper which converts csv file to xml. I'm able to convert csv to xml . But the final output response is not formatted with actual root element. Please find my final output xml structure. It doesnt have root element. Kindly guide me . If this is correct, help me how to insert root element. ( Bcz i tried Set payload to insert the root element) it is not working correclty.
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<name>jothi</name>
<street>123, main</street>
</employees>
<employees>
<name>Jane </name>
<street>Main Street</street>
</employees>
Also referred : csv to xml: not sure the best way to do it in Mule ESB
http://www.mulesoft.org/documentation/display/current/DataMapper+Flat-to-Structured+and+Structured-to-Flat+Mapping - giving errors in datamapper
Thank you.
Upgrading MuleStudio to 3.5 cleaned up a lot of the errors I was seeing using the DataMapper