WSO 2 EI Response change the first letter to uppercase - xslt

Hi in wso2 ei in payload factory i am getting the response in json
<payloadFactory media-type="json">
<format>{
"Body":$1
}
</format>
<args>
<arg evaluator="json" expression="$."/>
</args>
And the Response is :
{
"Body":{
"result":"done",
"idNumber":"123",
"address":{
"local":"US",
"abroad":"UK"
}
}
}
.... means multiple objects now what i need that all object first letter should be uppercase.
I need the below response
{
"Body":{
"Result":"done",
"IdNumber":"123",
"Address":{
"local":"US",
"abroad":"UK"
}
}
}
Means only the object first letter should be capitalize...Any help!

Replace payload mediator with xslt mediator .
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:strip-space elements="*"/>
<xsl:output method="text" indent="yes" media-type="application/json" encoding="UTF-8"/>
<xsl:template match="/">
{
Body :{
<xsl:for-each select="//*[local-name()='pid']">
Result:<xsl:value-of select="result"/>
IdNumber:<xsl:value-of select="idNumber"/>
</xsl:for-each>
}
}
</xsl:template>
</xsl:stylesheet>
Once this is done then you need to use property mediator so that the payload is still in json.
<property name="messageType" scope="default" type="STRING" value="application/json"/>
<property name="contentType" scope="default" type="STRING" value="application/json"/>

Related

WSO2 ESB From XML (multiple lines) to JSON (without wrappers)

I hope to find help here... Here is the particular case:
I get this XML from my Endpoint:
<Entries>
<Entry>
<Customer>1</Customer>
</Entry>
<Entry>
<Customer>2</Customer>
</Entry>
<Entries>
I can easily convert this XML to JSON by changing the Property messageType, which will result in:
{"Entries":{"Entry":[{"Customer": 1}, {"Customer": 2}]}}
Here is what I want to get, as a JSON result (without wrappers):
[{"Customer": 1}, {"Customer": 2}]
Is there someone who knows how?
Many thanks in advance!
I think you've to first manipulate your xml (maybe with an xslt mediator) to format it this way
<jsonArray>
<Customer>1</Customer>
<Customer>2</Customer>
</jsonArray>
Then I guess you'll get your expected output.
For instance the following xslt could do the job
<xsl:stylesheet exclude-result-prefixes="xsl" version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="utf-8" indent="yes" method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/Entries">
<xsl:element name="jsonArray">
<xsl:copy-of select="./Entry/Customer" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Thank you Anuruddha and Nicolas!
Your answers inspired me and I want to share what I did.
I have created this sequence, which I reuse with a Sequence mediator:
<sequence name="toJSON" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
<property name="result" scope="default" type="STRING" expression="json-eval($.Entries.Entry)"/>
<payloadFactory media-type="json">
<format>$1</format>
<args>
<arg evaluator="xml" expression="$ctx:result"/>
</args>
</payloadFactory>
</sequence>
It works so well I even changed the sequence of all my Proxies, even those that returns only 1 result.
Many thanks for your lights!!

WSO2 ESB - Modifying the output message content in the proxy service

