i have created a XSLT
for the WL.Server.invokeHttp
which gets the data from the backend server in XML format and performing transformation successfully. this adapter is used to get the data from backend server.
but while sending data to server in XML , as i am getting data in adapter as JSON , i need to transform it into XML using XSLT.
is there any provision in ibm worklight adapters that i can perform XSLT transformation for input request just the way i can do it easily while getting response from backend server.
Here i am explaining the return data ( i.e. response)
for example,
if i want to fetch data for all employees , i will invoke an adapter i.e. getEmpDetails
which is returning data in XML , but i don't want to display all the fields those are returning from backend server , so i am using XSLT to filter the specific data and my mobile app is consuming it in JSON so i am transforming into JSON format also.
Data from Server
<Employee>
<EmpName>Jhon Methew</EmpName>
<EmpId>1234</EmpId>
<EmpDepartment>Accounts</EmpDepartment>
</Employee>
<Employee>
<EmpName>James</EmpName>
<EmpId>4434</EmpId>
<EmpDepartment>Sales</EmpDepartment>
</Employee>
<Employee>
<EmpName>Anna</EmpName>
<EmpId>3344</EmpId>
<EmpDepartment>Business Development</EmpDepartment>
</Employee>
After transformation by assigning XSLT in adapter i am getting following data
"array":{
"employee":[{
"empname":"John Methew",
"empdept":"Accounts"
},{
"empname":"Anna",
"empdept":"Business Development"
}],
}
now if i want to add a new employee via createEmp
adatper then i need to pass the input request in JSON format from mobile to create a new employee, and my adapter needs to transfrom this request into XML ( here, i want an XSLT filter the way i am heaving in getEmpDetails
adapter ).
input request to create employee
"employee":{
"empname":"Rahul",
"empdept":"Softwares",
"empid":"4233",
"emppay":"20k"
}
adapter will accept the response and need to convert above JSON input request to XML , so i was asking is there any provision in worklight to transform incoming request ?
<Employee>
<EmpName>Rahul</EmpName>
<EmpId>4233</EmpId>
<EmpDepartment>Softwares</EmpDepartment>
<EmpPayment>20K</EmpPayment>
</Employee>
XSLT is not converting XML to JSON. XSLT transforms XML into another XML not JSON.
Once your adapter has retrieved XML from a backend the WL Server infrastructure will automcatically convert it to JSON. In case you have XSLT defined the original XML retrieved from a backend will first be translated into new XML according to XSLT and after that the new XML will be converted to JSON.
There's no out-of-the-box APIs that can convert JSON object to XML string in WL adapters. You can follow two different approaches here
Use 3rd party Java/JavaScript library to convert JSON to XML, e.g. https://code.google.com/p/x2js/ or http://www.json.org/java/. You can read more about using Java in adapters here http://www.ibm.com/developerworks/mobile/worklight/getting-started.html , search for "Using java in adapters"
Manually create your XML templates and populate them with values from JSON. See page 18+ of http://public.dhe.ibm.com/software/mobile-solutions/worklight/docs/v620/04_02_HTTP_adapter_-_Communicating_with_HTTP_back-end_systems.pdf. Use {myJsonObject.propertyName} to inject values to XML.