JSON是否有等价的XSLT ?它允许我对JSON进行转换,就像XSLT对XML所做的那样。


当前回答

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.

CsvCruncher

尽管它的名字并不暗示它,CsvCruncher可以有效地用于读取表格* JSON数据,并使用SQL处理它们。

* Tabular表示JSON树的某些部分是规则的,即一个到多个相同或相似结构的条目。

CsvCruncher将树的这一部分转换为SQL表,然后您就有了完整的SQL语法来查询它。 您还可以加载多个JSON树,然后使用多个表。 您还可以将SQL结果存储为JSON(或CSV),然后再次处理它。

与真正复杂的XSLT方法相比,这种方法对我的帮助更大(尽管当您真正深入了解它时,它非常有趣)。

免责声明:我是CsvCruncher的开发者。

很久以前,我为我的基于jackson的json处理框架写了一个dom适配器。它用了。xom图书馆。生成的dom树可以使用java xpath和xslt工具。我做了一些非常简单的实现选择。例如,根节点总是被称为“root”,数组进入一个带有li子元素的ol节点(就像在html中一样),而其他一切都只是一个带有原始值或另一个对象节点的子节点。

JsonXmlConverter.java

用法: JsonObject sampleJson = sampleJson(); org.w3c.dom.Document domNode = JsonXmlConverter.getW3cDocument(sampleJson, “root”);

看看jsonpath-object-transform

现在有了!我最近创建了一个库,json-transforms,正是为了这个目的:

https://github.com/ColinEberhardt/json-transforms

它结合使用了JSPath(一种模仿XPath的DSL)和一种直接受到XSLT启发的递归模式匹配方法。

这里有一个简单的例子。给定以下JSON对象:

const json = {
  "automobiles": [
    { "maker": "Nissan", "model": "Teana", "year": 2011 },
    { "maker": "Honda", "model": "Jazz", "year": 2010 },
    { "maker": "Honda", "model": "Civic", "year": 2007 },
    { "maker": "Toyota", "model": "Yaris", "year": 2008 },
    { "maker": "Honda", "model": "Accord", "year": 2011 }
  ]
};

这是一个变换:

const jsont = require('json-transforms');
const rules = [
  jsont.pathRule(
    '.automobiles{.maker === "Honda"}', d => ({
      Honda: d.runner()
    })
  ),
  jsont.pathRule(
    '.{.maker}', d => ({
      model: d.match.model,
      year: d.match.year
    })
  ),
  jsont.identity
];

const transformed  = jsont.transform(json, rules);

输出如下:

{
  "Honda": [
    { "model": "Jazz", "year": 2010 },
    { "model": "Civic", "year": 2007 },
    { "model": "Accord", "year": 2011 }
  ]
}

这个转换由三个规则组成。第一个匹配本田制造的任何汽车,发出一个具有Honda属性的对象,然后递归匹配。第二条规则匹配任何具有maker属性的对象,输出模型和年份属性。最后一个是递归匹配的恒等变换。