I use WSO2 ESB (4.8.1) and would like to know how to get the output as shown in the "Expected output" block.
Thanks in advance.
Proxy service definition:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="DATA_WS"
transports="https,http"
statistics="disable"
trace="enable"
startOnLoad="true">
<target>
<inSequence>
<switch xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
source="local-name(/*/*/*[1])">
<case regex="getSavProducts">
<payloadFactory media-type="xml">
<format>
<soapenv:Envelope xmlns:dat="http://ws.wso2.org/dataservice">
<soapenv:Header/>
<soapenv:Body>
<dat:getSavProducts/>
</soapenv:Body>
</soapenv:Envelope>
</format>
<args/>
</payloadFactory>
<log level="custom">
<property name="operation" value="getSavProducts"/>
</log>
<send>
<endpoint>
<address uri="http://dssserver:9783/services/DATA_WS/getSavProducts"/>
</endpoint>
</send>
</case>
<default/>
</switch>
</inSequence>
<outSequence>
<iterate xmlns:m="http://ws.wso2.org/dataservice"
id="iter1"
expression="//m:Entries/m:Entry">
<target>
<sequence>
<log level="custom">
<property name="output111" expression="//m:PRODUCT_DESC/text()"/>
</log>
<send/>
</sequence>
</target>
</iterate>
</outSequence>
</target>
<publishWSDL key="gov:/IB/DATA_WS.wsdl"/>
<parameter name="useOriginalwsdl">true</parameter>
<parameter name="disableOperationValidation">true</parameter>
<description/>
</proxy>
this is the output i get when calling the service
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<Entry xmlns="http://ws.wso2.org/dataservice">
<PRODUCT_CODE>2</PRODUCT_CODE>
<SUB_PRODUCT_CODE>20</SUB_PRODUCT_CODE>
<PRODUCT_DESC>TEST SUB PRODUCT</PRODUCT_DESC>
<STATUS>A</STATUS>
<PRODUCT_CATEGORY>G</PRODUCT_CATEGORY>
<PRODUCT_NAME>General</PRODUCT_NAME>
<CUR_CODE>GBP</CUR_CODE>
<CHANNEL>USSD</CHANNEL>
</Entry>
</soapenv:Body>
</soapenv:Envelope>
log
TID: [0] [ESB] [2016-10-31 10:39:33,761] INFO {org.apache.synapse.mediators.builtin.LogMediator} - output111 = TEST SUB PRODUCT {org.apache.synapse.mediators.builtin.LogMediator}
TID: [0] [ESB] [2016-10-31 10:39:33,761] INFO {org.apache.synapse.mediators.builtin.LogMediator} - output111 = TEST SUB PRODUCT 2 {org.apache.synapse.mediators.builtin.LogMediator}
TID: [0] [ESB] [2016-10-31 10:39:33,768] INFO {org.apache.synapse.mediators.builtin.LogMediator} - output111 = TEST SUB PRODUCT 3 {org.apache.synapse.mediators.builtin.LogMediator}
Expected output
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:getSavProductsResponse SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="DATA_WS">
<return xsi:type="ns2:dbconncyberfin_DbtSavProdcutTab" xmlns:ns2="http://dbconncyberfin/DATA_WS.xsd">
<array xsi:type="ns3:Array" ns3:arrayType="ns2:dbconncyberfin_DbtSavProdcutUser[16]" xmlns:ns3="http://schemas.xmlsoap.org/soap/encoding/">
<item xsi:type="ns2:dbconncyberfin_DbtSavProdcutUser">
<channel xsi:type="xsd:string">USSD</channel>
<curCode xsi:type="xsd:string">EUR</curCode>
<productCategory xsi:type="xsd:string">G</productCategory>
<productCode xsi:type="xsd:string">2</productCode>
<productDesc xsi:type="xsd:string">TEST SUB PRODUCT</productDesc>
<productName xsi:type="xsd:string">General</productName>
<status xsi:type="xsd:string">A</status>
<subProductCode xsi:type="xsd:string">20</subProductCode>
</item>
<item xsi:type="ns2:dbconncyberfin_DbtSavProdcutUser">
<channel xsi:type="xsd:string">USSD</channel>
<curCode xsi:type="xsd:string">USD</curCode>
<productCategory xsi:type="xsd:string">G</productCategory>
<productCode xsi:type="xsd:string">2</productCode>
<productDesc xsi:type="xsd:string">TEST SUB PRODUCT 2</productDesc>
<productName xsi:type="xsd:string">General</productName>
<status xsi:type="xsd:string">A</status>
<subProductCode xsi:type="xsd:string">54</subProductCode>
</item>
<item xsi:type="ns2:dbconncyberfin_DbtSavProdcutUser">
<channel xsi:type="xsd:string">USSD</channel>
<curCode xsi:type="xsd:string">SLR</curCode>
<productCategory xsi:type="xsd:string">G</productCategory>
<productCode xsi:type="xsd:string">1</productCode>
<productDesc xsi:type="xsd:string">TEST SUB PRODUCT 3</productDesc>
<productName xsi:type="xsd:string">General</productName>
<status xsi:type="xsd:string">A</status>
<subProductCode xsi:type="xsd:string">00</subProductCode>
</item>
</array>
</return>
</ns1:getSavProductsResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
solved it using xsl with xslt mediator.
item1.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
xmlns:m="http://ws.wso2.org/dataservice" version="2.0" exclude-result-prefixes="m fn"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns1="DATA_WS">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<ns1:getSavProductsResponse>
<return xsi:type="ns2:dbconncyberfin_DbtSavProdcutTab" xmlns:ns2="http://dbconncyberfin/DATA_WS.xsd">
<array xsi:type="ns3:Array" ns3:arrayType="ns2:dbconncyberfin_DbtSavProdcutUser[16]" xmlns:ns3="http://schemas.xmlsoap.org/soap/encoding/">
<xsl:for-each select="//m:Entries/m:Entry">
<item xsi:type="ns2:dbconncyberfin_DbtSavProdcutUser">
<channel xsi:type="xsd:string"><xsl:value-of select="m:CHANNEL"/></channel>
<curCode xsi:type="xsd:string"><xsl:value-of select="m:CUR_CODE"/></curCode>
<productCategory xsi:type="xsd:string"><xsl:value-of select="m:PRODUCT_CATEGORY"/></productCategory>
<productCode xsi:type="xsd:string"><xsl:value-of select="m:PRODUCT_CODE"/></productCode>
<productDesc xsi:type="xsd:string"><xsl:value-of select="m:PRODUCT_DESC"/></productDesc>
<productName xsi:type="xsd:string"><xsl:value-of select="m:PRODUCT_NAME"/></productName>
<status xsi:type="xsd:string"><xsl:value-of select="m:STATUS"/></status>
<subProductCode xsi:type="xsd:string"><xsl:value-of select="m:SUB_PRODUCT_CODE"/></subProductCode>
</item>
</xsl:for-each>
</array>
</return>
</ns1:getSavProductsResponse>
</xsl:template>
outSequence
<outSequence>
<xslt key="gov:/xslt/item1.xsl"/>
<enrich>
<source type="body" clone="true"/>
<target type="property" property="original"/>
</enrich>
<payloadFactory media-type="xml">
<format>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body/>
</SOAP-ENV:Envelope>
</format>
<args/>
</payloadFactory>
<enrich>
<source type="property" clone="true" property="original"/>
<target type="body"/>
</enrich>
<send/>
</outSequence>

