Remove ns1 prefix with XSLT for SoapEnvelope - xslt

Good afternoon,
I need to remove ns1 prefix with XSLT for SoapEnvelope
I have the following XML below:
<ns1:mt_BuscarOrdemServico_Request xmlns:ns1="http://www.supergasbras.com.br/service/BuscarOrdemServico">
<ordemServico>
<codigo>175811</codigo>
<codigoOrdemServico>7462242</codigoOrdemServico>
</ordemServico>
</ns1:mt_BuscarOrdemServico_Request>
I need to create a soap envelope to send my XML file to a webservice with the following format:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:esb="http://servicos.embratec.com.br/esb" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<esb:buscaOrdemServico>
<ordemServico xmlns:ns1="http://servicos.embratec.com.br/esb">
<codigo>11</codigo>
<codigoOrdemServico>74</codigoOrdemServico>
</ordemServico>
</esb:buscaOrdemServico>
</soapenv:Body>
</soapenv:Envelope
I am using the following XSLT code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:esb="http://servicos.embratec.com.br/esb">
<soapenv:Header>
</soapenv:Header>
<soapenv:Body>
<esb:buscaOrdemServico>
<xsl:copy-of select="*"/>
</esb:buscaOrdemServico>
</soapenv:Body>
</soapenv:Envelope>
</xsl:template>
</xsl:stylesheet>
But with this XSLT code I am getting the following result:
<soapenv:Envelope xmlns:esb="http://servicos.embratec.com.br/esb" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<esb:buscaOrdemServico>
<ns1:ordemServico xmlns:ns1="http://servicos.embratec.com.br/esb">
<codigo>11</codigo>
<codigoOrdemServico>74</codigoOrdemServico>
</ns1:ordemServico>
</esb:buscaOrdemServico>
</soapenv:Body>
</soapenv:Envelope
Can you help me to remove the prefix ns1?

You can get a result that is semantically identical to the one you want using:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:esb="http://servicos.embratec.com.br/esb">
<soapenv:Header/>
<soapenv:Body>
<esb:buscaOrdemServico>
<xsl:copy-of select="*/ordemServico"/>
</esb:buscaOrdemServico>
</soapenv:Body>
</soapenv:Envelope>
</xsl:template>
</xsl:stylesheet>
However, copying a node in XSLT 1.0 also copies the namespaces in scope for that node. Therefore, the result will include a redundant namespace declaration:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:esb="http://servicos.embratec.com.br/esb">
<soapenv:Header/>
<soapenv:Body>
<esb:buscaOrdemServico>
<ordemServico xmlns:ns1="http://www.supergasbras.com.br/service/BuscarOrdemServico">
<codigo>175811</codigo>
<codigoOrdemServico>7462242</codigoOrdemServico>
</ordemServico>
</esb:buscaOrdemServico>
</soapenv:Body>
</soapenv:Envelope>
This should not pose a problem for the receiving application. If it does, you can remove it by avoiding copying altogether - for example:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:esb="http://servicos.embratec.com.br/esb">
<soapenv:Header/>
<soapenv:Body>
<esb:buscaOrdemServico>
<ordemServico>
<codigo>
<xsl:value-of select="*/ordemServico/codigo"/>
</codigo>
<codigoOrdemServico>
<xsl:value-of select="*/ordemServico/codigoOrdemServico"/>
</codigoOrdemServico>
</ordemServico>
</esb:buscaOrdemServico>
</soapenv:Body>
</soapenv:Envelope>
</xsl:template>
</xsl:stylesheet>

Related

How to add SOAP Headers to soap message- XSLT

I'm getting the SOAP request with empty Headers. I want to add custom headers to the incoming request and copy the soap body as is to the response.How to add xml tags in the Headers ?
Below is the incoming request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<input>
<numb>15171</numb>
</input>
</soapenv:Body>
</soapenv:Envelope>
Expected Output:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<Username xmlns="http://blah.com">username</Username>
<Password xmlns="http://blah.com">password</Password>
</soapenv:Header>
<soapenv:Body>
<input>
<numb>15171</numb>
</input>
</soapenv:Body>
</soapenv:Envelope>
Here is my XSLT which I tried:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://soap/envelope/"
version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="soapenv:Header">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
<soapenv:Header>
<Username xmlns="http://blah.com">username</Username>
<Password xmlns="http://blah.com">password</Password>
</soapenv:Header>
</xsl:copy>
</xsl:template>
<xsl:template match="soapenv:Header"/>
</xsl:stylesheet>
Here's how you could do it.
Note that your 'soapenv' namespace in your XSLT was not matching what is defined in your input XML.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="soapenv:Header">
<xsl:copy>
<Username xmlns="http://blah.com">username</Username>
<Password xmlns="http://blah.com">password</Password>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
See it working here: https://xsltfiddle.liberty-development.net/6q1SDkU

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>

XSLT transformation of soap

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.

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>