[xsl] Matching on keys

Subject: [xsl] Matching on keys
From: "Wendell Piez wapiez@xxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 6 Jan 2017 17:55:54 -0000
Hello XSL-List,

I am having more and more occasions these days to handle HTML @class
semantics, which as you probably know can be 'overloaded' in the sense
that multiple values (NMTOKEN) of 'class' may have to be distinguished
and handled separately.

An idiom for matching any element .foo (CSS selector) could be

<xsl:template match="*[tokenize(@class,'\s+')='foo')]">
   ...
</xsl:template>

This will match the element by virtue of the value being present on
the @class, split apart at white space and compared (many-to-one) with
=

(That logic can of course be relegated to a stylesheet function call
for neatness. It's true you can get an element matching more than one
template this way: that's the idea.)

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?
(John Lumley what do you think?)

Regards,
Wendell

-- 
Wendell Piez | http://www.wendellpiez.com
XML | XSLT | electronic publishing
Eat Your Vegetables
_____oo_________o_o___ooooo____ooooooo_^

Current Thread