xslt 的优雅示例?

发布于 2024-07-15 09:28:00 字数 297 浏览 10 评论 0原文

经过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小霸王臭丫头 2024-07-22 09:28:01

人们经常可以找到漂亮的 XSLT 代码示例,尤其是当 XSLT 用作函数式编程语言时

有关示例,请参阅本文FXSL 2.0 上的strong>——XSLT 函数式编程库2.0。

作为一种 FP 语言,XSLT 也是一种声明性语言 。 除其他外,这意味着人们声明、指定现有的关系。

这样的定义通常不需要任何额外的代码来产生结果——它本身就是它自己的实现,或者是可执行的定义或可执行的规范。

这是一个小例子

此 XPath 2.0 表达式定义最大质因数 of a nature number”:

if(f:isPrime($pNum))
  then $pNum
  else
    for $vEnd in xs:integer(floor(f:sqrt($pNum, 0.1E0))),
        $vDiv1 in (2 to $vEnd)[$pNum mod . = 0][1],
        $vDiv2 in $pNum idiv $vDiv1
      return
        max((f:maxPrimeFactor($vDiv1),f:maxPrimeFactor($vDiv2)))

用英语发音,数字的最大素因数pNum就是数字本身,如果 pNum 是质数,否则如果 vDiv1vDiv2pNum 的两个因数,那么 pNum 的最大素数因子是最大素数中较大的一个vDiv1vDiv2 的因子。

我们如何使用它来实际计算 XSLT 中的最大质因数? 我们只需将上面的定义包装在 ,然后...得到结果!

 <xsl:function name="f:maxPrimeFactor" as="xs:integer">
  <xsl:param name="pNum" as="xs:integer"/>

  <xsl:sequence select=
   "if(f:isPrime($pNum))
      then $pNum
      else
        for $vEnd in xs:integer(floor(f:sqrt($pNum, 0.1E0))),
            $vDiv1 in (2 to $vEnd)[$pNum mod . = 0][1],
            $vDiv2 in $pNum idiv $vDiv1
          return
            max((f:maxPrimeFactor($vDiv1),f:maxPrimeFactor($vDiv2)))
   "/>
 </xsl:function>

然后,我们可以计算任意自然数的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":

if(f:isPrime($pNum))
  then $pNum
  else
    for $vEnd in xs:integer(floor(f:sqrt($pNum, 0.1E0))),
        $vDiv1 in (2 to $vEnd)[$pNum mod . = 0][1],
        $vDiv2 in $pNum idiv $vDiv1
      return
        max((f:maxPrimeFactor($vDiv1),f:maxPrimeFactor($vDiv2)))

To pronounce it in English, the maximum prime factor of a number pNum is the number itself, if pNum is prime, otherwise if vDiv1 and vDiv2 are two factors of pNum, then the maximum prime factor of pNum is the bigger of the maximum prime factors of vDiv1 and vDiv2.

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!

 <xsl:function name="f:maxPrimeFactor" as="xs:integer">
  <xsl:param name="pNum" as="xs:integer"/>

  <xsl:sequence select=
   "if(f:isPrime($pNum))
      then $pNum
      else
        for $vEnd in xs:integer(floor(f:sqrt($pNum, 0.1E0))),
            $vDiv1 in (2 to $vEnd)[$pNum mod . = 0][1],
            $vDiv2 in $pNum idiv $vDiv1
          return
            max((f:maxPrimeFactor($vDiv1),f:maxPrimeFactor($vDiv2)))
   "/>
 </xsl:function>

We can, then, calculate the MPF for any natural number, for example:

f:maxPrimeFactor(600851475143) = 6857

As for efficiency, well, this transformation takes just 0.109 sec.

Other examples of both ellegant and efficient XSLT code:

深陷 2024-07-22 09:28:01

代码生成怎么样? 我在 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.

随梦而飞# 2024-07-22 09:28:01

前几天我使用了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文