使xquery函数递归时出现问题

时间:2018-09-21 13:48:35

标签: xquery

我在编写递归编写的函数时遇到了一些困难。我需要能够打开此xml:

<entry ref="22">
    <headword>abaishen</headword>
    <part_of_speech> v. </part_of_speech>
    <variant>abeishen</variant>
    <variant>abaissen</variant>
    <variant>abeisen</variant>
    <variant>abashen</variant>
    <variant>abasshen</variant>
    <variant>abassen</variant>
    <variant>abeeshen</variant>
    <variant>abesen</variant>
    <variant>abessen</variant>
    <variant>abaished</variant>
    <variant>-et</variant>
    <variant>-it</variant>
    <variant>abaisht</variant>
    <variant>abaist</variant>
    <variant>abasht</variant>
    <variant>abast</variant>
</entry>

放入此XML中-本质上是将最后一个完整条目的词干替换为以“-”开头的所有条目的结尾:

 <entry ref="22">
        <headword>abaishen</headword>
        <variant>abeishen</variant>
        <variant>abaissen</variant>
        <variant>abeisen</variant>
        <variant>abashen</variant>
        <variant>abasshen</variant>
        <variant>abassen</variant>
        <variant>abeeshen</variant>
        <variant>abesen</variant>
        <variant>abessen</variant>
        <variant>abaished</variant>
        <variant>abaishet</variant>
        <variant>abaishit</variant>
        <variant>abaisht</variant>
        <variant>abaist</variant>
        <variant>abasht</variant>
        <variant>abast</variant>
        <part_of_speech> v. </part_of_speech>
    </entry>

我遇到的问题是第二个条目-it,它返回“ abaishet”以及我当前拥有的代码:

declare function local:hyphen-replace($f) {
let $j :=
   if (substring($f/text(), 1, 1) = "-") then
      let $ending := substring-after($f/text(),"-")
      let $ending-length := string-length($ending)
      let $previous := $f/preceding-sibling::*[1]
      let $previous-length := string-length($previous)
      return
           if (substring($previous/text(), 1, 1) = "-") then
               local:hyphen-replace($previous)
           else
               element {name($f)} {concat(substring($previous,1,($previous-length - $ending-length)),$ending)}   
 else
     $f
return $j
};

declare function local:verbCheck($nodes as node()*) as node()* {

    let $d := $nodes/part_of_speech
    let $s := functx:siblings($d)
    let $p := for $node in $nodes
                return
                    let $d := $node/part_of_speech
                    let $s := functx:siblings($d)
                    return
                        if ($d/text() = " v. ") then
                            for $f in $s
                                let $j := 
                                    local:hyphen-replace($f)
                            return ($j)


                        else
                            <empty/>

    return
      ($p,$d)
};

<list>
    {

        let $collection := concat($collection, '?select=*.xml')
        let $q := collection($collection)
        let $v := local:buildNodes($q)

        let $entries := 
            for $n in $v
            return <entry ref="{$n/@ref}">{local:verbCheck($n)}</entry>

        return local:remove-empty-elements($entries)

    }
</list>

对我来说,很明显我的问题是local:hypen-replace中的这段代码:

if (substring($previous/text(), 1, 1) = "-") then
                   local:hyphen-replace($previous)

因为它正在调用紧接的上一个项目,并用其信息替换“ -it”节点。但是我不知道如何重写它以使其递归正常工作。任何建议,将不胜感激。谢谢。

0 个答案:

没有答案