Re: [xsl] Best practice for typing?

Subject: Re: [xsl] Best practice for typing?
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 23 Nov 2019 10:18:18 -0000
Declaring the type on xsl:param is always good practice.

Declaring the type on xsl:variable is also good practice, though I don't tend
to bother if it's obvious, e.g. <xsl:variable name="x" select="12"/>.

Declaring the type on xsl:with-param can be useful documentation for someone
reading the code, in cases where the type isn't obvious, but it's not
something I regard as important.

Declaring the return type on functions and templates is also good practice; I
always do it for functions, but I tend not to bother for templates if they are
constructing new elements, since that's usually obvious.

In Saxon, I think the only case where declaring a type is likely to incur a
significant run-time cost is with maps: checking that a map conforms to its
declared type can be expensive, though Saxon does try to keep track of the
type information through map modifications in many cases. By contrast,
providing type information can sometimes enable very worthwhile optimizations.
For example with the expression $x/firstName, it's very useful to know
statically that $x is a single node: this not only avoids a run-time check, it
can also avoid a sort operation (but actually, we should get into the habit of
writing this as $x!firstName instead). Similarly with xxx[$i] it is very
useful to know in advance that $i is an integer.

Michael Kay
Saxonica

> On 23 Nov 2019, at 01:44, David Birnbaum djbpitt@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> Dear xsl-list,
>
> Is there a consensus about best practice with respect to possibly redundant
typing? For example, we can specify a type using @as on corresponding
<xsl:param> and <xsl:with-param> elements, but if the type is specified on
<xsl:param>, it seems as if that might make it redundant also to specify it on
<xsl:with-param>, since using the wrong type at the level of <xsl:with-param>
will be caught by the type specification on <xsl:param> anyway. Similarly, if
I construct the return value of a function by using <xsl:sequence> inside the
body of the function, I can specify the typing on <xsl:sequence>, but also on
<xsl:function>, and it seems as if specifying it in either of those places
will catch the same errors as specifying in both. (I am only beginning to
learn to use schema-aware processing, and therefore less certain about how
specifying @type when constructing an interim element or attribute interacts
with specifying @as when using it.)
>
> Is there consensus among XSLT developers about whether it might be better to
use @as everywhere it is allowed, or, alternatively, better to avoid using it
redundantly? What is the practice of other readers of this list?
>
> Thanks,
>
> David
> djbpitt@xxxxxxxxx <mailto:djbpitt@xxxxxxxxx>
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/293509> (by
email <>)

Current Thread