DataWeave transformation issue in esb mule - mysql

I have transformation in dataweave.
I am getting input from the Mysql database. Input payload type is List- Map. My dataweave editer has below code.
%dw 1.0
%namespace ns1 urn:abc:api:Components
%output application/xml
---
ns1#"ItemRequest":{
ns1#"Requester":
ns1#ac: payload.time_max,
ns1#"ErrorLanguage": "en_US",
ns1#"WarningLevel": "High"
}
My output is
<?xml version='1.0' encoding='UTF-8'?>
<ns1:ItemRequest xmlns:ns1="urn:abc:api:Components">
<ns1:Requester>
<ns1:ac>
<time_max>1</time_max>
</ns1:ac>
</ns1:Requester>
<ns1:ErrorLanguage>en_US</ns1:ErrorLanguage>
<ns1:WarningLevel>High</ns1:WarningLevel>
</ns1:AddItemRequest>
I am not sure why i am getting tag created under'ac' element in my output with 'time_max'. The 'Time_max' is the column name in MySQL table. But i dont want to add the column in the output xml. How can we avoid this tag creation ?
Expected output is
<?xml version='1.0' encoding='UTF-8'?>
<ns1:ItemRequest xmlns:ns1="urn:abc:api:Components">
<ns1:Requester>
<ns1:ac>1</ns1:ac>
</ns1:Requester>
<ns1:ErrorLanguage>en_US</ns1:ErrorLanguage>
<ns1:WarningLevel>High</ns1:WarningLevel>
</ns1:AddItemRequest>

I have resolved the issue by typing below code.
%dw 1.0
%namespace ns1 urn:abc:api:Components
%output application/xml
---
ns1#"ItemRequest":{
ns1#"Requester":
ns1#ac: payload[0].time_max,
ns1#"ErrorLanguage": "en_US",
ns1#"WarningLevel": "High"
}
The below line of snippet is did magic. I have included array index values.
ns1#ac: payload[0].time_max

Related

Adding parent xml node to json using xslt version 1.0

I need to add parent xml to json content using xslt version 1.0. I have tried multiple ways to get this but no luck.
**Input Content:**
------------------
{
"EmployerRegistrationReq": {
"EmployerRegistrationReq": {
"EmployerRegistrationHeader": {
"CorporateID": "SIT0001",
"CorpReferenceNumber": 48499964
}
}
}
}
**Expected Output:**
--------------------
<Library>
{
"EmployerRegistrationReq": {
"EmployerRegistrationReq": {
"EmployerRegistrationHeader": {
"CorporateID": "SIT0001",
"CorpReferenceNumber": 48499964
}
}
}
}
</Library>
Can anyone help me here:
In XSLT 1.0, this will be simple textual manipulation. You won't be able to treat your JSON as a structured item, at least, not without some additional libraries.
If your only problem is to take a "string" value, here a string that can be a serialization of a JSON value, and to make it the text() node child of an element <Library> then the problem is simply one of passing your JSON String text as a parameter to an XSLT 1.0 transformation and then use this stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="jsonString" />
<xsl:template match="/" >
<Library><xsl:value-of select="$jsonString" /></Library>
</xsl:template>
</xsl:stylesheet>
You can use an external entity, I think:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [
<!ENTITY json SYSTEM "json-sample1.json">
]>
<root>&json;</root>
Caveat: use of DTDs/entities might be restricted/disabled these days due to security default settings.

Azure APIM : Convert JSON Response to Customized XML Format

I have a requirement where I want to convert the JSON response , which is an array of object , to the customized XML format , so that my already existing code can parse it.
I know there is a Azure Transformation Policy named <json-to-xml /> , but there is no customization possible with it.
Sample JSON Response:
{
"data":[
{"a":1,"b":2},
{"a":3,"b":4}
],
"param2": "Success",
"param3": "true"
}
Desired XML Format:
<result>
<sub-res>
<res x="a" y=1>
<res x="b" y=2>
</sub-res>
<sub-res>
<res x="a" y=3>
<res x="b" y=4>
</sub-res>
</result>
I have tried using the liquid template as well but no success. Need guidance or pointers on this.
For this requirement, I created an api which response {"data":[{"a":1,"b":2},{"a":3,"b":4}]} to simulate your situation.
Then I use a <json-to-xml> in APIM policy first, the response will be convert to xml shown as below after the <json-to-xml> policy:
<Document>
<data>
<a>1</a>
<b>2</b>
</data>
<data>
<a>3</a>
<b>4</b>
</data>
</Document>
After that, use xslt to convert the xml to which you want.
Below is all of policy in my APIM for your reference:
The result of APIM show as what you want:

Invalid typenames in WFS:GetFeatures POST?

