如何在Saxon中向XPath添加字符串函数



我想创建一个函数,它将在字符串中查找一个单词,如

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>

最新更新