我需要一个技巧来让 xslt 让我们为变量设置新值 [英] i need a hack to make xslt let us set new values for variables

查看:36
本文介绍了我需要一个技巧来让 xslt 让我们为变量设置新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到,与编程语言不同,XSLT 不允许我们为变量设置新值.我只是想知道是否有任何黑客可以绕过人们正在使用的这个限制?因为能够重置变量的值本质上是有用的AFAIK

好吧,至少如果我们可以重置变量的值,我可以在这里解决我的问题:如何不在我的 xslt 代码中重复重复逻辑?

解决方案

我读过这与编程不同语言,XSLT 不允许我们为变量设置一个新值.

这里有两个错误的陈述:

  1. 不同于编程语言,XSLT".XSLT 一种编程语言.它是一组称为函数式编程语言的编程语言的成员.与您要说的最接近的正确陈述是:与 *过程式"编程语言不同"

  2. XSLT 不允许我们为变量设置新值".再次不正确 -- 不是 XSLT 不允许某些东西 -- 它是任何函数式编程语言,而 XSLT 是一种函数式编程语言.

<块引用>

我只是想知道是否有任何黑客攻击绕过这个限制人们正在使用?

没有黑客,因为人们不需要这样的黑客.无法改变变量值就无法生存的人,只需使用过程式编程语言即可.

<块引用>

因为拥有能够重置 a 的值变量本质上是有用的 AFAIK

实际上,拥有这样的能力在大多数情况下是有害的.理解程序和/或使用程序正确性验证器证明它的正确性变得更加困难.任何有副作用的程序也更难优化,而且任何此类程序的优化程度可能低于同样解决相同问题的不可变代码.

很高兴知道:众所周知,任何可以用过程语言解决的程序也可以用函数语言解决.因此,我们不需要可变变量——甚至没有一个问题不能以函数方式解决.认为问题无法以函数方式解决并不会真正使这个问题无法解决——问问其他人,他们会告诉你一个简洁的解决方案——通常比程序性解决方案.

可以用纯 XSLT 完成的一个很好的例子FXSL 图书馆.FXSL 提供了一组用于解决大类问题的函数/模板,其中包括非常复杂的问题,例如求任何幂 (x^y) 或对数,或基本三角函数,或求解任何方程f(x) = 0 等等......

FXSL 函数的其他示例是在文本处理领域——单词标记化、拼写检查、索引构建等......

使用 FXSL 函数实现更复杂的任务:通用 LR-1 解析器、JSON 和 XPath 2.0 解析器、随机数序列的生成、数值积分和微分、...等

所有这一切都是在不更改任何变量的情况下完成的 -- XSLT 不允许这样做"这一事实并没有让您感到不便 :)

I've read that unlike programming languages, XSLT does not allow us to set a new value to a variable. I'm just wondering is there any hacks to get around this limitation that people are using? because having the ability to reset the value of a variable is inherently useful AFAIK

Well at least if we could reset the value of a variable, I could solve my problem here: how do i not repeat repeated logic in my xslt code?

解决方案

I've read that unlike programming languages, XSLT does not allow us to set a new value to a variable.

Two incorrect statements here:

  1. "unlike programming languages, XSLT". XSLT is a programming language. It is a member of a group of programming languages called Functional Programmin Languages. The closest correct statement to what you are trying to say is: "unlike *procedural" programming languages"

  2. "XSLT does not allow us to set a new value to a variable". Again incorrect -- it is not XSLT that doesn't allow something -- it is any functional programming language, and XSLT is a functional programming language.

I'm just wondering is there any hacks to get around this limitation that people are using?

There are no hacks as people don't need such hacks. People who cannot live without being able to change the value of a variable, simply use a procedural programming language.

because having the ability to reset the value of a variable is inherently useful AFAIK

Actually, having such an ability is harmful in most cases. It makes much more difficult to understand a program and/or to prove it correct with program correctness verifiers. Any program with side effects is also more difficult to optimize and any such programs may be less optimized than immutable code that also solves the same problem.

It is good to know: It is wellknown that any program that can be solved with a procedural language can also be solved with a functional language. Therefore, one doesn't need a mutable variable -- there isn't even a single problem that cannot be solved in a functional way. That you think the problem is unsolvable in a functional way doesn't really make this problem unsolvable -- just ask other people and they will show you a neat solution -- often much shorter and much more elegant than a procedural solution.

A good example what can be done with pure XSLT is the FXSL library. FXSL provides a set of functions/templates of solving large classes of problems, among them quite complicated ones such as finding any power (x^y) or logarithm, or the basic trigonometric functions, or solving any equation f(x) = 0, etc. ...

Other examples of FXSL functions are in the area of text-processing -- word tokenization, spelling checking, concordance building, etc. ...

Even more complex tasks implemented with FXSL functions: a generic LR-1 parser, parsers for JSON and for XPath 2.0, generation of a sequence of random numbers, numerical integration and differentiation, ..., etc.

All this is done without changing any variables -- not a bit disconvenienced by the fact that "XSLT doesn't allow this" :)

这篇关于我需要一个技巧来让 xslt 让我们为变量设置新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