Validation String XSLT WSO2 - xslt

I'm newbie about validation on programming. Like Checking string and checking field input empty or not. Maybe anyone in here can share me about simple sample validation rule about string and checking field to me with XSLT. Thanks

First of all, XSLT is for XML transformations and if you want to do schema validations you should be looking at XSD validations. Having said that below is how you can use XSLT to validate a message and generate a payload.
Let's say you have a Payload like the below.
<Request>
<messages>
<message>
<id>123</id>
<name>Not Empty</name>
</message>
<message>
<id>234</id>
<name></name>
</message>
</messages>
</Request>
In the above payload, you want to check each message and check whether the name is empty or not. Inorder to validate this you can use XSLT. First create a XSLT as a LocalEntry in the local-entries directory. You can use the following content. (You can create your XSLT in the registry as well)
<?xml version="1.0" encoding="UTF-8"?>
<localEntry key="LOCAL_XSLT_NullCheck" xmlns="http://ws.apache.org/ns/synapse">
<xsl:stylesheet version="1.0" 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="/">
<Response>
<!-- Iterating through message elements -->
<xsl:for-each select="//messages/message">
<message>
<messageID><xsl:value-of select="id/text()"/></messageID>
<xsl:choose>
<!-- Check if name is empty -->
<xsl:when test="boolean(name/text())">
<isEmpty>false</isEmpty>
</xsl:when>
<xsl:otherwise>
<isEmpty>true</isEmpty>
</xsl:otherwise>
</xsl:choose>
</message>
</xsl:for-each>
</Response>
</xsl:template>
</xsl:stylesheet>
</localEntry>
Next create a API to consume your request and to validate the payload and then to generate the response.
<?xml version="1.0" encoding="UTF-8"?>
<api context="/xslt" name="TestAPI" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST">
<inSequence>
<log level="full">
<property name="Message" value="Incoming Message"/>
</log>
<xslt key="LOCAL_XSLT_NullCheck"/>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
Now once you invoke the API with the aforementioned message. You should see the following response.
<Response xmlns="http://ws.apache.org/ns/synapse">
<message>
<messageID>123</messageID>
<isEmpty>false</isEmpty>
</message>
<message>
<messageID>234</messageID>
<isEmpty>true</isEmpty>
</message>
</Response>
You ca read more on XSLT mediator from here. You can also consider using FastXSLT Mediator as well based on your requirement. Read more on XSLT from here.

Related

XPATH to get distinct set of nodes from XML

I have an XML file as shown below.
<?xml version="1.0" encoding="UTF-8"?>
<body xmlns:bons0="http://DistinctAppSample" xmlns:httpsca="http://www.ibm.com/xmlns/prod/websphere/http/sca/6.1.0" xmlns:mq="http://www.ibm.com/xmlns/prod/websphere/mq/sca/6.0.0" xmlns:smo="http://www.ibm.com/websphere/sibx/smo/v6.0.1" xmlns:tns="http://DistinctAppSample/TestInf" xmlns:tns1="http://www.w3.org/2005/08/addressing" xmlns:tns2="http://www.w3.org/2003/05/soap-envelope" xmlns:tns_1="wsdl.http://DistinctAppSample/TestInf" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="foo.xsd">
<tns:testOperation>
<input>
<request>
<salary>100</salary>
<newSalary>101</newSalary>
</request>
<request>
<salary>123</salary>
<newSalary>101</newSalary>
</request>
<request>
<salary>101</salary>
<newSalary>123</newSalary>
</request>
<request>
<salary>100</salary>
<newSalary>101</newSalary>
</request>
</input>
</tns:testOperation>
</body>
I am trying to remove the duplicate "request" node where we have same salary and newSalary value (node1 and node4 has same values. So I need to consider only one node). Sample output is follows.
<?xml version="1.0" encoding="UTF-8"?>
<body xmlns:bons0="http://DistinctAppSample" xmlns:httpsca="http://www.ibm.com/xmlns/prod/websphere/http/sca/6.1.0" xmlns:mq="http://www.ibm.com/xmlns/prod/websphere/mq/sca/6.0.0" xmlns:smo="http://www.ibm.com/websphere/sibx/smo/v6.0.1" xmlns:tns="http://DistinctAppSample/TestInf" xmlns:tns1="http://www.w3.org/2005/08/addressing" xmlns:tns2="http://www.w3.org/2003/05/soap-envelope" xmlns:tns_1="wsdl.http://DistinctAppSample/TestInf" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="foo.xsd">
<tns:testOperation>
<input>
<request>
<salary>100</salary>
<newSalary>101</newSalary>
</request>
<request>
<salary>123</salary>
<newSalary>101</newSalary>
</request>
<request>
<salary>101</salary>
<newSalary>123</newSalary>
</request>
</input>
</tns:testOperation>
</body>
I would need to write a XPATH to achieve this Scenario.
I have tried //request[not(salary = preceding::salary and newSalary = preceding::newSalary)] but it is not working as expected. Any suggesition would be much appriciated.
Thanks.

