2

Is there a way to do a key() lookup using greaterthan/ lessthan?

example: key('akeyname', <8) would return all nodes with the key string value less than 8.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431

1 Answers1

3

Is there a way to do a key() lookup using greaterthan/ lessthan?

example: key('akeyname', <8) would return all nodes with the key string value less than 8

No, because the second argument of the key() function must be an expression, but "<8" isn't a syntactically legal XPath expression.

The closest to what you want:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kLT8" match="num" use="not(. >= 8)"/>

 <xsl:template match="/">
  <result>
   <xsl:copy-of select="key('kLT8', 'true')"/>
  </result>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document:

<nums>
  <num>01</num>
  <num>05</num>
  <num>03</num>
  <num>04</num>
  <num>08</num>
  <num>06</num>
  <num>07</num>
  <num>02</num>
  <num>09</num>
  <num>10</num>
</nums>

the wanted, correct result is produced:

<result>
   <num>01</num>
   <num>05</num>
   <num>03</num>
   <num>04</num>
   <num>06</num>
   <num>07</num>
   <num>02</num>
</result>

A more flexible solution is the use of Higher Order Functions (HOFs) in XSLT, which has been implemented for years by the FXSL library (written entirely in XSLT).

Here is the solution using HOFs:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:f="http://fxsl.sf.net/">
 <xsl:import href="../f/func-Operators.xsl"/>
 <xsl:import href="../f/func-filter.xsl"/>

 <xsl:param name="pLimit" as="xs:integer" select="8"/>

 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
  <xsl:sequence select="f:filter(*/number(), f:gt($pLimit))"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the same XML document (above), the wanted, correct result is produced:

1 5 3 4 6 7 2

Note: HOFs will become a standard feature of XPath/XSLT/XQuery with the forthcoming version 3.0.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • +1. Reading your answers is totally useful. Passing true as a key argument is an awesome solution. – Flack Dec 18 '10 at 18:55
  • @Flack: You are welcome. I will update this answer soon with something that might be even more interesting. – Dimitre Novatchev Dec 18 '10 at 18:59
  • A good answer, thanks. At least I know that I am not simply overlooking some simple syntax. I thought the lookup was more like a string, not an xpath statement. I will post a little more context on what I am trying to do when I get a chance. – Darin Walsh Dec 18 '10 at 19:26
  • @Darin-Walsh: You are welcome. :) See my update that shows the use of HOFs as a more flexible solution. – Dimitre Novatchev Dec 18 '10 at 19:31
  • +1 for a solution I didn't think of and also for the FXSL example. Getting more and more interested in FXSL the more I look at it. If you have some recommended resources (except the obvious one, the FXSL site) please share! – Per T Dec 18 '10 at 20:22
  • @Per-T: This paper (for FXSL2): http://conferences.idealliance.org/extreme/html/2006/Novatchev01/EML2006Novatchev01.html is probably a good resource. The paper for FXSL1 is here: http://conferences.idealliance.org/extreme/html/2003/Novatchev01/EML2003Novatchev01.html – Dimitre Novatchev Dec 18 '10 at 20:24