我想创建一个函数,它会在一个字符串中找到一个单词,比如
word(@class, 'items')
这将等于
fn:matches(@class, "(^|\s)items(\s|$)")
有多种方法可以做到这一点,但最简单的解决方案是使用 XQuery,它允许您在语言中定义函数:
declare function local:word($a as xs:string, $b as xs:string) as xs:boolean {
matches($a, concat("(^|\s)", $b, "(\s|$)"))
};
这是我为 XSLT 2.0 找到的
<x:transform version="2.0"
xmlns:x="http://www.w3.org/1999/XSL/Transform"
xmlns:find="http://user.com/namespace">
<x:output method="html"/>
<x:function name="find:word">
<x:param name="src"/>
<x:param name="word"/>
<x:sequence select="matches($src, concat('(^|\s)', $word, '(\s|$)'))"/>
</x:function>
<x:template match="//*[find:word(@class, 'items')]">
<x:copy>
<x:copy-of select="@*"/>
</x:copy>
</x:template>
</x:transform>