We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 8 months ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(25)
JSON 的 XSLT 等效项 - 候选列表(工具和规范)
工具
1。 XSLT
您可以使用 XSLT for JSON 旨在 fn:json-to-xml。
2. jq
3. jj
4. fx
5. CsvCruncher
6. jl
7. JOLT
8. gron
9. json-e
10. JSLT
11. JSONata
12. JSONPath Plus
13. json-transforms 最后提交时间:2017 年 12 月 1 日
14. json 最后提交时间:2018 年 6 月 23 日
15. jsawk 最后提交时间:2015 年 3 月 4 日
16. yate 最后提交时间:2017 年 3 月 13 日
17. jsonpath-object-transform 最后提交时间:2017 年 1 月 18 日
18. 装订 最后提交时间:2013 年 9 月 16 日
19. mapneat
20。 DTL
规格:
XSLT equivalents for JSON - a list of candidates (tools and specs)
Tools
1. XSLT
You can use XSLT for JSON with the aim of fn:json-to-xml.
2. jq
3. jj
4. fx
5. CsvCruncher
6. jl
7. JOLT
8. gron
9. json-e
10. JSLT
11. JSONata
12. JSONPath Plus
13. json-transforms Last Commit Dec 1, 2017
14. json Last commit Jun 23, 2018
15. jsawk Last commit Mar 4, 2015
16. yate Last Commit Mar 13, 2017
17. jsonpath-object-transform Last Commit Jan 18, 2017
18. Stapling Last Commit Sep 16, 2013
19. mapneat
20. DTL
Specs:
有趣的想法。在 Google 上进行的一些搜索产生了一些有趣的页面,其中包括:
希望这会有所帮助。
Interesting idea. Some searching on Google produced a few pages of interest, including:
Hope this helps.
尝试JOLT。它是一个用 Java 编写的 JSON 到 JSON 转换库。
它是专门创建的,因为我们不想玩“JSON -> XML -> XSLT -> XML -> JSON”游戏,并且使用模板进行任何足够复杂的转换是无法维护的。
Try JOLT. It is a JSON to JSON transformation library written in Java.
It was created specifically because we did not want to play the "JSON -> XML -> XSLT -> XML -> JSON" game, and using a template for any sufficiently complex transform is unmaintainable.
XSLT 支持 JSON,如 http://www.w3.org/TR/xslt- 所示30/#json
XML 使用尖括号作为分隔符,JSON 使用大括号、方括号……即XML 较少的标记识别比较意味着它针对声明性转换进行了优化,而更多的比较(如 switch 语句)出于速度原因假设推测分支预测,而脚本语言中的命令式代码对此很有用。作为直接结果,对于半结构化数据的不同组合,您可能希望将 XSLT 和 JavaScript 引擎的性能作为响应式页面的一部分进行基准测试。对于可忽略不计的数据负载,转换可能与无需 XML 序列化的 JSON 一样有效。 W3的决定应该基于更好的分析。
XSLT supports JSON as seen at 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.
jq - 轻量级、灵活的命令行 JSON 处理器
它不像 XSLT 那样基于模板,但更简洁。例如,将
name
和address
字段提取到数组中:[.name, .address]
教程 演练了转换 Twitter 的 JSON API 的示例(以及 手册 有很多例子)。
jq - lightweight and flexible command-line JSON processor
It's not template-based like XSLT, but more concise. e.g. to extract
name
andaddress
fields into an array:[.name, .address]
The tutorial walks through an example of transforming Twitter's JSON API (and the manual has many examples).
我最近发现了一个我喜欢的 JSON 样式工具: https://github.com/twigkit/tempo。非常容易使用的工具——在我看来,它比 XSLT 更容易使用——不需要 XPATH 查询。
I recently found a tool that I love for styling JSON: https://github.com/twigkit/tempo. Very easy tool to use--in my opinion, it is much easier to work with than XSLT--no need for XPATH queries.
看看 jsonpath-object-transform
Have a look at jsonpath-object-transform
最近,我围绕此编写了自己的小型库,它试图尽可能接近
5.1处理模型(XSLT REC)
https://www.w3.org/TR/xslt#section-Processing-Model
尽可能地(无论如何我都可以),用几行 JavaScript 代码。
以下是一些不完全简单的使用示例...
1. JSON 到某些标记:
Fiddle: https://jsfiddle.net/YSharpLanguage/kj9pk8oz/10
(灵感来自D.1 文档示例 (XSLT REC) https://www.w3.org/TR/xslt#section-Document-Example)
其中:
... 给出:
和
2. JSON 到 JSON:
小提琴: https://jsfiddle.net/YSharpLanguage/ppfmmu15/10
其中:
... 给出:
3. XSLT 与 JavaScript:
JavaScript 等价于...
XSLT 3.0 REC 第 14.4 节示例:根据公共值对节点进行分组
(位于:http:// jsfiddle.net/YSharpLanguage/8bqcd0ey/1)
比照。 https://www.w3.org/TR/xslt-30/#grouping -examples
其中...
... 给出:
4. JSONiq 与 JavaScript:
相当于...
JSONiq 用例第 1.1.2 节。 JSON 分组查询
(位于:https://jsfiddle.net/YSharpLanguage/hvo24hmk/3< /a>)
参见。 http://jsoniq.org/docs/JSONiq-usecases/html -single/index.html#jsongrouping
其中...
...给出:
克服 JSONPath wrt 的限制也很有用。针对祖先轴进行查询,如这个SO问题提出的(当然还有其他人)。
例如,如何在知道其品牌 ID 的情况下获得杂货商品的折扣
?
一个可能的解决方案是:
...给出:
'HTH,
I wrote my own small library around this, recently, which tries to stay as close to
5.1 Processing Model (XSLT REC)
https://www.w3.org/TR/xslt#section-Processing-Model
as is possible (as I could anyway), in a few lines of JavaScript code.
Here are a few not-completely-trivial examples of use...
1. JSON-to-some-markup:
Fiddle: https://jsfiddle.net/YSharpLanguage/kj9pk8oz/10
(inspired by D.1 Document Example (XSLT REC) https://www.w3.org/TR/xslt#section-Document-Example)
where this:
... gives:
and
2. JSON-to-JSON:
Fiddle: https://jsfiddle.net/YSharpLanguage/ppfmmu15/10
where this:
... gives:
3. XSLT vs. JavaScript:
A JavaScript equivalent of...
XSLT 3.0 REC Section 14.4 Example: Grouping Nodes based on Common Values
(at: http://jsfiddle.net/YSharpLanguage/8bqcd0ey/1)
Cf. https://www.w3.org/TR/xslt-30/#grouping-examples
where...
... gives:
4. JSONiq vs. JavaScript:
A JavaScript equivalent of...
JSONiq Use Cases Section 1.1.2. Grouping Queries for JSON
(at: https://jsfiddle.net/YSharpLanguage/hvo24hmk/3)
Cf. http://jsoniq.org/docs/JSONiq-usecases/html-single/index.html#jsongrouping
where...
... gives:
It is also useful to overcome the limitations of JSONPath wrt. querying against the ancestor axis, as raised by this SO question (and certainly others).
E.g., how to get the discount of a grocery item knowing its brand id, in
?
A possible solution is:
... which gives:
'HTH,
如果说缺乏工具就意味着缺乏需求,这只是在回避问题。同样的情况也适用于 Linux 中对 X 或 Y 的支持(为什么要为如此少数的操作系统开发高质量的驱动程序和/或游戏?为什么要关注大型游戏和硬件公司不开发的操作系统?)。可能需要使用 XSLT 和 JSON 的人最终会使用一种有些微不足道的解决方法:将 JSON 转换为 XML。但这不是最佳解决方案,不是吗?
当您拥有本机 JSON 格式并且想要在浏览器中“所见即所得”编辑它时,XSLT 将是解决该问题的充分解决方案。使用传统的 JavaScript 编程来做到这一点可能会变得很痛苦。
事实上,我已经实现了 XSLT 的“石器时代”方法,使用子字符串解析来解释 javascript 的一些基本命令,例如调用模板、处理子级等。当然,使用 JSON 对象实现转换引擎比实现一个成熟的 XML 解析器来解析 XSLT。问题是,要使用 XML 模板转换 JSON 对象,您需要解析模板的 XML。
要使用 XML(或 HTML、文本或其他内容)转换 JSON 对象,您需要仔细考虑语法以及需要使用哪些特殊字符来标识转换命令。否则,您最终将不得不为您自己的自定义模板语言设计一个解析器。走过那条路之后,我可以告诉你,这条路并不美好。
更新(2010 年 11 月 12 日):经过几周的解析器工作,我已经能够对其进行优化。预先解析模板并将命令存储为 JSON 对象。转换规则也是 JSON 对象,而模板代码是 HTML 和类似于 shell 代码的自制语法的混合。我已经能够将复杂的 JSON 文档转换为 HTML 来制作文档编辑器。编辑器代码约为 1K 行(这是一个私人项目,所以我无法共享),JSON 转换代码约为 990 行(包括迭代命令、简单比较、模板调用、变量保存和评估)。我计划在麻省理工学院的许可下发布它。如果您想参与,请给我发邮件。
To say lack of tools suggest lack of need is just begging the question. The same could be applied to support for X or Y in Linux (Why bother developing quality drivers and/or games for such a minority OS? And why pay attention to an OS that big game and hardware companies don't develop for?). Probably the people who would need to use XSLT and JSON end up using a somewhat trivial workaround: Transforming JSON into XML. But that's not the optimal solution, is it?
When you have a native JSON format and you want to edit it "wysywyg" in the browser, XSLT would be a more than adequate solution for the problem. Doing that with traditional javascript programming can become a pain in the arse.
In fact, I have implemented a "stone-age" approach to XSLT, using substring parsing to interpret some basic commands for javascript, like calling a template, process children, etc. Certainly implementing a transformation engine with a JSON object is much easier than implementing a full-fledged XML parser to parse the XSLT. Problem is, that to use XML templates to transform a JSON object you need to parse the XML of the templates.
To tranform a JSON object with XML (or HTML, or text or whatever) you need to think carefully about the syntax and what special characters you need to use to identify the transformation commands. Otherwise you'll end up having to design a parser for your own custom templating language. Having walked through that path, I can tell you that it's not pretty.
Update (Nov 12, 2010): After a couple of weeks working on my parser, I've been able to optimize it. Templates are parsed beforehand and commands are stored as JSON objects. Transformation rules are also JSON objects, while the template code is a mix of HTML and a homebrew syntax similar to shell code. I've been able to transform a complex JSON document into HTML to make a document editor. The code is around 1K lines for the editor (it's for a private project so I can't share it) and around 990 lines for the JSON transformation code (includes iteration commands, simple comparisons, template calling, variable saving and evaluation). I plan to release it under a MIT license. Drop me a mail if you want to get involved.
作为老问题的另一个新答案,我建议看看 DefiantJS。它不是 JSON 的 XSLT 等价物,它是 JSON 的 XSLT。文档的“模板”部分包括以下示例:
As yet another new answer to an old question, I'd suggest a look at DefiantJS. It's not an XSLT equivalent for JSON, it is XSLT for JSON. The "Templating" section of the documentation includes this example:
现在有了!我最近创建了一个库,json-transforms,正是为了这个目的:
https:// /github.com/ColinEberhardt/json-transforms
它使用 DSL 的组合 JSPath以 XPath 为模型,以及直接受 XSLT 启发的递归模式匹配方法。
这是一个简单的例子。给定以下 JSON 对象:
这是一个转换:
输出以下内容:
该转换由三个规则组成。第一个匹配本田制造的任何汽车,发出具有本田属性的对象,然后递归匹配。第二条规则匹配具有
maker
属性的任何对象,输出model
和year
属性。最后是递归匹配的恒等变换。There is now! I recently created a library, json-transforms, exactly for this purpose:
https://github.com/ColinEberhardt/json-transforms
It uses a combination of JSPath, a DSL modelled on XPath, and a recursive pattern matching approach, inspired directly by XSLT.
Here's a quick example. Given the following JSON object:
Here's a transformation:
Which output the following:
This transform is composed of three rules. The first matches any automobile which is made by Honda, emitting an object with a
Honda
property, then recursively matching. The second rule matches any object with amaker
property, outputting themodel
andyear
properties. The final is the identity transform that recursively matches.我真的厌倦了大量的 JavaScript 模板引擎,以及它们所有的内联 HTML 模板、不同的标记样式等,并决定 构建一个小型库,支持 JSON 数据结构的 XSLT 格式。无论如何,这都不是火箭科学——它只是将 JSON 解析为 XML,然后使用 XSLT 文档进行格式化。它也很快,虽然不如 Chrome 中的 JavaScript 模板引擎快,但在大多数其他浏览器中,它至少与大型数据结构的 JS 引擎替代品一样快。
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.
我正在使用 Camel 路线 umashal(xmljson) ->到(xlst)->元帅(xmljson)。足够高效(虽然不是 100% 完美),但很简单,如果您已经在使用 Camel。
I am using Camel route umarshal(xmljson) -> to(xlst) -> marshal(xmljson). Efficient enough (though not 100% perfect), but simple, if you are already using Camel.
JSLT 非常接近 XSLT 的 JSON 等效项。它是一种转换语言,您可以使用 JSON 语法编写输出的固定部分,然后插入表达式来计算要插入模板中的值。
一个例子:
它是在 Jackson 之上用 Java 实现的。
JSLT is very close to a JSON equivalent of XSLT. It's a transform language where you write the fixed part of the output in JSON syntax, then insert expressions to compute the values you want to insert in the template.
An example:
It's implemented in Java on top of Jackson.
JSONiq 就是这样一个标准,Zorba 开源 C++ 实现。 JSONiq 也可以被视为添加 JSON 作为本机数据类型的 XQuery。
JSONiq is such a standard and Zorba an open-source C++ implementation. JSONiq can also be seen as XQuery with adding JSON as a native data type.
很可能使用 XSLT 转换 JSON:您需要 JSON2SAX 反序列化器和 SAX2JSON 序列化器。
Java 中的示例代码:
http://www.gerixsoft.com/blog/json/xslt4json
it is very possible to convert JSON using XSLT: you need JSON2SAX deserializer and SAX2JSON serializer.
Sample code in Java:
http://www.gerixsoft.com/blog/json/xslt4json
Yate (https://github.com/pasaran/yate) 是在 XSLT 之后专门设计的,具有 JPath 特性(JS 的自然 XPath 等效项),编译为 JavaScript,并且具有相当长的生产使用历史。它实际上没有记录,但阅读示例和测试应该足够了。
Yate (https://github.com/pasaran/yate) is specifically designed after XSLT, features JPath (a natural XPath equivalent for JS), compiles to JavaScript and has quite a history of production use. It’s practically undocumented, but reading through samples and tests should be enough.
我知道这是一篇旧帖子,但我刚刚遇到它,尽管我有偏见,因为我最初编写了该工具,但我认为我有最适合该问题的答案。
该语言、DTL 或数据转换语言是专门为转换 JSON(或任何结构化数据)而设计的。它的用途与 XSLT 非常相似,您可以在 npm 上以 dtl-js 形式或在网站 getdtl.org 上找到它其中包括一个实时浏览器内工具来探索它。
看起来像这样:
DTL 和 XSLT 在几个重要方面是相似的:
数据转换的目的:XSLT 和 DTL 主要用于转换数据。它们获取输入数据并将其转换为所需的输出格式。
支持复杂转换:两者都可以处理复杂的数据转换,允许用户以复杂的方式操作和重组数据。
嵌入性:两者都可以嵌入到它们转换的数据中。 DTL 可以嵌入到 JSON 结构中,就像 XML 文件中的 XSLT 一样。
基于规则的逻辑:它们根据定义的规则或模式进行操作 - XSLT 对 XML 元素使用模板规则,而 DTL 对 JSON 元素使用转换表达式。
模式匹配:XSLT 使用 XPath 来选择 XML 节点,类似地,DTL 使用表达式根据特定条件选择和操作 JSON 元素。
声明性:两种语言都是声明性的,重点关注转换应该实现什么而不是如何完成。
迭代和条件处理:它们支持条件逻辑和迭代 - XSLT,具有
xsl:for-each
和xsl:if
等结构,以及带有map
和grep
等帮助器的 DTL。可扩展性:两者都可以通过附加功能进行扩展,例如 XSLT 中的自定义函数和 DTL 中的自定义帮助程序。
转换上下文感知:每个都有一个上下文感知转换过程,这对于将正确的转换逻辑应用于数据至关重要。
数据不可知逻辑:两者中的转换逻辑都是数据不可知的,允许其应用于符合预期结构的不同数据集。
简而言之,DTL 是为 JSON 量身定制的,专门设计用于对 JSON 执行与 XSLT 对 XML 执行的完全相同的工作。
再说一次,我有偏见,但 DTL 与 XSLT 不同的一些重要方面:
更容易理解:
DTL 转换结构通常反映所需的输出结构,并且语法更易于理解和阅读。 (您可以查看快速入门,它告诉您需要了解的所有信息。 )
数据格式灵活性:
虽然 DTL 主要是为 JSON 设计的,但它也可以处理 YAML、CSV,甚至纯文本数据。这种多功能性使得 DTL 更适合 XML 之外的各种数据格式。
dtl
命令行工具可使用多种格式进行批量数据处理。与现代网络技术集成:
DTL 与 JavaScript 和现代 Web 开发实践无缝集成,使其非常适合主要使用 JSON 的 Web 应用程序。它运行在服务器上、浏览器中,甚至直接用于 MongoDB 等 NoSQL 数据库。
易于调试:
DTL 更简单的语法和结构可以使调试更容易,尤其是在 JavaScript 环境中使用时。 网站上的交互式工具和
dtlr
命令行 REPL/数据浏览器让一切变得简单弄清楚你需要做什么。可读性和维护:
DTL 简单的语法和类似 JSON 的结构可以使 DTL 转换更具可读性和可维护性。
强大的编辑器支持:
DTL 具有强大的编辑器支持,为 vs-code、sublime-text 和 vim 中的所有内置功能提供语法突出显示和片段。
再说一次,我最初写它是因为我有偏见,但我写它是因为这个问题没有一个好的答案。它很稳定且易于维护,自 2013 年以来已被许多公司投入生产使用,而且它的功能非常出色。
希望有帮助。
I know this is an old post, but I just came across it and, though I'm biased because I wrote the tool originally, I think I have the answer that most fits the question.
The language, DTL, or Data Transformation Language is specifically designed for transforming JSON (or any structured data). It is very similar in purpose to XSLT You can find it on npm as dtl-js or on the website getdtl.org which includes a live in-browser tool to explore it.
It looks like this:
DTL and XSLT are alike in several important ways:
Purpose for Data Transformation: Both XSLT and DTL are primarily used for transforming data. They take input data and transform it into a desired output format.
Support for Complex Transformations: Both can handle complex data transformations, allowing users to manipulate and restructure data in sophisticated ways.
Embeddability: Both can be embedded within the data they transform. DTL can be embedded within JSON structures, much like XSLT within XML files.
Rule-Based Logic: They operate based on defined rules or patterns - XSLT uses template rules for XML elements, whereas DTL uses transformation expressions for JSON elements.
Pattern Matching: XSLT utilizes XPath for selecting XML nodes, and similarly, DTL employs expressions to select and manipulate JSON elements based on certain criteria.
Declarative Nature: Both languages are declarative, focusing on what the transformation should achieve rather than how it should be done.
Iterative and Conditional Processing: They support conditional logic and iteration - XSLT with constructs like
xsl:for-each
andxsl:if
, and DTL with helpers likemap
andgrep
.Extensibility: Both can be extended with additional functionalities, like custom functions in XSLT and custom helpers in DTL.
Transformation Context Awareness: Each has a context-aware transformation process, essential for applying the correct transformation logic to the data.
Data Agnostic Logic: The transformation logic in both is data-agnostic, allowing it to be applied to different sets of data that conform to the expected structure.
In short, DTL is tailored for JSON and is specifically designed to do exactly the same job for JSON that XSLT does for XML.
Again, I'm biased, but some important ways DTL is different from XSLT:
Easier to Understand:
DTL transform structure usually mirrors the desired output structure and the syntax is much simpler to understand and read. (You can look at the quickstart which tells you everything you need to know.)
Data Format Flexibility:
While DTL is primarily designed for JSON, it can also handle YAML, CSV, and even plain text data. This versatility makes DTL more suitable for a variety of data formats beyond XML. The
dtl
command line tool works with multiple formats for bulk data processing.Integration with Modern Web Technologies:
DTL integrates seamlessly with JavaScript and modern web development practices, making it a natural fit for web applications that predominantly use JSON. It runs on the server, in the browser, and has even been used directly in NoSQL databases like MongoDB.
Ease of Debugging:
The simpler syntax and structure of DTL can make debugging easier, especially when used within JavaScript environments. The interactive tool on the website and the
dtlr
command line REPL / data explorer make it easy to figure out what you need to do.Readability and Maintenance:
DTL's straightforward syntax and JSON-like structure can make DTL transformations more readable and maintainable.
Strong editor support:
DTL has strong editor support, providing syntax highlighting and snippets for all built-in functionality in vs-code, sublime-text and vim.
Again, I wrote it originally so I'm biased, but I wrote it because there wasn't a good answer for this question. It's stable and maintained and it's been in production use since 2013 by many companies and it's very good at what it does.
Hope it helps.
为什么不使用 Mr. 将 JSON 转换为 XML Data Coverter,使用 XSLT 将其转换,然后使用相同的方法将其更改回 JSON。
Why don't you converts JSON to XML using Mr. Data Coverter , tranform it using XSLT and then change it back to JSON using the same.
有关利用纯 JavaScript 以及 XSLT 匹配表达式和递归模板背后熟悉的声明性模式的方法的工作涂鸦/概念证明,请参阅 https://gist.github.com/brettz9/0e661b3093764f496e36
(对于 JSON 可能采取类似的方法。)
请注意,该演示还依赖于 JavaScript 1.8 表达式闭包,以便于表达模板在 Firefox 中(至少在方法的 ES6 缩写形式可能实现之前)。
免责声明:这是我自己的代码。
For a working doodle/proof of concept of an approach to utilize pure JavaScript along with the familiar and declarative pattern behind XSLT's matching expressions and recursive templates, see https://gist.github.com/brettz9/0e661b3093764f496e36
(A similar approach might be taken for JSON.)
Note that the demo also relies on JavaScript 1.8 expression closures for convenience in expressing templates in Firefox (at least until the ES6 short form for methods may be implemented).
Disclaimer: This is my own code.
很久以前,我为我的基于 Jackson 的 json 处理框架编写了一个 dom 适配器。它使用 nu.xom 库。生成的 dom 树与 java xpath 和 xslt 工具一起使用。我做出了一些非常简单的实现选择。例如,根节点始终称为“root”,数组进入带有 li 子元素的 ol 节点(如在 html 中),其他所有内容都只是具有原始值的子节点或另一个对象节点。
JsonXmlConverter.java< /a>
用法:
<代码>
JsonObject 样本Json = 样本Json();
org.w3c.dom.Document domNode = JsonXmlConverter.getW3cDocument(sampleJson, "root");
I wrote a dom adapter for my jackson based json processing framework long time ago. It uses the nu.xom library. The resulting dom tree works with the java xpath and xslt facilities. I made some implementation choices that are pretty straightforward. For example the root node is always called "root", arrays go into an ol node with li sub elements (like in html), and everything else is just a sub node with a primitive value or another object node.
JsonXmlConverter.java
Usage:
JsonObject sampleJson = sampleJson();
org.w3c.dom.Document domNode = JsonXmlConverter.getW3cDocument(sampleJson, "root");
尚未给出的一种方法是使用解析器生成器在 XSLT 中创建解析器,该解析器解析 JSON 并生成 XML 输出。
在 XML 会议上经常提到的一个选项是 ReX 解析器生成器 (http://www.bottlecaps. de/rex/) - 尽管网站上完全没有记录,但可以通过搜索找到菜谱。
One approach not yet given is to use a parser generator to create a parser in XSLT which parses JSON and produces an XML output.
One option that gets mentioned a lot at the XML conferences is the ReX parser generator (http://www.bottlecaps.de/rex/) - although totally undocumented on the site, recipes are available on searching.
可以将 XSLT 与 JSON 结合使用。
XPath(3.1) XSLT(3.0) 和 XQuery(3.1) 的版本 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,也没有完全评估此选项,但我希望研究一下很快就会有更多。到目前为止只是一些想法。
It may be possible to use XSLT with JSON.
Verson 3 of XPath(3.1) XSLT(3.0) and XQuery(3.1) supports JSON in some way.
This seems to be available in the commercial version of Saxon, and might at some point be included in the HE version.
https://www.saxonica.com/html/documentation/functions/fn/parse-json.html
-
What I would expect from an alternative solution:
I would want to be able input JSON to fetch a matching set of data, and output JSON or TEXT.
Access arbitrary properties and evaluate the values
Support for conditional logic
I would want the transformation scripts to be external from the tool, text based, and preferably a commonly used language.
Potential alternative?
I wonder if SQL could be a suitable alternative.
https://learn.microsoft.com/en-us/sql/relational-databases/json/json-data-sql-server
It would be nice if the alternative tool could handle JSON and XML
https://learn.microsoft.com/en-us/sql/relational-databases/xml/openxml-sql-server
I have not yet tried to convert the XSLT scripts I use to SQL, or fully evaluated this option yet, but I hope to look into it more soon. Just some thoughts so far.
CsvCruncher
尽管它的名字没有暗示,CsvCruncher 可以有效地用于读取表格* JSON 数据并使用 SQL 处理它们。
*
表格表示 JSON 树的某些部分是规则的,即具有相同或相似结构的一对多条目。CsvCruncher 将树的这一部分转换为 SQL 表,然后您就有完整的 SQL 语法来查询它。
您还可以加载多个 JSON 树,然后使用多个表。
您还可以将 SQL 结果存储为 JSON(或 CSV),然后再次处理。
这比真正复杂的 XSLT 方法更适合我(尽管当您真正深入了解它时它会很有趣)。
免责声明:我是 CsvCruncher 的开发者。
CsvCruncher
Despite it's name does not suggest it, CsvCruncher can be efficiently used to read tabular* JSON data and process them using SQL.
*
Tabular means that some part of the JSON tree is regular, i.e. one to many entries of same or similar structure.CsvCruncher turns this part of the tree into a SQL table, and then you have a full SQL syntax to query it.
You may also load multiple JSON trees, and then have multiple tables to play with.
You may also store the SQL result as a JSON (or CSV) and then process it again.
This has served me way better than really complicated XSLT approach (despite it's a lot of fun when you really know it deeply).
Disclaimer: I am the developer of CsvCruncher.
不太确定是否需要这样做,对我来说,缺乏工具就意味着缺乏需要。 JSON 最好作为对象进行处理(无论如何,它在 JS 中的处理方式),并且您通常使用对象本身的语言来进行转换(Java 用于从 JSON 创建的 Java 对象,同样适用于 Perl、Python、Perl、c#、PHP 等在)。只需进行正常的赋值(或设置、获取)、循环等即可。
我的意思是,XSLT 只是另一种语言,需要它的一个原因是 XML 不是对象表示法,因此编程语言的对象并不完全适合(分层 xml 模型和对象/结构之间的阻抗)。
Not too sure there is need for this, and to me lack of tools suggests lack of need. JSON is best processed as objects (the way it's done in JS anyway), and you typically use language of the objects itself to do transformations (Java for Java objects created from JSON, same for Perl, Python, Perl, c#, PHP and so on). Just with normal assignments (or set, get), looping and so on.
I mean, XSLT is just another language, and one reason it is needed is that XML is not an object notation and thus objects of programming languages are not exact fits (impedance between hierarchic xml model and objects/structs).