模板:XSLT 与 jQuery
我需要一个与我的应用程序捆绑在一起的 html 页面的模板机制。起初我查看了所有的 javascript 模板解决方案(如 jQuery 模板),但由于我的输入数据是 XML,我突然又想起了 XSLT。我遇到了数十个库,但似乎没有一个使用 XSLT,所以我完全忘记了它的存在,以及它对于创建模板有多么有用。
那么,XSLT 是否会慢慢被 javascript 替代品所弃用并逐步淘汰呢?我在某处读到 XSLT 对于大多数用户来说太复杂了,无法使用,但这是它唯一的缺点还是还有更多缺点?
更新:我自己只能想到一个缺点:使用 XSLT 时,必须在向用户显示任何内容之前解析/渲染整个页面,而使用 javascript 时,页面已经可见,并且缺少的元素是之后填满。
I need a templating mechanism for html-pages bundled with my app. At first I looked at all the javascript templating solutions (like jQuery templates), but since my input data is XML, I suddenly remembered XSLT again. I came across tens of libaries, but none seemed to make use of XSLT, so I completely forgot about its existence, and how useful it is for creating templates.
So, is XSLT slowly deprecated and phased out by javascript alternatives? I read somewhere XSLT was too complicated for most users to work with, but is that its only downside or are there more cons?
Update: I can think of only one disadvantage myself: with XSLT the complete page has to be parsed/rendered before displaying anything to the user, and with the javascript the page is already visible and the missing elements are filled afterwards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
XSLT 是一种现代 XML 转换和函数式编程语言。即将推出的 3.0 版本本身支持(与 XPath 3.0 一起)高阶函数以及其他新功能。
XSLT 并不“古老”(很少有人知道 XSLT 2.0 或知道 XSLT W3C XSLT WG 正在开发 3.0。
浏览器中的 XSLT 最近由 Saxon CE 大力推动strong> —— 一个精简的 XSLT 2.0 处理器,从 Java 编译为 Javascript,并可在五种主要浏览器上使用客户端。 @Michael Kay 甚至演示了在他的 iPhone 上运行客户端的 XSLT 2.0...
XSLT is a modern XML transformation and functional programming language. The upcoming version 3.0 natively supports (together with XPath 3.0) higher-order functions, among other new features.
XSLT isn't "old" (few people know XSLT 2.0 or are aware that XSLT 3.0 is being worked on by the W3C XSLT WG).
XSLT in the browser had a recent big push forward by Saxon CE -- a trimmed down XSLT 2.0 processor that is compiled from Java to Javascript and is available client-side on the five major browsers. @Michael Kay even demoed XSLT 2.0 running client side on his iPhone...
XSLT 是一个很好的模板解决方案。语言和语法有点不寻常——“选择/何时”而不是“案例/切换”是一个明显的例子——但它做得很好。
XSLT 的主要优点是您不需要 JavaScript 即可使用它;正确构建它,无论是否启用 JS,每个主流浏览器都会将其呈现为 HTML。它在处理器上可能比 JavaScript 解决方案更容易,尽管我不知道有谁测试过这个。如果您已经在处理 XML 数据,那么这也是显而易见的选择。
缺点是它是一项(相对)旧的技术,基本上已被放弃,虽然浏览器仍然支持它,但尚未继续发展。 Firefox 存在 HTML 转义问题,无意修复,而 Chrome @includes 存在相当大的问题。 IE 显然是最新的,但这当然仅适用于最新版本。
结果是,如果您想使用 XSLT,则必须在所有主要浏览器中测试它并解决错误,尽管该技术至少从 2006 年就已经存在。它绝不是被弃用的,它只是不是很受欢迎,因为它不像 JSON + jQuery 模板那样紧凑或易于阅读。
XSLT is a good solution for templating. The language and syntax are a bit unusual -- "choose/when" instead of "case/switch" is a glaring example -- but it does its job well.
XSLT's main advantage is that you don't need JavaScript to use it; build it right and every major browser will render it as HTML whether JS is enabled or not. It may be easier on the processor than JavaScript solutions, although I don't know anyone who's tested this. It's also the obvious choice if you're already dealing with XML data, as you are.
The downside is that it's a (relatively) old technology which has been largely abandoned, and while browsers still support it, they haven't moved forward with it. Firefox has an issue with HTML escaping that they have no intention of fixing, and Chrome has a pretty major problem with @includes. IE is apparently up-to-date, but that of course only applies to the latest version.
The upshot is that if you want to use XSLT, you have to test it in all your major browsers and work around the bugs, despite the fact that the technology has been around since at least 2006. It is by no means deprecated, it's just not very popular because it's not as compact or easy to read as, say, JSON + jQuery templates.
在浏览器中呈现 XSLT 有一些奇怪的特定于浏览器的怪癖。如果我需要在客户端呈现数据,我宁愿使用 jQuery 模板。
在服务器端,XSLT 是一个很棒的工具,支持多种编程语言(至少对于 XSLT 1.0,对于 XSLT 2.0,您只能在 Java 和 .NET 之间进行选择)。因此,也许您可以使用某种中间件来获取客户端请求,将其传递到 Web 服务或生成 XML 的任何内容,使用 XSLT 样式表将其呈现为 HTML,并将 HTML 结果传递到浏览器。如果您的特定场景允许在中间件层进行缓存,您的应用程序也会更快,因为客户端不必处理 jQuery 或 XSLT 模板。
Rendering XSLT in the browser has some strange browser-specific quirks. I would rather use jQuery templates if I needed to render data on the client side.
On the server side, XSLT is a great tool with support for many programming languages (at least for XSLT 1.0, for XSLT 2.0 you can only choose between Java and .NET). So maybe you can have some kind of middleware that gets the client-side request, passes it on to the web service or whatever is generating the XML, renders it as HTML with an XSLT style sheet and passes the HTML result to the browser. If your specific scenario allows caching in the middleware layer, your application will also be faster since the client does not have to process jQuery or XSLT templates.
下面是加载 XML 和 XSL 并在 Javascript 中执行转换的示例。
http://www.w3schools.com/xsl/xsl_client.asp
有时不是可以在 XML 文档中导入 XSLT,例如从第三方获取 XML 并使用 XSLT 转换为 HTML 并在浏览器中呈现。
Here is the example to load XML and XSL and perform transformation in Javascript.
http://www.w3schools.com/xsl/xsl_client.asp
Sometimes it is not possible to import XSLT in XML document, for example getting XML from third party and using your XSLT transform to HTML and render in browser.