[Home] [By Thread] [By Date] [Recent Entries]
Hi Justin,
This is a lot about whether you should create a method or a field in javascript, or a sub or a function on old basic, or a struct or a class in C++. Sometimes you can interchange them (a struct in C++ can be replaced by a class), sometimes not (a struct can have functions, but does not do encapsulation). Depending on your need, a programmer should choose what best suits his or her task. In the case of functions/named templates, this will depend on what the programmer needs: 1. If you need an initial template, the only way is a named template 2. If you need the current context node, the obvious choice is a named template 3. If you want to mix both matching and calling of a template, you need a named/matching template 4. If you need/want to use it inside an xpath expression, you likely choose a function 5. If you want recursion, a function is probably easier 6. If you want polymorphism (partially, only nr of arguments) you must use a function 7. If all you need to do is act on a simple data type (not a node), you probably want a function 8. If you like import precedence tricks, templates may be easier, because they don't error when they hit the same imp precedence 9..... etc, I am sure this list can be extended beyond the horizon ;) See my comments below. Justin Johansson wrote: Please correct me if I am wrong on this one :- yes xsl:templates can, and must, be invoked within a sequence constructor either You can cheat: if you place your template inside a function you can call your template from an xpath expression. Don't forget about the initial template (-it on the commandline of saxon) and the mix: both named and matching template. xsl:functions can, and must, be invoked from an XPath expression. yes Consequently the use of call-template is extemely analogous to yes. Mainly, named parameters are not possible with functions, and the order is important when calling the function. With a named template the order is not important. All parameters are mandatory in a function (making it tedious if you want several parameters having a default, this means writing several function statements for all arities). Not so in a template, where a parameter can have a default and is optional (unless the required attr is set). The bottom line is that named templates and functions are essentially Maybe so, maybe not. See the list above. There are some occasions where I still favor call-template over a function call. This is of course a design issue and has much to do with clarity of the code, or readability. But in my experience, since XSLT 2.0, I have used call-template less and less. The rationale for asking this question is that is generally a good idea to eliminate choices (from the programmer) for the way something is accomplished unless there are compelling reasons to provide the alternatives. Perhaps. I think that any good programmer should be able in making weighted choices. Poor programmers, or programmers learning a new language, are often best helped when they hit their head a couple times when making the wrong choices, instead of being told to so because of some guideline. At the other hand, having a guideline with good argumentation, will help both good and poor programmers to increase their understanding of the subject, and still make up their own mind, of course.
|

Cart



