应用于此 XML 文档时 : 01 05 0-6ren">
gpt4 book ai didi

xslt - xsl :key key() function lookup greater than/less than

转载 作者:行者123 更新时间:2023-12-04 06:39:11 25 4
gpt4 key购买 nike

有没有办法使用大于/小于进行 key() 查找?

示例:key('akeyname', <8) 将返回键字符串值小于 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



不,因为 key() 的第二个参数函数必须是表达式 ,但是 "<8"不是语法上合法的 XPath 表达式。

最接近你想要的 :
<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>

应用于此 XML 文档时 :
<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>

产生了想要的、正确的结果 :
<result>
<num>01</num>
<num>05</num>
<num>03</num>
<num>04</num>
<num>06</num>
<num>07</num>
<num>02</num>
</result>

一个更灵活的解决方案是在 XSLT 中使用高阶函数 (HOF),它已由 FXSL library 实现多年。 (完全用 XSLT 编写)。

这是使用 HOF 的解决方案:
<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>

当此转换应用于同一个 XML 文档(上图)时,会产生所需的正确结果 :
1 5 3 4 6 7 2

备注 : HOF 将在即将发布的 3.0 版中成为 XPath/XSLT/XQuery 的标准特性。

关于xslt - xsl :key key() function lookup greater than/less than,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4479441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com