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

时间:2013-03-28 12:53:05

标签: xpath saxon

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

word(@class, 'items')

将等于

fn:matches(@class, "(^|\s)items(\s|$)")

2 个答案:

答案 0 :(得分:1)

有多种方法可以做到这一点,但最简单的解决方案是使用XQuery,它允许您在语言中定义函数:

declare function local:word($a as xs:string, $b as xs:string) as xs:boolean {
  matches($a, concat("(^|\s)", $b, "(\s|$)"))
};

答案 1 :(得分:0)

这是我在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>