Re: Allow variable before sort?

I've looked at this in the past, and although I can clearly see the utility, the semantics get surprisingly complicated, and the implementation is quite complicated too. 

Typically, I think you would want the value of the variable to depend on the item being sorted, and if it has to be evaluated before the sort, but used afterwards, then the value o the variable has to be somehow retained along with the item so it survives the sort and can be reinstated afterwards. In most cases it's probably cheaper to re-evaluate it. There's also the problem of position(): the position in the unsorted sequence isn't the same as the position in the sorted sequence, which creates a lot of scope for semantic confusion. 

Have you considered

<xsl:for-each select="//employee ! map{ 'e':., 'var': compute-variable(.) }">
   <xsl:sort select="?var"/>
   ....
</xsl:for-each>

Michael Kay
Saxonica




> On 1 Dec 2020, at 14:31, Joel Kalvesmaki <kalvesmaki@gmail.com> wrote:
> 
> Hi everyone,
> 
> Would there be any problems in allowing <xsl:variable> to land before <xsl:sort> in XSLT 4.0? No doubt engines have been able to optimize for instances where patterns in xsl:sort/@select are replicated in the body of an <xsl:for-each> or <xsl:for-each-group>. But is there a drawback to letting the user declare <xsl:variable> before the sort is conducted? Many times I have wanted to do so, to avoid repeating an in the body of the operation. 
> 
> In such cases I often resort to building a function, but some of these are one-time deals, and it would be cleaner, from my perspective to do this through a variable.
> 
> jk
> -- 
> Joel Kalvesmaki
> kalvesmaki.com <http://kalvesmaki.com/>

Received on Tuesday, 1 December 2020 14:50:35 UTC