How would i transform the message Dynamically In WSO2ESB

I am using wso2esb4.8.0
I wish to transform the message using wso2esb .Actaulle need to add Complex Element to Payload
How would i achive this.
my Client request is
<?xml version="1.0" encoding="UTF-8"?>
But my Adapter endpoint will allow the request in this format.If it is for one operation i may follow payload mediator to make my request But those are bumch of requests
so Endpoint allowing request is
Just extract the Operation_Name and adding it as Complex Element But I am unable to do it I am trying in Proxy like this
Proxy is
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="SamplePOC7"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<property xmlns:open="http://www.openuri.org/"
xmlns:ns="http://org.apache.synapse/xsd"
xmlns:env="http://eai.mmn.mm/Envelope"
xmlns:poin="http://eai.mm.mm/gg"
name="Operation_Name"
expression="//poin:Operation_Name/text()"
scope="default"
type="STRING"/>
<property xmlns:open="http://www.openuri.org/"
xmlns:ns="http://org.apache.synapse/xsd"
xmlns:env="http://eai.mmn.xxxx/Envelope"
name="AddElement"
expression="concat('open:',get-property('Operation_Name'))"
scope="default"
type="STRING"/>
<enrich>
<source xmlns:env="http://eai.mmn.mm/Envelope"
clone="true"
xpath="//env:Payload/*"/>
<target xmlns:poin="http://eai.mm.XXX/gg"
xpath="concat('open:',//poin:Operation_Name/text())"/>
</enrich>
<log level="full">
<property name="Message" expression="get-property('AddElement')"/>
</log>
</inSequence>
<outSequence/>
</target>
<description/>
</proxy>
But its giving errors like the below my error log is
[2014-05-15 10:53:48,167] INFO - ProxyService Successfully created the Axis2 se
rvice for Proxy service : SamplePOC7
[2014-05-15 10:53:53,089] ERROR - EnrichMediator Invalid Target object to be enr
ich.
[2014-05-15 10:54:19,160] INFO - ProxyService Building Axis service for Proxy s
With which mediator i can extract this request as per my desire way please if you know paste configuration or else provide any example
Thanks in advance
You can use XSLT mediator or JavaScript
A sample proxy with javascript doing what you want :
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="TestSOF"
transports="http"
startOnLoad="true"
trace="disable">
<description/>
<target>
<inSequence>
<property name="FORCE_SC_ACCEPTED" value="true" scope="axis2"/>
<property xmlns:open="http://www.openuri.org/"
xmlns:ns="http://org.apache.synapse/xsd"
xmlns:poin="http://eai.mtn.iran/PointOfSales"
name="Operation_Name"
expression="//poin:Operation_Name/text()"
scope="default"
type="STRING"/>
<property xmlns:ns="http://org.apache.synapse/xsd"
xmlns:poin="http://eai.mtn.iran/PointOfSales"
name="PointOfSales"
expression="//poin:PointOfSales"
scope="default"
type="STRING"/>
<script language="js"><![CDATA[
mc.setPayloadXML(<open:{mc.getProperty("Operation_Name")} xmlns:open="http://www.openuri.org/">
{new XML(mc.getProperty("PointOfSales"))}
</open:{mc.getProperty("Operation_Name")}>);
]]></script>
<log level="full"/>
</inSequence>
</target>
</proxy>
A sample using XSLT :
<?xml version="1.0" encoding="UTF-8"?>
<localEntry xmlns="http://ws.apache.org/ns/synapse" key="SOFXSL">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:open="http://openuri.org/"
xmlns:env="http://eai.mtnn.iran/Envelope"
xmlns:poin="http://eai.mtn.iran/PointOfSales"
version="2.0">
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="no"/>
<xsl:param name="operationname"/>
<xsl:template match="/">
<xsl:element name="open:{$operationname}">
<xsl:apply-templates select="//poin:PointOfSales"/>
</xsl:element>
</xsl:template>
<xsl:template match="#*|*|comment()">
<xsl:copy>
<xsl:apply-templates select="#*|*|text()|comment()|processing-instruction()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<description/>
</localEntry>
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="TestSOF"
transports="http"
startOnLoad="true"
trace="disable">
<description/>
<target>
<inSequence>
<property name="FORCE_SC_ACCEPTED" value="true" scope="axis2"/>
<property xmlns:open="http://www.openuri.org/"
xmlns:ns="http://org.apache.synapse/xsd"
xmlns:poin="http://eai.mtn.iran/PointOfSales"
name="Operation_Name"
expression="//poin:Operation_Name/text()"
scope="default"
type="STRING"/>
<xslt key="SOFXSL">
<property name="operationname" expression="get-property('Operation_Name')"/>
</xslt>
<log level="full"/>
</inSequence>
</target>
</proxy>

