JSON是否有等价的XSLT ?它允许我对JSON进行转换,就像XSLT对XML所做的那样。
当前回答
有关利用纯JavaScript的方法以及XSLT匹配表达式和递归模板背后熟悉的声明式模式的工作演示/概念证明,请参见https://gist.github.com/brettz9/0e661b3093764f496e36
(JSON也可以采用类似的方法。)
注意,为了方便在Firefox中表达模板,演示还依赖于JavaScript 1.8表达式闭包(至少在实现方法的ES6简写形式之前是如此)。
免责声明:这是我自己的代码。
其他回答
一种尚未给出的方法是使用解析器生成器在XSLT中创建解析器,解析JSON并生成XML输出。
在XML会议上被多次提及的一个选项是ReX解析器生成器(http://www.bottlecaps.de/rex/)——尽管在站点上完全没有文档记载,但是可以通过搜索找到菜谱。
我最近发现了一个我喜欢的JSON样式工具:https://github.com/twigkit/tempo。非常容易使用的工具——在我看来,使用它比使用XSLT容易得多——不需要XPATH查询。
JSONiq就是这样一个标准,而Zorba是一个开源的c++实现。JSONiq也可以看作是添加了JSON作为原生数据类型的XQuery。
I've been really tired of the enormous amount of JavaScript templating engines out there, and all their inline HTML-templates, different markup styles, etc., and decided to build a small library that enables XSLT formatting for JSON data structures. Not rocket science in any way -- it's just JSON parsed to XML and then formatted with a XSLT document. It's fast too, not as fast as JavaScript template engines in Chrome, but in most other browsers it's at least as fast as the JS engine alternative for larger data structures.
XSLT支持JSON,见http://www.w3.org/TR/xslt-30/#json
XML uses angular brackets for delimiter tokens, JSON uses braces, square brackets, ... I. e. XML's fewer token recognition comparisons means it's optimized for declarative transformation, whereas more comparisons, being like switch statement, for speed reasons assume speculative branch prediction that imperative code in scripting languages is useful for. As direct consequence, for different mixes of semi-structured data, you may want to benchmark XSLT and javascript engines' performance as part of responsive pages. For negligible data payload, transformations might work just as well with JSON without XML serialization. W3's decision ought to be based on better analysis.
推荐文章
- 查询JSON类型内的数组元素
- 将JSON字符串转换为HashMap
- 将JsonNode转换为POJO
- Json_encode()转义正斜杠
- c# vs Java Enum(适合c#新手)
- 如何写一个JSON文件在c# ?
- 如何在XSLT中实现if-else语句?
- 在序列化和反序列化期间JSON属性的不同名称
- 为什么PHP的json_encode函数转换UTF-8字符串为十六进制实体?
- Ajax会调用什么样的响应,比如'for (;;);{json data}的意思?
- 在JavaScript中将JSON字符串解析为特定对象原型
- 使用Jackson将JSON字符串转换为漂亮的打印JSON输出
- jQuery。由于转义了JSON中的单引号,parseJSON抛出“无效JSON”错误
- 用c#解析JSON
- XPath查找节点是否存在