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.
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.
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.