开发者

i need a hack to make xslt let us set new values for variables

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 lea开发者_JS百科st 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 variables have to be intended in the traditional sense of mathematics, and not as variables in the common sense of procedural languages like C++.

XSLT does not allow us to set a new value to a variable.

Actually is not that really true, in the sense that variable are scoped in the XSLT element they are contained.

Therefore, if you define a variable as child of xsl:stylesheet you have to treat it like a global constant.

But if you define a variable as child of xsl:template, while you have to treat your variable like a local constant in the current context of the template only, in the scope of the transform you can think of it as a variable which can take a different value according to the template context. The same applies to named template or functions.

is there any hacks to get around this limitation?

Just get used to work with them in a functional way.


Abstract example:

If you define a variable as child of xsl:stylesheet so that they get values according to the transform parameters, you can think the variable as a variable in the scope of the transform.

So think a stylesheet with parameters as a function:

F(x)

and a template with a variable, as a sub-function within the F(x), let say:

T(y)

So that you can have the abstract:

F(x) = G( T(y), x )

you can also have, for example:

F(x) = G( T(y(x)), x )

depending on how you design your stylesheet.


In XSLT, you have constants, not variables. No idea why they call them variables. They don't vary. They're constant. The only hack I know of is to hack your mind into understanding XSLT. Not the best solution, I know...but that's XSLT.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