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


当前回答

为什么不使用Mr. Data Coverter将JSON转换为XML,然后使用XSLT将其转换为JSON呢?

其他回答

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.

作为一个老问题的另一个新答案,我建议看看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实现的。

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.

我不太确定是否需要这样做,对我来说,缺乏工具意味着缺乏需求。JSON最好作为对象处理(无论如何在JS中是这样做的),并且您通常使用对象本身的语言来进行转换(Java用于从JSON创建的Java对象,同样用于Perl、Python、Perl、c#、PHP等)。只是普通的赋值(或set, get),循环等等。

我的意思是,XSLT只是另一种语言,需要它的一个原因是XML不是一种对象符号,因此编程语言的对象并不完全匹配(层次结构XML模型和对象/结构之间的阻抗)。