I'm trying to get even the simplest WFS HTTP_POST request to work, against a GeoServer WFS endpoint.
This HTTP_GET request works, and returns what I expect (newlines inserted for clarity):
http://mygeoserver.com/geoserver/ows?
service=wfs&
version=2.0.0&
request=getfeature&
count=3&
typenames=mynamespace:myfeaturetype&
cql_filter=dccode=%27XYZ%27
I'd expect this HTTP_POST request:
http://mygeoserver.com/geoserver/ows
with this request body:
<GetFeature
version="2.0.0"
service="WFS"
count="3"
xmlns="http://www.opengis.net/wfs/2.0"
xmlns:fes="http://www.opengis.net/fes/2.0"
xmlns:gsml="http://xmlns.geosciml.org/GeoSciML-Core/3.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/wfs/2.0
http://schemas.opengis.net/wfs/2.0/wfs.xsd"
>
<Query
typeNames="mynamespace:myfeaturetype"
>
<Filter
xmlns="http://www.opengis.net/fes/2.0"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:gsml="http://xmlns.geosciml.org/GeoSciML-Core/3.2" >
<PropertyIsEqualTo>
<ValueReference>dccode</ValueReference>
<Literal>XYZ</Literal>
</PropertyIsEqualTo>
</Filter>
</Query>
</GetFeature>
to return the same.
Instead I get an error:
cvc-datatype-valid.1.2.3: 'mynamespace:myfeaturetype' is not a valid value of union type 'TypeNamesType'.
cvc-attribute.3: The value 'mynamespace:myfeaturetype' of attribute 'typeNames' on element 'Query' is not valid with respect to its type, 'TypeNamesListType'.
What's throwing me for a loop is that the very same value that works for the typeNames parameter in the HTTP_GET throws an error in the HTTP_POST.
The problem seems to be that I need to specify a namespace definition in the XML for "mynamespace".
<Query
typeNames="mynamespace:myfeaturetype"
xmlns:mynamespace="http://????"
>
I'm assuming that this is available, somewhere within my GeoServer installation. But where?
It will be whatever you set as the URI of the workspace mynamespace. You can look it using a describeFeature request. So for example:
curl http://localhost:8080/geoserver/wfs\?service=wfs\&version=1.1.0\&request=DescribeFeatureType\&typeName=topp:states
gives:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:gml="http://www.opengis.net/gml"
xmlns:topp="http://www.openplans.org/topp"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://www.openplans.org/topp">
<xsd:import namespace="http://www.opengis.net/gml" schemaLocation="http://localhost:8080/geoserver/schemas/gml/3.1.1/base/gml.xsd"/>
<xsd:complexType name="statesType">
<xsd:complexContent>
[....]

XPath/JSONPath search of arrays

Our REST API is returning an array of nested objects. Using XPath or JSONPath, I would like to extract the id elements of each top level array object but not the children's id elements.
[
{
"id":"1",
"child":{
"id":"a"
}
},
{
"id":"2",
"child":{
"id":"b"
}
}
]
The expected output is 1, 2 and NOT 1, a, 2, b.
Can anybody help with the query syntax? I have the example at http://jsfiddle.net/dqvrfvc1/2/
Try selecting the id at a specific level:
search = JSON.search(data, '/*/*/id')
See the update here: http://jsfiddle.net/dqvrfvc1/5/
If we dump the XML to the console (console.log(JSON.toXML(data));) we see:
<d:data xmlns:d="defiant-namespace" d:constr="Array" d:mi="9">
<d:item d:mi="4">
<id d:constr="String" d:mi="1">1</id>
<child d:mi="3">
<id d:constr="String" d:mi="2">a</id>
</child>
</d:item>
<d:item d:mi="8">
<id d:constr="String" d:mi="5">2</id>
<child d:mi="7">
<id d:constr="String" d:mi="6">b</id>
</child>
</d:item>
</d:data>
This means that instead of /*/*/id, we can be even more specific with:
search = JSON.search(data, '/d:data/d:item/id')
Note: Namespace selection isn't possible, so there doesn't seem to be the need to bind the d: namespace prefix to the defiant-namespace uri.
Also, take a look at the "XPATH EVALUATOR" section of http://defiantjs.com and switch between XML and JSON views to see how the JSON is represented in XML. This will help you understand the the data structure and at what level id would be found.
Given you have jmeter in your tags here is a solution for it:
JSON Path Expression should look like: $[*].id
Demo:
References:
JSON Extractor
JSON Path: Getting Started
API Testing With JMeter and the JSON Extractor
You mentioned XPath: in XPath 3.1 this is
parse-json($data)?*?id

WCF service response from "html" to "xml"

trying to translate from one asmx web service to svc web service. same code, but got the following difference in responce
when using asmx:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://www.MyKidsSpending.com/">-2202.68</string>
when using svc:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">-2202.68</string>
where did the first line go?
Here is the fist couple of lines of the function that I am testing in the svc service....
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Xml)]
public string Balance(string Uid)
{
WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
I was suggested that the response was html, that is why it doesn't have
<?xml version="1.0" encoding="utf-8"?>
now I change text/html to text/xml, why doesn't still match? Suggestions? Thanks