Re: [xsl] user-defined XSLT functions and stylesheet variables?

Subject: Re: [xsl] user-defined XSLT functions and stylesheet variables?
From: "Jeremy Cronk jcronk@xxxxxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 20 Jun 2021 17:35:40 -0000
Forgive my ignorance on this, but why is there a global context item in the
first place?  Ive never seen it do anything but cause trouble, but on the
other hand, Ive never seen it actually being used except by global
variables.

Jeremy


From: Michael Kay mike@xxxxxxxxxxxx <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Friday, June 18, 2021 at 2:43 PM
To: xsl-list <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject: Re: [xsl] user-defined XSLT functions and stylesheet variables?
This email originated from outside of the organization. Do not click links or
open attachments unless you recognize the sender and know the content is
safe.

First, I'd suggest that it's bad practice to have global variables whose value
depends on the global context item. If you have such variables, it makes it
much harder to reuse the code, e.g. as part of a pipeline. (If you want, you
can use the xsl:global-context-item declaration to enforce this.)

If your global variables satisfy that constraint, I think there are cases
where referencing global variables within functions is a perfectly reasonable
thing to do. Referring to variables that represent manifest constants is
obviously harmless. Referring to the values of stylesheet parameters can also
make sense, though I'd look at each case on its merits. For example, if you
have a stylesheet parameter that's a set of currency exchange rates, having a
set of functions that acts as an API to this parameter document
(f:getExchangeRate('USD', 'GBP')) might be entirely sensible.

Michael Kay
Saxonica


On 18 Jun 2021, at 18:41, David Birnbaum
djbpitt@xxxxxxxxx<mailto:djbpitt@xxxxxxxxx>
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx<mailto:xsl-list-service@xxxxxxxxxxxx
rytech.com>> wrote:

Dear XSLT-list,

Is there agreement about Best Practice with respect to using stylesheet
(global) variables inside user-defined functions?

If I want to be able to reuse my function in different contexts, passing in
everything the function needs as an explicit parameter ensures that it won't
have expectations about the global context. There may also be philosophical
reasons that functions should be entirely self-contained, and should not
depend on variables available in the execution context.

Meanwhile, since XSLT does not allow variables to be modified, the concern (in
other languages) about modifying global variables inside functions does not
obtain in XSLT, and my use of stylesheet variables would be read-only. If I've
written a one-off function intended for use only in a single stylesheet,
passing global variables into it as explicit parameters is more verbose than
relying on the fact that the function, like everything else in the stylesheet,
has access to stylesheet variables. Passing everything in explicitly does make
the function more self-documenting (since everything the function uses is
declared as a parameter inside the body) and perhaps also more convenient to
test, but what do other developers think about how to balance those advantages
against the added verbosity?

Best,

David
XSL-List info and archive<http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe<http://lists.mulberrytech.com/unsub/xsl-list/293509> (by
email)

XSL-List info and archive<http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe<http://lists.mulberrytech.com/unsub/xsl-list/3354580> (by
email<>)

Current Thread