You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修改xsl:key以使用contains函数实现带后缀文档编号查询

XSLT:基于带后缀的文档编号匹配查找标题

现有文件与初始实现

我有一个用于查找的小型XML文件internal_docnbr_titles.xml,内容如下:

<custom-lookup>
        <document number="121212">
            <title>Engine Manual</title>
            <aka>EM</aka>
        </document>
        <document number="333333">
            <title>Cleaning, Inspection, and Repair Manual</title>
            <aka>CIR</aka>
        </document>
        <document number="222222">
            <title>Engine Illustrated Parts Catalog</title>
            <aka>EIPC</aka>
        </document>
        <document number="444444">
            <title>Illustrated Tool and Equipment Manual</title>
            <aka>ITEM</aka>
        </document>
</custom-lookup>

原本我通过以下XSLT代码,基于文档编号$docnbr获取对应标题:

<xsl:variable name="dictionary" select="doc('internal_docnbr_titles.xml')/custom-lookup" as="element()" />
<xsl:key name="referenced-by-docnbr" match="*" use="@number"/>
<xsl:sequence select="key('referenced-by-docnbr',$docnbr, $dictionary)/title" />

但现在$docnbr可能带有后缀,需要忽略后缀进行匹配(已知文档编号必然是$docnbr的子串),希望实现的效果等价于以下查询逻辑:

<xsl:sequence select="$dictionary/document[contains($docnbr,@number)]/title" />

我尝试过修改key为:

<xsl:key name="referenced-by-docnbr" match="*[contains(.,@number)]" use="@number"/>

但没有返回任何结果。


编辑补充1:函数化实现尝试

我原本就把查询逻辑封装在了函数里,所以套用了Martin的方案,代码如下:

<xsl:function name="fn:get-manual-title" as="xs:string?">
            <xsl:param name="docnbr" as="xs:string?"/>              
           
            <xsl:sequence select="fn:lookup($docnbr)/title" /></xsl:function>

<xsl:function name="fn:lookup" as="element(document)*" cache="yes" new-each-time="no">
            <xsl:param name="docnbr" as="xs:string?"/>
            <xsl:sequence select="$dictionary/document[contains($docnbr,@number)]"/>
        </xsl:function>

编辑补充2:最终可行方案

保留了Martin的思路,但修正了key的上下文问题,修改后的key如下(其中$gek是全局变量),功能恢复正常:

<xsl:key name="referenced-by-docnbr" match="*[matches(@number,'^'|| $gek,'i')]" use="@number"/>

内容的提问来源于stack exchange,提问作者Caroline

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 00:15:06