我有一个小查找文件,
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>
我正在使用此键和顺序访问
title
:
<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
可能附加了一个后缀,我可以在查找时忽略该后缀,因此我想更改密钥以使用 contains()
功能。 (@number
应该始终出现在 $docnbr
中)
那么我该如何重写密钥,使其像这样工作:
<xsl:sequence select="$dictionary/document[contains($docnbr,@number)]/title
我尝试过以下变体:
<xsl:key name="referenced-by-docnbr" match="*[contains(.,@number)]" use="@number"/>
但这不会返回任何东西。
编辑:
我已经在函数中包含了这个序列,所以我围绕 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>
编辑:
我保留了马丁的解决方案,但问题是密钥没有任何上下文。将其更改为
<xsl:key name="referenced-by-docnbr" match="*[matches(@number,'^'|| $gek,'i')]" use="@number"/>
其中 $gek
是全局变量,然后就可以了。
您可以尝试使用“记忆”功能(声明一些命名空间,例如
xmlns:mf="http://example.com/mf"
)
<xsl:function name="mf: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>
然后代替
<xsl:sequence select="key('referenced-by-docnbr',$docnbr, $dictionary)/title" />
尝试例如<xsl:sequence select="mf:lookup($docnbr)/title" />