[英]How to convert JSON to XML with namespace?
Please suggest me how to convert the JSON object to XML请建议我如何将 JSON object 转换为 XML
Expectation,期待,
From JSON,从 JSON,
{
"Multiply": {
"intA": 5, "intB": 5
}
}
To XML,至 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>
Consider XSLT 3.0 (available to Python users via Saxon/C).考虑 XSLT 3.0(通过 Saxon/C 提供给 Python 用户)。
<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.与现成的 json 到 xml 转换工具相比,这种方法的优势在于它可以让您完全控制 XML output。 (And of course you can go in the other direction as well).
(当然你也可以在另一个方向上使用 go)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.