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


当前回答

作为一个老问题的另一个新答案,我建议看看defantjs。它不是JSON的XSLT等价物,它是JSON的XSLT。文档的“模板”部分包括以下示例:

<!-- Defiant template -->
<script type="defiant/xsl-template">
    <xsl:template name="books_template">
        <xsl:for-each select="//movie">
            <xsl:value-of select="title"/><br/>
        </xsl:for-each>
    </xsl:template>
</script>

<script type="text/javascript">

var data = {
        "movie": [
            {"title": "The Usual Suspects"},
            {"title": "Pulp Fiction"},
            {"title": "Independence Day"}
        ]
    },
    htm = Defiant.render('books_template', data);

console.log(htm);
// The Usual Suspects<br>
// Pulp Fiction<br>
// Independence Day<br>

其他回答

JSLT非常接近于XSLT的JSON等价物。这是一种转换语言,您可以用JSON语法编写输出的固定部分,然后插入表达式来计算您想要插入到模板中的值。

一个例子:

{
  "time": round(parse-time(.published, "yyyy-MM-dd'T'HH:mm:ssX") * 1000),
  "device_manufacturer": .device.manufacturer,
  "device_model": .device.model,
  "language": .device.acceptLanguage
}

它是在Jackson之上用Java实现的。

试着震动。它是一个用Java编写的JSON到JSON转换库。

它是专门创建的,因为我们不想玩“JSON -> XML -> XSLT -> XML -> JSON”的游戏,而且对任何足够复杂的转换使用模板是不可维护的。

有趣的想法。在谷歌上的一些搜索产生了一些有趣的页面,包括:

如何实现这样一个“jsonT”工具的概述,以及一些下载 关于该实现的一些讨论 一个公司可能已经实施了一些合适的东西

希望这能有所帮助。

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.

JSONiq就是这样一个标准,而Zorba是一个开源的c++实现。JSONiq也可以看作是添加了JSON作为原生数据类型的XQuery。