How to convert JSON to XML with namespace?

Please suggest me how to convert the JSON object to XML

Expectation,

From JSON,

{     "Multiply": {         "intA": 5, "intB": 5     } } 

To XML,

<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body>     <Multiply xmlns="http://tempuri.org/">         <intA>5</intA>         <intB>5</intB>     </Multiply> </soap:Body> </soap:Envelope> 
Add Comment
1 Answer(s)

Consider XSLT 3.0 (available to Python users via Saxon/C).

<xsl:template name="xsl:initial-template" expand-text="yes">   <xsl:variable name="json" select="json-doc('my.json')"/>   <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                  xmlns:xsd="http://www.w3.org/2001/XMLSchema"                  xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">     <soap:Body>       <Multiply xmlns="http://tempuri.org/">         <intA>{$json?Multiply?IntA}</intA>         <intB>{$json?Multiply?IntB}</intB>       </Multiply>     </soap:Body>   </soap:Envelope> </xsl:template> 

The advantage of this approach, compared with off-the-shelf json-to-xml conversion tools, is that it gives you total control over the XML output. (And of course you can go in the other direction as well).

Answered on July 17, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.