如何使用 xslt 将 json 转换为 xml [英] How to convert json to xml using xslt

查看:49
本文介绍了如何使用 xslt 将 json 转换为 xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何将 JSON 转换为 XML?

How would you convert JSON to XML?

考虑:

<sampleTag>
{
  "Order": {
    "InvestmentAccount": { "AccountNumber": "10" },
    "Parcel": {      
      "Limit": "0",
      "ExpiryDate": "1900-01-01T00:00:00",
      "Asset": [
    {        
        "Open": "25.15",
        "High": "25.15",
        "Low": "25.11",
        "Close": "25.87"
      }
    {        
        "Open": "25.15",
        "High": "25.15",
        "Low": "25.11",
        "Close": "25.87"
      }]
    },

    "OrderDate": "2012-10-11T21:46:03.6489906+11:00",

  }
}
</sampleTag>

转换后的文档如下:

<Order>
    <InvestmentAccount>
        <AccountNumber>10</AccountNumber>
    </InvestmentAccount>
    <Parcel>
        <Limit>0</Limit>
        <ExpiryDate>1900-01-01T00:00:00</ExpiryDate>
        <Asset>
            <Open>25.15</Open>
            <High>25.15</High>
            <Low>25.11</Low>
            <Close>25.87</Close>
        </Asset>
        <Asset>
            <Open>25.15</Open>
            <High>25.15</High>
            <Low>25.11</Low>
            <Close>25.87</Close>
        </Asset>
    </Parcel>
    <OrderDate>2012-10-11T21:46:03.6489906+11:00</OrderDate>
</Order>

推荐答案

我在 JSON 解析方面的工作并未涵盖完整的 JSON 语法.

My work on JSON parsing doesn't cover the full JSON grammar.

将任何 JSON 文档翻译"为 XML 文档的任务没有解决方案.有一些 JSON 结构,如果不定义额外的约定和引入额外的元素,就无法将其转换为 XML——因此最终的 XML 结构不是原始 JSON 对象的真实自然表示.

And the task of "translating" any JSON document to an XML document doesn't have a solution. There are JSON constructs, which cannot be translated to XML without defining additional conventions and introducing additional elements -- so the final XML structure isn't a true and natural representation of the original JSON object.

在 XSLT 3.0 中有一个函数可以解析任何 JSON 对象——parse-json() -- 到 map -- XSLT 3.0 中引入的一种新数据类型.在这里阅读:http://www.w3.org/TR/xslt-30/#json

In XSLT 3.0 there is a function to parse any JSON object -- parse-json() -- to a map -- a new data type introduced in XSLT 3.0. Read about this here: http://www.w3.org/TR/xslt-30/#json

这篇关于如何使用 xslt 将 json 转换为 xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