xslt 的优雅示例?
经过 XAML 的长时间学习循环后,我回到了 HTML 和 javascript,并意识到声明性代码的概念(就转换规则而言)是一个非常强大的概念。
尽管语法过多,但 XML 的 XSLT 处理仍然是声明性转换编程的基石。 然而,我总是发现很难理解如何将 XSLT 用于日常任务(使用 XML)。
除了生成 HTML 之外,还有哪些 XSLT 优雅地解决编程问题的好例子?
我猜它擅长图形转换和数据重新处理...
编辑:我希望有一些实际的例子 - 让我完全放弃 xslt 的原因之一是代码的视觉复杂性。
After a long learning loop via XAML, I've returned to HTML and javascript, and realised that the concept of declarative code - in terms of rules for transformation - is an incredibly powerful concept.
Despite its surfeit of syntax, XSLT processing of XML is the keystone of declarative transformation programming. I have, however, always found it hard to understand how XSLT would be used for everyday tasks (with XML).
What are some good examples of XSLT elegantly solving a programming problem, outside of generating HTML?
I'm guessing it's good at graph transformation and data reprocessing...
Edit: I'm hoping for some actual examples - one of the things that puts me off full-on xslt is the visual complexity of the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
人们经常可以找到漂亮的 XSLT 代码示例,尤其是当 XSLT 用作函数式编程语言时。
有关示例,请参阅本文FXSL 2.0 上的strong>——XSLT 函数式编程库2.0。
作为一种 FP 语言,XSLT 也是一种声明性语言 。 除其他外,这意味着人们声明、指定现有的关系。
这样的定义通常不需要任何额外的代码来产生结果——它本身就是它自己的实现,或者是可执行的定义或可执行的规范。
这是一个小例子。
此 XPath 2.0 表达式定义“最大质因数 of a nature number”:
用英语发音,数字的最大素因数
pNum
就是数字本身,如果pNum
是质数,否则如果vDiv1
和vDiv2
是pNum
的两个因数,那么pNum
的最大素数因子是最大素数中较大的一个vDiv1
和vDiv2
的因子。我们如何使用它来实际计算 XSLT 中的最大质因数? 我们只需将上面的定义包装在
中,然后...得到结果!然后,我们可以计算任意自然数的MPF,例如:
f:maxPrimeFactor(600851475143)
= 6857至于效率,这个转换只需要 0.109 秒。
优雅且高效的 XSLT 代码的其他示例:
One can often find examples of beautiful XSLT code, especially when XSLT is used as a functional programming language.
For examples see this article on FXSL 2.0 -- the Functional Programming library for XSLT 2.0.
As an FP language XSLT is also a declarative language. This, among other things means that one declares, specifies existing relationships.
Such a definition often does not need any additional code to produce a result -- it itself is its own implementation, or an executable definition or executable specification.
Here is a small example.
This XPath 2.0 expression defines the "Maximum Prime Factor of a natural number":
To pronounce it in English, the maximum prime factor of a number
pNum
is the number itself, ifpNum
is prime, otherwise ifvDiv1
andvDiv2
are two factors ofpNum
, then the maximum prime factor ofpNum
is the bigger of the maximum prime factors ofvDiv1
andvDiv2
.How do we use this to actually calculate the Maximum Prime Factor in XSLT? We simply wrap up the definition above in an
<xsl:function>
and ... get the result!We can, then, calculate the MPF for any natural number, for example:
f:maxPrimeFactor(600851475143)
= 6857As for efficiency, well, this transformation takes just 0.109 sec.
Other examples of both ellegant and efficient XSLT code:
代码生成怎么样? 我在 protobuf-net 中使用它从 .proto 模型创建 .cs 文件(导出为 xml) - 像这样。 在 .NET 4.0 中,T4 模板将是更明显的选择,但在此之前 T4 引擎并不是独立的(据我所知)。
Xslt 还广泛用于在不同的 xml 布局之间进行转换 - 例如,在 biztalk 等中间件工具中。
How about code generation? I use this in protobuf-net to create the .cs files from a .proto model (exported as xml) - like so. In .NET 4.0, T4 templates would be a more obvious choice, but the T4 engine isn't standalone (AFAIK) before then.
Xslt is also used extensively to transform between different xml layouts - for example, in middleware tools like biztalk.
前几天我使用了 XSLT。 我需要提供一种方法让用户将树控件的内容保存到文件中,以便他们可以通过电子邮件将其发送给老板、向他们的孙辈展示等。
要求他们能够将其保存为 HTML文件具有漂亮整洁的外观,或者作为 XML,以便他们可以根据自己的意愿进一步处理它。
因此,我建议我们输出链接有 XSLT 的 XML,这样他们就拥有原始 XML,但在浏览器中打开它时看起来很不错。
这并不理想(如果他们将 HTML 移动到没有 XSL 的其他地方,IE 根本拒绝显示它!)但是如果我们改变计划,我可以修改产品,这样它会让用户选择格式,如果他们选择HTML,我所要做的就是在保存 XML 之前运行 XSLT 以获取 HTML。
理想情况下,有一种方法可以制作一个包含 XML 和 XSLT 的独立 HTML 文件,但我不知道有什么方法......这本身就是一个问题。
I used XSLT the other day. I needed to provide a way to let the user save the contents of a tree control to a file so they could email it to their boss, show it to their grandchildren, etc.
It was requested that they be able to save it as an HTML file with a nice tidy look to it, or as XML so they could process it further however they wished.
So I suggested that we output XML with an XSLT linked to it, so that way they have the raw XML but it looks nice when they open it in the browser.
It's not ideal (if they move the HTML somewhere else without the XSL, IE refuses to display it at all!) but if we change plans, I can just modify the product so it will let the user choose the format, and if they choose HTML, all I have to do is run the XSLT on the XML to get the HTML before I save it.
Ideally there would be a way to make a self-contained HTML file that contained XML and XSLT, but I'm not aware of one... which is a question in itself.