其他语言中 XSLT 的两个概念:apply-template 和 xpath
背景: 在放弃了 XSLT 作为我的编程工具包的一部分的实际日常使用之后,我想知道是否有其他语言的实现来实现我对该工具的(唯一)两件事:
- 通过 xpath 使用“路径”样式语句遍历数据结构的能力
- 使用 apply-templates 而不是通过迭代或“循环”方法遍历模板转换的能力。
根据 Google 的说法,有一些努力为 Javascript 添加“xpath 风格”支持,但这些显然还没有流行起来。 到目前为止,我还没有发现任何有人在另一种语言中使用“应用模板”方法
问题:有人知道一种编程语言(希望是一种是主流)从 XSLT 中窃取这两个好主意,或者使用不同的方法应用相同或相似的概念?
Background: Having given up on the practical daily use of XSLT as a part of my programming toolkit, I was wondering if there were any implementations in other languages of the (only) two things I miss about that tool:
- the ability to traverse data structures using "path" style statments via xpath
- the ability to traverse template transformations using apply-templates instead of via an iterative or "looping" approach.
According to Google there are a couple of efforts out there to add "xpath-style" support to Javascript, but these have not apparently caught on very much. So far I haven't found anything where someone uses an "apply-templates" approach in another language
Question: Does anyone out there know of a programming language (hopefully one that is main-stream) that steals these two good ideas from XSLT, or applies the same or similar concepts using a different method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对您的第一个要求的评论:
XPath 对数据结构做出了很多假设。 如果您打算使用它,您也可以将您的结构转换为 XML,因为一旦您使其可通过某种类似 XPath 的语言进行遍历,它就会看起来像 XML,除非您严格限制您的 XPath 子集。
另外,请记住,您缺少的“只有两件事”,即 XPath 和模板处理,实际上是构成 Xslt 的重要组成部分。 我很好奇你为什么决定把它从你的工具带上拿下来。
尽管您想要 Xslt 替代方案,但我仍然特别推荐 Xslt 和 Xslt 2.0。 添加 unparsed-text 和 analyze-string 您拥有强大的文本处理语言。 例如,查看 CSV到 XML 样式表。 即使 JSON 不是常规的,您仍然可以使用递归模板编写一个简单的 JSON 到 XML 转换器,并随意转换结果。
A comment on your first requirement:
XPath makes a lot of assumptions on the data structure. If you're going to use it, you might as well convert your structure to XML because it's going to look like it anyway once you make it traversable via some XPath-like language unless you severely limit your XPath subset.
Also, keep in mind that the "only two things" that you are missing, XPath and template processing, are in-fact a huge part of what makes up Xslt. I'm curious why you decided to take it off of your tool-belt.
In spite of that fact that you wanted an Xslt alternative, I would still recommend Xslt and Xslt 2.0 in particular. With the addition of the unparsed-text and analyze-string you have a powerful text processing language. For example take a look at a CSV to XML stylesheet. Even though JSON isn't regular, you'd still be able to write a simple JSON to XML translator using recursive templates and transform the result at will.
我还没有发现这样的事情。 但为什么有人会使用其他东西来转换 XML 呢? 一旦您了解了开发解决方案的非过程方式,XSLT 就可以完美地完成工作。 我们的应用程序主要基于 XSLT,它是一个非常强大的工具。
I have found nothing like that. But why would anybody use anything else to transform XML ? XSLT does a perfect job once you understand the non procedural way of developing solutions. Our applications are largely XSLT based and it is a really powerful tool.
对于 XPath,绝对如此。 对于 C,有 Xalan-C++,对于 Java javax.xml.xpath (有多个实现),并且 C# 有 XPathNavigator 和选择节点。 如果您想将 XPath 用于对象层次结构,请查看 JXPath。
对于模板转换,如果您还没有,您应该查看 C# 的 LINQ还没有。 它并不完全相同,但它允许在没有显式循环的情况下处理对象。
For XPath, definitely. For C, there's Xalan-C++, for Java javax.xml.xpath (with multiple implementations), and C# has XPathNavigator and SelectNodes. If you want to use XPath for object hierarchies, look at JXPath.
For the template transformations, you should look at C#'s LINQ if you haven't already. It's not exactly the same thing, but it allows processing objects without explicit looping.
XPath 虽然对于 XSLT 的工作至关重要,但又独立于 XSLT。 像 libxml 这样的库免费为您提供它。 您描述的模板应用程序的风格有点棘手; 这就是您通常使用 XSLT 的目的。
任何执行此操作的编程语言都应该是函数式的。 您可以尝试编写自己的、不太冗长的 XSLT 方言; Perl 还可以为您提供足够的绳索来令人信服地模拟此功能(尽管性能影响尚不清楚)。
然而,艰难的答案是,除了作为现有语言的库之外,它实际上并不存在。
XPath, while essential to making XSLT work, is independent of it; libraries like libxml give you it for free. The style of template application you describe is a little trickier; that's what you would normally use XSLT for.
Any programming language that does this should be functional. You could try writing your own, less-verbose, XSLT dialect; Perl also may give you enough rope to emulate this feature convincingly (although the performance implications are unclear).
The tough answer, though, is that this doesn't really exist, except as libraries for already existing languages.
我不知道还有任何其他嵌入 XPath 的语言,但 LINQ to XML 有点类似,特别是在其 VB 语法糖化身。 不过,您可以在 Common Lisp 宏或 D 模板中实现它。
据我所知没有主流语言。 事实上,此功能可能是使用 XSLT(而不是 XQuery,查看密切相关的语言)的主要原因。
它是在任意条件下对接收器进行有效可扩展的动态调度 - 因此,我认为你可以在 Common Lisp 中做到这一点(具体来说,CLOS) - 如果我没记错的话,它的多种方法可以匹配任意条件,所以如果你有一个 XPath模式求值器,您可以使用它来模拟
apply-templates
甚至更多 - 因为apply-templates
仅在单个参数上分派,而 CLOS 多方法在多个参数上分派。I'm not aware of any other language that embeds XPath, but LINQ to XML is somewhat similar, particularly in its VB syntactic sugar incarnation. You could implement it in Common Lisp macros, or D templates, however.
No mainstream languages that I know of. Indeed, this feature is probably the main reason to use XSLT (and not e.g. XQuery, looking at closely related languages).
It's effectively extensible dynamic dispatch on receiver on arbitrary conditions - as such, I think you could probably do it in Common Lisp (CLOS, to be specific) - if I remember correctly, its multimethods can match arbitrary conditions, so if you have an XPath pattern evaluator, you could use it to emulate
apply-templates
, and even more - sinceapply-templates
only dispatches on a single argument, while CLOS multimethods dispatch on multiple arguments.