SSRS - passing complex parameters to a webservice

I am trying to get data from a webservice to put in a SSRS report.
Here is a dataset that works with a simple string parameter (The parameter is set in dataset properties):
<Query xmlns="http://tempuri.org/">
<Method Name="ValidateUserWSToken" Namespace="http://tempuri.org/" />
<SoapAction>http://tempuri.org/WSTokenService/ValidateUserWSToken</SoapAction>
</Query>
Here is a similar method that expects a complextype for it's parameter
<Query>
<Method Name="GetUserWSToken" Namespace="http://tempuri.org/"/>
<SoapAction>http://tempuri.org/WSTokenService/GetUserWSToken</SoapAction>
</Query>
Putting the parameter in the dataset properties as
<UserName>username</UserName><Password>password</Password>
gives the error "There was an error trying to deserialize parameter http://tempuri.org/:request. The InnerException message was 'Error in line 5 position 20. Expected state 'Element'.. Encountered "text" with name '', namespace ''.'.". I don't think setting this in dataset properties will work.
I have tried variations on the following to try to put the parameter in the query text
<Query>
<Method Name="GetUserWSToken" Namespace="http://tempuri.org/" />
<SoapAction>http://tempuri.org/WSTokenService/GetUserWSToken</SoapAction>
<Parameters>
<Parameter Name="request" type="xml">
<DefaultValue xmlns:a="some namespace">
<a:Password>password</a:Password>
<a:UserName>username</a:UserName>
</DefaultValue>
</Parameter>
</Parameters>
</Query>
but these all fail with a 500 error - "Object reference not set to an instance of an object.", presumably because the webservice is not finding the parameter or finding it is null.
Here is the XML that SoapUI produces (and works)
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:civ="some namespace">
<soapenv:Header/>
<soapenv:Body>
<tem:GetUserWSToken>
<!--Optional:-->
<tem:request>
<civ:Password>password</civ:Password>
<civ:UserName>username</civ:UserName>
</tem:request>
</tem:GetUserWSToken>
</soapenv:Body>
</soapenv:Envelope>
Can this be converted into something I can use in SSRS query text?
This works:
<Query>
<Method Name="GetUserWSToken" Namespace="http://tempuri.org/">
<Parameters>
<Parameter Name="request" Type="XML" xmlns="some namespace">
<DefaultValue>
<Password>password</Password>
<UserName>username</UserName>
</DefaultValue>
</Parameter>
</Parameters>
</Method>
<SoapAction>http://tempuri.org/WSTokenService/GetUserWSToken</SoapAction>
<ElementPath IgnoreNamespaces="true">*</ElementPath>
</Query>
and this is what it is converted to:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetUserWSToken xmlns="http://tempuri.org/">
<request><Password xmlns="some namespace">Password123</Password><UserName xmlns="some namespace">FifeIntegrationUser</UserName></request>
</GetUserWSToken>
</soap:Body>
</soap:Envelope>

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

Why authorize purchase api returning numericString xml field

