Недавно увидел интересный вариант использования ключей. Вещь во многом бесполезная, но уж больно красивая как по мне.
<items>
<item>Январь</item>
<item>Февраль</item>
<item>Март</item>
<item>Апрель</item>
<item>Май</item>
<item>Июнь</item>
</items>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:key name="testMatch" match="item" use="contains(., 'р')"/>
<xsl:template match="/">
<result>
<matched>
<xsl:copy-of select="key('testMatch', 'true')"/>
</matched>
<not-matched>
<xsl:copy-of select="key('testMatch', 'false')"/>
</not-matched>
</result>
</xsl:template>
</xsl:stylesheet>
<result>
<matched>
<item>Январь</item>
<item>Февраль</item>
<item>Март</item>
<item>Апрель</item>
</matched>
<not-matched>
<item>Май</item>
<item>Июнь</item>
</not-matched>
</result>
Почему не передавать просто true()? Для меня это чисто стилистическая заморочка.
The
useattribute is an expression specifying the values of the key; the expression is evaluated once for each node that matches the pattern. If the result is a node-set, then for each node in the node-set, the node that matches the pattern has a key of the specified name whose value is the string-value of the node in the node-set; otherwise, the result is converted to a string, and the node that matches the pattern has a key of the specified name with value equal to that string.
…
When the second argument to thekeyfunction is of type node-set, then the result is the union of the result of applying the key function to the string value of each of the nodes in the argument node-set. When the second argument to key is of any other type, the argument is converted to a string as if by a call to thestringfunction…
http://www.w3.org/TR/xslt#key
Такое вот в итоге явное приведение. Но key('testMatch', true()) тоже конечно работает. По крайней мере, в тех имплементациях, что есть под рукой.