Re: [xsl] Matching on keys

Subject: Re: [xsl] Matching on keys
From: "Graydon graydon@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 6 Jan 2017 20:16:46 -0000
On Fri, Jan 06, 2017 at 05:56:02PM -0000, Wendell Piez wapiez@xxxxxxxxxxxxxxx scripsit:
> Question: I can define a key, as in
> 
> <xsl:key name="elements-by-class" match="*[matches(@class,'\S')]"
> use="tokenize(@class,'\s+')"/>
> 
> then
> 
> <xsl:template match="key('elements-by-class','foo')">
>  ...
> </xsl:template>
> 
> I regard this as a neat trick, and think it might be an "improvement"
> :-) but I suppose from one point of view at least, that may be
> arguable.
> 
> What do readers think? Should I prefer a stylesheet function instead?

I've been doing something similar (@xmi:id and @xmi:idref rather than
@class) and finding that creating global variables with maps in them
that relate the key value to a node in the source document works very
well even with Saxon 9.6 which presumably doesn't have all the
optimizations for 3.0.  One can

<xsl:apply-templates select="$map($key)" />

even when there's a sequence of element nodes being mapped by that key
and (if I understand xsl:map correctly) away it goes to the node of the
source document via indirection rather than a copy.

There'd probably be a structural difference to the "can we match all of
these by template?" approach which might not be useful for what you want
to do; I find most of the logic wanders into the map-building steps in
the variables.

It does mean you have to use 3.0 but I'm inclined to claim that's a
feature. :)

-- Graydon

Current Thread