如何在适配器中对输入请求执行xslt转换 [英] how to perform xslt transformation for input request in adapter

查看:97
本文介绍了如何在适配器中对输入请求执行xslt转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为 WL.Server.invokeHttp 创建了一个 XSLT ,它从后端服务器获取数据XML格式并成功执行转换。这个适配器用于从后端服务器获取数据。

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.

但是在以XML格式向服务器发送数据时,因为我将适配器中的数据作为JSON获取,我需要转换使用XSLT将其转换为XML。

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.

在ibm worklight适配器中是否有任何规定我可以对输入请求执行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)

例如,

如果我想获取所有员工的数据,我将调用一个适配器,即 getEmpDetails ,它以XML格式返回数据,但我不想显示从后端服务器返回的所有字段,所以我使用XSLT过滤特定数据,我的移动应用程序在JSON中使用它,所以我也转换为JSON格式。

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.

来自服务器的数据

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

通过分配XSLT进行转换后适配器我得到以下数据

After transformation by assigning XSLT in adapter i am getting following data

"array":{

      "employee":[{

         "empname":"John Methew",
         "empdept":"Accounts"


       },{

         "empname":"Anna",
         "empdept":"Business Development"


       }],

}

现在,如果我想通过 createEmp adatper添加新员工然后我需要从移动设备传递JSON格式的输入请求以创建新员工,我的适配器需要将此请求转换为XML(在这里,我想要一个XSLT过滤器,就像我在 getEmpDetails 适配器)。

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 ).

创建员工的输入请求

"employee":{

             "empname":"Rahul",
             "empdept":"Softwares",
             "empid":"4233",
             "emppay":"20k"


           }

适配器将接受响应并需要将上面的JSON输入请求转换为XML,所以我问工作灯是否有任何规定来转换传入的请求?

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未将XML转换为JSON。 XSLT将XML转换为另一种XML而不是JSON。

XSLT is not converting XML to JSON. XSLT transforms XML into another XML not JSON.

一旦您的适配器从后端检索XML,WL服务器基础结构将自动将其转换为JSON。如果您定义了XSLT,则从后端检索的原始XML将首先根据XSLT转换为新的XML,然后将新的XML转换为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.

没有开箱即用的API可以将JSON对象转换为WL适配器中的XML字符串。你可以在这里遵循两种不同的方法

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


  1. 使用第三方Java / JavaScript库将JSON转换为XML,例如: https://code.google.com/p/x2js/ http://www.json.org/java/ 。您可以在此处阅读有关在适配器中使用Java的更多信息 http://www.ibm .com / developerworks / mobile / worklight / getting-started.html ,搜索在适配器中使用java

  1. 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"

手动创建XML模板并使用JSON中的值填充它们。请参阅 http:第18页以及//public.dhe.ibm.com/software/mobile-solutions/worklight/docs/v620/04_02_HTTP_adapter_-_Communicating_with_HTTP_back-end_systems.pdf 。使用{myJsonObject.propertyName}将值注入XML。

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.

这篇关于如何在适配器中对输入请求执行xslt转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