XSLT output value-of select is incorrect

My template_1.xml file
<?xml version="1.0" encoding="UTF-8"?>
<DSExport>
<TableDefinitions>
<Property Name="Category">\Table Definitions\Teradata\XML_TEST</Property>
<Property Name="ShortDesc">Imported from: SRC_COLUMN_ADD_TEST</Property>
<Collection Name="Columns" Type="MetaColumn">
<SubRecord>
<Property Name="Name">CUST_ID_1</Property>
<Property Name="Description">CUST_ID: nullable int32</Property>
<Property Name="SqlType">4</Property>
<Property Name="Precision">9</Property>
<Property Name="Scale">0</Property>
<Property Name="Nullable">1</Property>
</SubRecord>
<SubRecord>
<Property Name="Name">DESCR</Property>
<Property Name="Description">DESCR: nullable string[max=144]</Property>
<Property Name="SqlType">12</Property>
<Property Name="Precision">144</Property>
<Property Name="Scale">0</Property>
<Property Name="Nullable">1</Property>
</SubRecord>
<SubRecord>
<Property Name="Name">CUST_ADDR</Property>
<Property Name="Description">CUST_ADDR: string[max=500]</Property>
<Property Name="SqlType">12</Property>
<Property Name="Precision">500</Property>
<Property Name="Scale">0</Property>
<Property Name="Nullable">0</Property>
</SubRecord>
<SubRecord>
<Property Name="Name">AGE</Property>
<Property Name="Description">AGE: nullable int32</Property>
<Property Name="SqlType">4</Property>
<Property Name="Precision">9</Property>
<Property Name="Scale">0</Property>
<Property Name="Nullable">1</Property>
</SubRecord>
</Collection>
Hi I am new to XSLT , I tried lot to get my expected out as mentioned below, some how am getting result only from same attribute value of same element, please help on this...Thanks
My template.xsl file
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<DSExport>
<id-of>
<xsl:for-each select="//SubRecord">
<SubRecord>
<xsl:value-of select="//SubRecord/Property[#Name]" />
</SubRecord>
</xsl:for-each>
</id-of>
</DSExport>
</xsl:template>
</xsl:stylesheet>
My current ouput: output.xml
<?xml version="1.0" encoding="UTF-8"?>
<DSExport>
<id-of>
<SubRecord>CUST_ID_1</SubRecord>
<SubRecord>CUST_ID_1</SubRecord>
<SubRecord>CUST_ID_1</SubRecord>
<SubRecord>CUST_ID_1</SubRecord>
</id-of>
</DSExport>
My expected output :
<?xml version="1.0" encoding="UTF-8"?>
<DSExport>
<id-of>
<SubRecord>CUST_ID_1</SubRecord>
<SubRecord>DESCR</SubRecord>
<SubRecord>CUST_ADDR</SubRecord>
<SubRecord>AGE</SubRecord>
</id-of>
</DSExport>
Your problem is with this expression
<xsl:value-of select="//SubRecord/Property[#Name]" />
The first slash at the start of the xpath expression indicates it is an absolute path, and so it will start searching from the document element. Two slashes then indicate it will search for the SubRecord element at any level in the document. This results in it always finding the first SubRecord element in the XML, regardless of where you are currently positioned.
You need to use a relative expression here. It will be relative to the context node you are currently positioned on (which is a SubRecord element). Try replacing it with this
<xsl:value-of select="Property[#Name]" />
Note that, strictly speaking this will get the first Property element which has an attribute of Name present. Although this gives your expected result, maybe it would be better written as this
<xsl:value-of select="Property[#Name='Name']" />
i.e. Get the Property which has a Name attribute with a value of Name.

