繁体   English   中英

如何使用命名空间将 JSON 转换为 XML?

[英]How to convert JSON to XML with namespace?

请建议我如何将 JSON object 转换为 XML

期待,

从 JSON,

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

至 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>

考虑 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>

与现成的 json 到 xml 转换工具相比,这种方法的优势在于它可以让您完全控制 XML output。 (当然你也可以在另一个方向上使用 go)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM