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


当前回答

使用XSLT转换JSON是非常可能的:您需要JSON2SAX反序列化器和SAX2JSON序列化器。

Java示例代码: http://www.gerixsoft.com/blog/json/xslt4json

其他回答

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

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

可以将XSLT与JSON结合使用。 XPath(3.1)、XSLT(3.0)和XQuery(3.1)的Verson 3在某种程度上支持JSON。 这似乎在Saxon的商业版本中可用,并且可能在某个时候包含在HE版本中。 https://www.saxonica.com/html/documentation/functions/fn/parse-json.html

-

我对另一种解决方案的期望是:

我想要能够输入JSON获取一组匹配的数据,并输出JSON或文本。

访问任意属性并计算值

支持条件逻辑

我希望转换脚本来自于工具的外部,基于文本,最好是一种常用的语言。

潜在的替代吗?

我想知道SQL是否是一个合适的选择。 https://learn.microsoft.com/en-us/sql/relational-databases/json/json-data-sql-server

如果替代工具能够处理JSON和XML就太好了 https://learn.microsoft.com/en-us/sql/relational-databases/xml/openxml-sql-server

我还没有尝试将我使用的XSLT脚本转换为SQL,也没有完全评估这个选项,但我希望很快就会研究它。目前只是一些想法。

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.

一种尚未给出的方法是使用解析器生成器在XSLT中创建解析器,解析JSON并生成XML输出。

在XML会议上被多次提及的一个选项是ReX解析器生成器(http://www.bottlecaps.de/rex/)——尽管在站点上完全没有文档记载,但是可以通过搜索找到菜谱。