WSO2 ESB XSLT Mediator creates temp files (never cleaned)

I use WSO2 ESB 4.5.1 on Windows.
My problem is that the temp folder WSO2_HOME/tmp is growing up and never cleaned.
I found out that the problem comes from the xslt mediator, everytime it transform a big xml file (~15kb) a new temp file is created.
Does anyone have an idea why these tmp files are not cleaned up?
Proxy:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse" name="input" transports="vfs" startOnLoad="true" trace="disable">
<parameter name="transport.PollInterval">5</parameter>
<parameter name="transport.vfs.FileURI">vfs:file://C:/WSO2/Test/From</parameter>
<parameter name="transport.vfs.FileNamePattern">.*[.].*</parameter>
<parameter name="transport.vfs.ContentType">application/xml</parameter>
<target faultSequence="errorSequence">
<inSequence>
<log level="full"/>
<property name="ClientApiNonBlocking" scope="axis2" action="remove"/>
<property name="OUT_ONLY" value="true"/>
<xslt key="avintis_xml_indent"/>
<property name="transport.vfs.ReplyFileName" expression="fn:concat('out_', $trp:FILE_NAME, '.xml')" scope="transport"/>
<send>
<endpoint>
<address uri="vfs:file://C:/WSO2/Test/To"/>
</endpoint>
</send>
</inSequence>
</target>
</proxy>
XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:avintis="http://www.avintis.com/esb"
xmlns:urn="urn:hl7-org:v2xml" version="2.0"
xmlns:payload="http://ws.apache.org/commons/ns/payload">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="*|text()|#*">
<xsl:copy>
<xsl:apply-templates select="*|text()|#*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
inputfile:
any xml file bigger than ~15kb
Seems temp files do not get cleaned up by the HouseKeeping Task. To clean them using the housekeeping task you can configure it in the Carbon.xml as shown below.
<WorkDirectory>${carbon.home}/tmp/work</WorkDirectory>
<HouseKeeping>
<AutoStart>true</AutoStart>
<Interval>10</Interval>
<MaxTempFileLifetime>30</MaxTempFileLifetime>
</HouseKeeping>