I need to transform the xml object into soap object.
Below is the xml object I have. I converted it from JSON object:
<?xml version="1.0" encoding="UTF-8"?>
<requestXML>
<a1/>
<b1>2018</b1>
<c1>2019</c1>
<d1>1573</d1>
</requestXML>
Now I want to convert this xml object into soap object. Below is the desired output;
<soapenv:Envelope xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/ xmlns:sample=http://tempurl.com/>
<soapenv:Header/>
<soapenv:Body>
<sample:requestXML>
<a1></a1>
<b1>2018</b1>
<c1>2019</c1>
<d1>1573</d1>
</sample:requestXML>
</soapenv:Body>
</soapenv:Envelope>
However when i tried to do it with xslt, its giving me below object:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sample="http://tempurl.com/">
<soapenv:Header/>
<soapenv:Body>
<sample:requestXML>
<requestXML>
<a1/>
<b1>2018</b1>
<c1>2019</c1>
<d1>1573</d1>
</requestXML>
</sample:requestXML>
</soapenv:Body>
</soapenv:Envelope>
I used below xslt for transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" indent="yes" encoding="UTF-8" omit-xml-declaration="no"/>
<xsl:template match="/">
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sample="http://tempurl.com/">
<soapenv:Header/>
<soapenv:Body>
<sample:requestXML>
<xsl:copy>
<xsl:copy-of select="*"/>
<xsl:text disable-output-escaping="yes">
</xsl:text>
</xsl:copy>
</sample:requestXML>
</soapenv:Body>
</soapenv:Envelope>
</xsl:template>
</xsl:stylesheet>
@dchiesa1 : Could you please help or suggest what wrong I am putting in xslt file. I am not able to remove the root element.
Solved! Go to Solution.
To meet your requirements, I think you can use this XSL
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" indent="yes" encoding="UTF-8"
omit-xml-declaration="yes"
xslt:indent-amount="2"
xmlns:xslt="http://xml.apache.org/xslt" />
<xsl:template match="/">
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sample="http://tempurl.com/">
<soapenv:Header/>
<soapenv:Body>
<sample:requestXML>
<xsl:copy>
<xsl:copy-of select="requestXML/*"/>
</xsl:copy>
</sample:requestXML>
</soapenv:Body>
</soapenv:Envelope>
</xsl:template>
</xsl:stylesheet>
With this XSL, here is my output:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sample="http://tempurl.com/">
<soapenv:Header/>
<soapenv:Body>
<sample:requestXML>
<a1/>
<b1>2018</b1>
<c1>2019</c1>
<d1>1573</d1>
</sample:requestXML>
</soapenv:Body>
</soapenv:Envelope>
But I want to make some comments on this output. It's unusual to have a namespace-qualified element as a child of soap:Body, and then have namespace-unqualified child elements of THAT. Specific to your case, sample:requestXML is namespace-qualified. Elements a1, b1, c1 and so on are unqualified. That's unusual. To get them to be namespace qualified you'd need to use a different XSL. Something like this:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sample="http://tempurl.com/">
<xsl:output method="xml" version="1.0" indent="yes" encoding="UTF-8"
omit-xml-declaration="yes"
xslt:indent-amount="2"
xmlns:xslt="http://xml.apache.org/xslt" />
<xsl:param name="newUri" select="'http://tempurl.com/'"/>
<xsl:template match="/">
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sample="http://tempurl.com/">
<soapenv:Header/>
<soapenv:Body>
<sample:requestXML>
<xsl:apply-templates select="requestXML/*"/>
</sample:requestXML>
</soapenv:Body>
</soapenv:Envelope>
</xsl:template>
<xsl:template match="*" >
<xsl:element name="sample:{local-name()}" namespace="{$newUri}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
And the result of that XSL is this:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sample="http://tempurl.com/">
<soapenv:Header/>
<soapenv:Body>
<sample:requestXML>
<sample:a1/>
<sample:b1>2018</sample:b1>
<sample:c1>2019</sample:c1>
<sample:d1>1573</sample:d1>
</sample:requestXML>
</soapenv:Body>
</soapenv:Envelope>
To meet your requirements, I think you can use this XSL
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" indent="yes" encoding="UTF-8"
omit-xml-declaration="yes"
xslt:indent-amount="2"
xmlns:xslt="http://xml.apache.org/xslt" />
<xsl:template match="/">
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sample="http://tempurl.com/">
<soapenv:Header/>
<soapenv:Body>
<sample:requestXML>
<xsl:copy>
<xsl:copy-of select="requestXML/*"/>
</xsl:copy>
</sample:requestXML>
</soapenv:Body>
</soapenv:Envelope>
</xsl:template>
</xsl:stylesheet>
With this XSL, here is my output:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sample="http://tempurl.com/">
<soapenv:Header/>
<soapenv:Body>
<sample:requestXML>
<a1/>
<b1>2018</b1>
<c1>2019</c1>
<d1>1573</d1>
</sample:requestXML>
</soapenv:Body>
</soapenv:Envelope>
But I want to make some comments on this output. It's unusual to have a namespace-qualified element as a child of soap:Body, and then have namespace-unqualified child elements of THAT. Specific to your case, sample:requestXML is namespace-qualified. Elements a1, b1, c1 and so on are unqualified. That's unusual. To get them to be namespace qualified you'd need to use a different XSL. Something like this:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sample="http://tempurl.com/">
<xsl:output method="xml" version="1.0" indent="yes" encoding="UTF-8"
omit-xml-declaration="yes"
xslt:indent-amount="2"
xmlns:xslt="http://xml.apache.org/xslt" />
<xsl:param name="newUri" select="'http://tempurl.com/'"/>
<xsl:template match="/">
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sample="http://tempurl.com/">
<soapenv:Header/>
<soapenv:Body>
<sample:requestXML>
<xsl:apply-templates select="requestXML/*"/>
</sample:requestXML>
</soapenv:Body>
</soapenv:Envelope>
</xsl:template>
<xsl:template match="*" >
<xsl:element name="sample:{local-name()}" namespace="{$newUri}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
And the result of that XSL is this:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sample="http://tempurl.com/">
<soapenv:Header/>
<soapenv:Body>
<sample:requestXML>
<sample:a1/>
<sample:b1>2018</sample:b1>
<sample:c1>2019</sample:c1>
<sample:d1>1573</sample:d1>
</sample:requestXML>
</soapenv:Body>
</soapenv:Envelope>
Thanks @dchiesa1 . I understand the solution. Thanks a lot.