XSLT transformation of soap - xslt

I have this xml file
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tes="http://testwork/">
<soapenv:Header/>
<soapenv:Body>
<tes:sayHelloWorldFrom>
<!--Optional:-->
<arg0>?</arg0>
</tes:sayHelloWorldFrom>
</soapenv:Body>
</soapenv:Envelope>
I want to extract body using xslt transformation, my xsl is
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
xmlns:m="http://www.example.org/stock">
<xsl:template match="/">
<xsl:apply-templates select="soapenv:Envelope/soapenv:Body/*"/>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
But during the transformation there appears error
Unable to perform XSL transformation on your XML file. null
What is wrong with my xsl?

Your XSL is missing the namespaces that are in your XML. Without it, your XSL can't find your elements in the XML because it would be looking in the wrong namespace for it.
So add
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tes="http://testwork/"
To your XSL and it should transform without issue.

Related

Input-Output transformation remove nodes and update nodes

How to translate the given input xml into the output as shown below.
The input xml should be transformed by removing the wsse:Security header completely and EventUser value must be updated with the Username node value.
I tried below xlst. It is not working
XLST -
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="EventUser">
<xsl:copy>
<xsl:value-of select="Username"/>
</xsl:copy>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Input -
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<wsse:Security mustUnderstandValue="1"
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken id="UsernameToken-274">
<wsse:Username>MyUsername</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">Password</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<axis2ns682:Create xmlns:axis2ns682="http://g22.test01.org/test02/RF">
<tns:CreateDetails xmlns:tns="http://g22.test01.org/test02/RF">
<tns:EventUser>Event</tns:EventUser>
<tns:PreApprovedTemplateID>221398</tns:PreApprovedTemplateID>
<tns:Priority>High</tns:Priority>
<tns:PlannedImplementation_Start>2017-09-29</tns:PlannedImplementation_Start>
</tns:CreateDetails>
</axis2ns682:Create>
</soapenv:Body>
</soapenv:Envelope>
Output -
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
</soapenv:Header>
<soapenv:Body>
<axis2ns682:Create xmlns:axis2ns682="http://g22.test01.org/test02/RF">
<tns:CreateDetails xmlns:tns="http://g22.test01.org/test02/RF">
<tns:EventUser>MyUsername</tns:EventUser>
<tns:PreApprovedTemplateID>221398</tns:PreApprovedTemplateID>
<tns:Priority>High</tns:Priority>
<tns:PlannedImplementation_Start>2017-09-29</tns:PlannedImplementation_Start>
</tns:CreateDetails>
</axis2ns682:Create>
</soapenv:Body>
</soapenv:Envelope>
You have three problems with your XSLT
You have not accounted for namespaces in your XSLT. In your XML, the elements are in various namespaces (the EventUser element is in namespace http://g22.test01.org/test02/RF as denoted by the tns: prefix.
You have not coded anything to remove the wsse:Security element
The template matching EventUser is selecting Username, which would only would if Username was a child of EventUser. You actually need to select it from the soap:Header
Try this XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tns="http://g22.test01.org/test02/RF"
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
version="1.0">
<xsl:template match="wsse:Security" />
<xsl:template match="tns:EventUser">
<xsl:copy>
<xsl:value-of select="//soapenv:Header/wsse:Security/wsse:UsernameToken/wsse:Username"/>
</xsl:copy>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

wso2 xslt mediator with namespaces

we want to use an xslt mediator to transform a xml in other.
we have this soap message.
<?xml version = "1.0" encoding="ISO-8859-1"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.es">
<soapenv:Header/>
<soapenv:Body>
<ws:reception>
<ws:xml>
<message>Data messsage to send</message>
</ws:xml>
</ws:reception>
</soapenv:Body>
</soapenv:Envelope>
And we want this message as result.
<?xml version = "1.0" encoding="ISO-8859-1"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.es">
<soapenv:Header/>
<soapenv:Body>
<ws:reception>
<ws:xml>
<![CDATA[<message>Data messsage to send]]></message>
</ws:xml>
</ws:reception>
</soapenv:Body>
</soapenv:Envelope>
we are using this xslt template
<?xml version = "1.0" encoding = "ISO-8859-1"?>
<xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "//ws:xml">
<xsl:copy>
<xsl:text disable-output-escaping="yes"> <![CDATA[</xsl:text>
<xsl:copy-of select="*"/>
<xsl:text disable-output-escaping="yes"> ]]></xsl:text>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
But it doesn't work.
can anyone help us??
Thanks in advance.
Your two files are identical. But if I'm right in thinking you want to change
<message>Data messsage to send</message>
into
<message><![CDATA[Data messsage to send]]></message>
if so try the below:
(code has been edited to reflect updated question)
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="message">
<xsl:text disable-output-escaping="yes"> <![CDATA[</xsl:text>
<message>
<xsl:value-of select="//message"/>
</message>
<xsl:text disable-output-escaping="yes"> ]]></xsl:text>
</xsl:template>
Be warned though, Michael Kay will shout at you for using disable-output-escaping

Modify the content inside the SOAP envolope body using XSLT

I have a scenario, where i have to only alter the value(serverURL) of one attribute inside the SOAP body and leave the rest intact.
I am not able to find a solution for it. please help.
XML file is
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<loginResponse>
<result>
<metadata>xxx</metadata>
<passwordExpired>xx</passwordExpired>
<sandbox>xx</sandbox>
<serverUrl>xx</serverUrl>
<userId>xx</userId>
<userInfo>
<accessibilityMode>xx</accessibilityMode>
</userInfo>
</result>
</loginResponse>
</soapenv:Body>
</soapenv:Envelope>
XSLT File is
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" >
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="*|#*|text()">
<xsl:copy>
<xsl:apply-templates select="*|#*|text()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="serverUrl">
<serverUrl>https://Sahoo/Nrusingha</serverUrl>
</xsl:template>
</xsl:stylesheet>

adding SOAP header in SOAP envelope using XSLT

i have an incoming SOAP message where there were no SOAP header tag present i need to add SOAP header in that. I am using this XSLT for doing this but SOAP HEADER is not getting added.
XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dp="http://www.datapower.com/extensions"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
extension-element-prefixes="dp"
exclude-result-prefixes="dp" >
<xsl:variable name="uuid" select="dp:variable('var://context/txn/uuid')" />
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="soapenv:Header">
<soapenv:Header>
<UUID><xsl:value-of select="$uuid"/></UUID>
</soapenv:Header>
</xsl:template>
</xsl:stylesheet>
The incomig SOAP mesagge can have header tag or it might not have header tag
Input SOAP Message Without Header tag:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<hmhs-medical-acknowledgement id="4080186649" result="FAIL""/>
</soapenv:Body>
</soapenv:Envelope>
Input SOAP Message With Header tag:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<hmhs-medical-acknowledgement id="4080186649" result="FAIL""/>
</soapenv:Body>
</soapenv:Envelope>
Required Output SOAP Message:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<UUID>71A4B2FA029D412787B06E07685ED101</UUID>
</soapenv:Header>
<soapenv:Body>
<hmhs-medical-acknowledgement id="4080186649" result="FAIL""/>
</soapenv:Body>
</soapenv:Envelope>
Can somebody help how to add the SOAP header in that.
AFAICT, you want to do:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dp="http://www.datapower.com/extensions"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
extension-element-prefixes="dp">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="uuid" select="dp:variable('var://context/txn/uuid')" />
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/soapenv:Envelope">
<xsl:copy>
<soapenv:Header>
<UUID><xsl:value-of select="$uuid"/></UUID>
</soapenv:Header>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="soapenv:Header"/>
</xsl:stylesheet>

how to select value wierd soap - xml

i'm new to xsl and i want to know how i can select value of the field r1
this is my xml (not a 100% soap)
<Result>
<send>
<x>1</x>
</send>
<received>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns1:Consult xmlns:ns1="http://www.example.org/New/">
<message>
<r1>2</r1>
</message>
</ns1:Consult>
</soapenv:Body>
</soapenv:Envelope>
</received>
</Result>
and i tried this
<xsl:value-of select="/Result/received/soap:Envelope/soap:Body[1]/*[namespace-uri()='http://www.example.org/New/' and local-name()='Consult'][1]/message/r1"/>
but it doesn't work
You need to register the SOAP namespace:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<xsl:template match="/">
<xsl:value-of
select="/Result/received/soap:Envelope/soap:Body[1]/*[
namespace-uri()='http://www.example.org/New/'
and local-name()='Consult'][1]/message/r1" />
</xsl:template>
</xsl:stylesheet>
Better yet, register them both:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://www.example.org/New/>
<xsl:template match="/">
<xsl:value-of
select="/Result/received/soap:Envelope/
soap:Body[1]/ns1:Consult[1]/message/r1" />
</xsl:template>
</xsl:stylesheet>