i am sending purchase request in xml with createcustomer profile.
customerProfileId is returning good but customerpayment profile id is returning in field customerPaymentProfileIdList > numericString.
here is full response.
<?xml version="1.0" encoding="utf-8"?>
<createTransactionResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
<messages>
<resultCode>Ok</resultCode>
<message>
<code>I00001</code>
<text>Successful.</text>
</message>
</messages>
<transactionResponse>
<responseCode>1</responseCode>
<authCode>R7BVRZ</authCode>
<avsResultCode>Y</avsResultCode>
<cvvResultCode>P</cvvResultCode>
<cavvResultCode>2</cavvResultCode>
<transId>60032752664</transId>
<refTransID />
<transHash>C1F3242645F7F118D709452E5758AF35</transHash>
<testRequest>0</testRequest>
<accountNumber>XXXX0015</accountNumber>
<accountType>MasterCard</accountType>
<messages>
<message>
<code>1</code>
<description>This transaction has been approved.</description>
</message>
</messages>
<transHashSha2 />
</transactionResponse>
<profileResponse>
<messages>
<resultCode>Ok</resultCode>
<message>
<code>I00001</code>
<text>Successful.</text>
</message>
</messages>
<customerProfileId>1813409569</customerProfileId>
<customerPaymentProfileIdList>
<numericString>1808086931</numericString>
</customerPaymentProfileIdList>
<customerShippingAddressIdList />
</profileResponse>
</createTransactionResponse>

WSO2 XSLT Mediator not resolving xpath

hopefully someone can point out the blindingly obvious to me - I want to perform a few simple translations on a soap payload before I send to a backend service - crucially, I need access to all the soap-headers delivered in the original payload.
My naive(?) thinking was to simply set the source attribute for the <xslt> mediator to "/" as Im aware by default it will start from the first child of the <body>, and I really need access to the headers.
WSO2 returns a "The evaluation of the XPath expression / did not result in an OMNode" error.
Are there one or more WSO2/xpath interworking quirks that Ive missed in the literature? Any pointers gratefully recieved, thanks
As Tishan mentioned you can use $header synapse xpath variable to access soap headers in message context. But it's bit tricky when it comes to xslt mediator. You cannot directly access message context values inside xslt stylesheet. But it is possible to pass these values as parameters and use in transformation. Let's see how we can achieve this.
Below is how account.xslt file looks like. Please note that there are two parameters called PARAM_SSN and PARAM_ACCT_NO used to assign value for <ssn></ssn> and <accountNumber></accountNumber>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="PARAM_SSN"></xsl:param>
<xsl:param name="PARAM_ACCT_NO"></xsl:param>
<xsl:template match="/">
<account xmlns="http://services.samples">
<ssn>
<xsl:value-of select="$PARAM_SSN"></xsl:value-of>
</ssn>
<accountNumber>
<xsl:value-of select="$PARAM_ACCT_NO"></xsl:value-of>
</accountNumber>
<accountHolder>
<xsl:value-of select="//name"></xsl:value-of>
</accountHolder>
</account>
</xsl:template>
</xsl:stylesheet>
Above file saved in registry of WSO2 ESB under /_system/governance/transform/account.xslt
Next is sample proxy which do the transformation with account.xslt
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="TransformExample"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<xslt key="gov:/transform/account.xslt">
<property name="PARAM_SSN" expression="$header/seccode"/>
<property name="PARAM_ACCT_NO" expression="$trp:acctNo"/>
</xslt>
<log level="custom">
<property name="Transformed Payload" expression="$body"/>
</log>
</inSequence>
<outSequence>
<send/>
</outSequence>
</target>
<description/>
</proxy>
Here you could see that inside <xslt> mediator, I am passing values for two parameters define in xslt by accessing message context. Value for PARAM_SSN taken from soap header and value for PARAM_ACCT_NO taken from transport header. This proxy service called by soapUI with below payload.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<seccode>987654321</seccode>
</soapenv:Header>
<soapenv:Body>
<request>
<name>Indika Sampath</name>
</request>
</soapenv:Body>
</soapenv:Envelope>
Also I am sending acctNo as transport header along with request. Once this hit the proxy, you could see transformed output log as below in console.
[2016-01-09 07:19:02,146] INFO - LogMediator Transformed Payload = <soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<account xmlns="http://services.samples"><ssn>987654321</ssn><accountNumber>123456789</accountNumber><accountHolder>Indika Sampath</accountHolder></account>
</soapenv:Body>
Hope this would resolve your problem.
Cheers!
You can access SOAP headers using Synapse Xpath Variable $header in your source xpath (Ex:$header/wsa:To). Hope this helps!!