Copy attributes from element in xsl - xslt

I have a soap request in context variable, that is a response too:
<soap:Envelope>
<xsl:copy-of select="dp:variable('var://context/Requestdata/STRequest')"/>
</soap:Envelope>
Now, I want that the response will be one new element, with attributes of a specific element from the context variable:
<soap:Envelope>
<NewElement att1="xxx" att2="yyy" att3="zzz"/>
</soap:Envelope>
How can I do this?

Related

Importing the wsdl file in postman

I am trying to access the client URL web service in postman. I used the import link to import the wsdl file client URL in postman and successfully generated the collection. However, upon testing the request, I am unable to retrieve the data from the client server. I am only getting an empty element in the response body. I followed some of the SOAP Web Service tutorial on youtube and it is still the same. The client provided me the valid username for me to test but still the same.
This is my request body:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<zhi_sa_mgt xmlns="urn:mss:jfnllns:df">
<P_DATE>2022-10-22</P_DATE>
<P_NAME>
<item>HPOITY</item>
</P_NAME>
</zhi_sa_mgt>
</soap:Body>
</soap:Envelope>
And the response body:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<zhi_sa_mgt xmlns="urn:mss:jfnllns:df">
<P_DATA/>
</zhi_sa_mgt>
</soap:Body>
</soap:Envelope>
I just wanted to ask, what could be the possible reason/s on why I am getting the empty tag <P_DATA>? This is my first SOAP web service task and I cannot figure out why I cant retrieve the data from the client server. Thank you in advance for your response.

WSO2 Proxy service XML Xpath

Im using wso2 EI 6.5.0
in my proxy service i need to get element between these 2 tags <aa></aa>
xpath //tem:Request is working but /tem:Request/xDoc/aa does not work
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:Request>
<xDoc>
<aa>
<bb>
....
</bb>
<cc>
<Parameter>
......
</Parameter>
</cc>
</aa>
</xDoc>
</tem:Request>
</soapenv:Body>
</soapenv:Envelope>
You have to use either /soapenv:Envelope/soapenv:Body/tem:Request/xDoc/aa or $body/tem:Request/xDoc/aa.
The purpose of using "//" in an xpath is to directly access a particular element. But if we use "/", we need to specify the entire path to traverse through the XML tags and reach a particular element.
On a different note, if you want to access the tag <aa/> then you can directly use the xpath //aa.

SOAP service crash with empty field in request

Our SOAP web service provider insist on removing empty field tags from the request because it breaks the service. Is that right practice?
See below example of request. StockID is empty tag. Should it break the SOAP service?
I would like to know best practice around empty tags in request
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Body xmlns:m="http://www.example.org/stock">
<m:GetStockPrice>
<m:StockName>IBM</m:StockName>
<m:StockID/>
</m:GetStockPrice>
</soap:Body>
</soap:Envelope>
This Depends on how the rest of the system is programmed. There are 3 ways of sending StockID:
Empty tag
Tag removed
Tag With nil="true"
What is probably happening for the first item above is that the program is deserializing the tag as an empty string, and then crashing since there is no Stock id = "".
In the 2 last items above it would deserialize as NULL and then not try and find a Stock id = NULL.

SOAP UI Automation - Assertion if reponse XML's dynamic elements value is blank

Please find below sample soap request
<FlowerSearchRequest>
<Request>
<name>ROSE</name>
<color></color>
<smell></smell>
</Request>
</FlowerSearchRequest>
Current Functionality
<Request>--<name> is mandatory field
<Request>--<color> and <Request>--<smell> is not mandatory i.e, they can be blank(<smell></smell>) but cannot be null
SOAP response <FlowerSearchResponse> contains all the request parameters <Request> and the response parameters for this request under <ResponseList> (as shown below)
For each <Request>, Response can have List of <Response> elements under <ResponseList>
If i search with ROSE , for few results <Response> -- <smell> element may/may not have value (but it cannot be null)
<Response> occurrence in result is dynamic , i.e, if I search with Lily I may get 10 or even more <Response> elements
SOAP response for above request
<FlowerSearchResponse>
<Request>
<name>ROSE</name>
<color></color>
<smell></smell>
</Request>
<ResponseList>
<Response>
<name>ROSE</name>
<color>Red</color>
<smell>Pleasant</smell>
</Response>
<Response>
<name>ROSE</name>
<color>Blue</color>
<smell>UnPleasant</smell>
</Response>
<Response>
<name>ROSE</name>
<color>Yellow</color>
<smell></smell>
</Response>
</ResponseList>
</FlowerSearchResponse>
Question
In SOAP UI, how to automate below case?
Go through all the <Response> elements from the dynamic list (not sure about the count of <Response> tags at the time of writing assertions), Assert if even a single <Response> has Blank value for <smell> element
Assertion test case should only consider elements <Response>-- <smell> when its checking for Blank <smell> elements i.e, it should not consider the <Request> -- <smell>
P.S: I am really novice at SOAP UI assertions-- trying hard to find a solution using xslt, but couldn't succeed so far.
If you want an XPath expression returning true or false depending on whether there is any Response/smell element with no content:
(: returns true if NO single one smell element is empty :)
empty(//Response/smell[empty(node())])
If you want to count them instead:
(: returns the number of empty smell elements :)
count(//Response/smell[empty(node())])

XSLT to get the attribute value from a SOAP Request

I have the following SOAP Request and i need to extract the IP Address parameter value inside the XSLT template.
SOAP REQUEST:
<soapenv:Envelope
xmlns:ws="http://diamondip.com/ipcontrol/ws/"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
<soapenv:Header />
<soapenv:Body>
<ws:deleteDevice soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<inpDev xsi:type="ser:WSDevice"
xmlns:ser="http://service.ipcontrol.diamondip.com"
>
<ipAddress xsi:type="soapenc:string"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
>xxx.xxx.xx.xx</ipAddress>
</inpDev>
</ws:deleteDevice>
</soapenv:Body>
</soapenv:Envelope>
I tried using the following to get the value,but it didn't work
<xsl:variable name="ipAddress" select="soapenv:Envelope/soapenv:Body/ws:deleteDevice/inpDev/ipAddress/text()"/>
Appreciate any advise!
The one thing we don't know from your question is what the current context node is when you do the xsl:variable. It should work if your current context node is the root node.
Try changing the XPath to an absolute path (put a forward slash at the beginning). If that doesn't work, make sure all the namespaces are defined correctly in your XSLT.
You can use below code to navigate and get the output.
<xsl:stylesheet version ="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match ='/'>
<xsl:variable name="ipAddress" select="/*[local-name() = 'Envelope']/*[local-name() = 'Body']/*[local-name() = 'deleteDevice']/*[local-name() = 'inpDev']/*[local-name() = 'ipAddress']/text()"/>
IP Adress: <xsl:value-of select = "$ipAddress"/>
</xsl:template>
</xsl:stylesheet>