使用XQuery / Xpath检测xml:id序列中的间隙/第一个ID

问题描述 投票:1回答:2

我有一个主xml文件,其中包含如下列表:

<listPerson>
    <person xml:id="pe0001">
        <persName>
            <surname>Anderson</surname>
            [...]
       </persName>
    </person>
    <person xml:id="pe0002">
        <persName>
            <surname>Smith</surname>
            [...]
        </persName>
    </person>
    <person xml:id="pe0004">
        <persName>
            <surname>Another</surname>
            [...]
        </persName>
    </person>
</listPerson>

我有一个html表单,它调用app.xql中的一个应用程序,并在master xml文件中插入一个新的<person>记录。如果ID序列中存在间隙(例如上面的ID pe0003),我希望eXist-db返回该ID并“填补空白”,否则只输出最新的可用ID(即pe0005)。我已经完成了最后一件事:

declare function app:addPers($node as node(), $model as map(*)) {

    let $peid := doc('masterfile.xml')//tei:listPerson/tei:person[@xml:id][last()]/@xml:id
    let $idnumber := xs:decimal(substring-after($peid, 'pe'))
    let $newidnumber := (sum($idnumber + 1))
    let $newpeid := concat('pe0', $newidnumber)

    return

<html stuff>

}

我现在要做的是使用XQuery / Xpath代码来检测序列中是否存在间隙并相应地采取行动。这是我到目前为止所做的:

[app.xql]

declare function app:addPers($node as node(), $model as map(*)) {

let $seqpe := doc('masterfile.xml')//tei:listPerson/tei:person[@xml:id]/@xml:id
        let $peid := 
        for $item at $pos in $seqpe
            let $item := xs:decimal(substring-after($seqpe, 'pe'))
            return if ($item[$pos + 1] - $item[$pos] != 1) then 
            doc('masterfile.xml')//tei:listPerson/tei:person[@xml:id][$item]/@xml:id
        else 
        doc('masterfile.xml')//tei:listPerson/tei:person[@xml:id][last()]/@xml:id

        let $newidnumber := (sum($peid + 1))
        let $newpeid := concat('pe0', $newidnumber)
 return 

<html stuff>

}

这会返回err:FORG0001 cannot construct xs:decimal from ""错误。我究竟做错了什么?

更新

这是我做的另一个测试,它返回一个err:XPDY0002 Undefined context sequence for 'following-sibling::tei:person错误:

let $seqpe := doc('masterfile.xml')//tei:listPerson/tei:person
         let $peid := 
         for $item in $seqpe
             return if ((xs:decimal(substring-after(following-sibling::tei:person/@xml:id, 'pe'))) - (xs:decimal(substring-after($item/@xml:id, 'pe'))) ne 1) then 
             doc('masterfile.xml')//tei:listPerson/tei:person[@xml:id eq $item/@xml:id]/@xml:id
         else 
         doc('masterfile.xml')//tei:listPerson/tei:person[@xml:id][last()]/@xml:id

     let $newidnumber := (sum($peid + 1))
     let $newpeid := concat('pe0', $newidnumber)

第二次更新

至于返回最后一个ID,这两个代码:

(let $idnext :=

  for $person in doc('/db/apps/app-ct/data/indices/pedb.xml')//tei:listPerson/tei:person[position() ne last()]
  where local:get-id($person/@xml:id) ne (local:get-id($person/following-sibling::tei:person[1]/@xml:id) - 1)
return 
    if (empty($idnext)) then
    (local:get-id(listPerson/person[last()]/@xml:id) + 1)
    else (local:get-id($person/@xml:id) + 1)
let $newpeid := 
if (fn:string-length($idnext) = 1) then
   concat('pe000', $idnext) else if
   (fn:string-length($idnext) = 2) then 
   concat('pe00', $idnext) else if 
   (fn:string-length($idnext) = 3) then 
   concat('pe0', $idnext) else 
   concat('pe', $idnext)

return

<html stuff>)[1]

还有这个:

    (let $idnext :=

      for $person in doc('/db/apps/app-ct/data/indices/pedb.xml')//tei:listPerson/tei:person[position() ne last()]
      where local:get-id($person/@xml:id) ne (local:get-id($person/following-sibling::tei:person[1]/@xml:id) - 1)
      return local:get-id($person/@xml:id) + 1
    return 
        if (empty($idnext)) then
        (local:get-id(listPerson/person[last()]/@xml:id) + 1)
        else ($idnext),
    let $newpeid := 
    if (fn:string-length($idnext) = 1) then
       concat('pe000', $idnext) else if 
       (fn:string-length($idnext) = 2) then 
       concat('pe00', $idnext) else if 
       (fn:string-length($idnext) = 3) then 
       concat('pe0', $idnext) else 
       concat('pe', $idnext)

    return

<html stuff>)[1]

返回err:XPDY0002 variable '$idnext' is not set.错误。

第三次和最后的更新

下面的代码正是我想要的,即返回第一个可用的ID,不管它是否在差距内。

let $id_gap :=

        (for $person in doc('myfile.xml')//tei:listPerson/tei:person[position() ne last()]
        where local:get-id($person/@xml:id) ne (local:get-id($person/following-sibling::tei:person[1]/@xml:id) - 1)
        return (local:get-id($person/@xml:id) + 1))[1]

        let $idnext :=
        if (empty($id_gap))
        then (local:get-id(doc('myfile.xml')//tei:listPerson/tei:person[last()]/@xml:id) + 1)
        else ($id_gap)

        let $newpeid := 
         if (fn:string-length($idnext) = 1) then
            concat('pe000', $idnext) else if 
            (fn:string-length($idnext) = 2) then 
            concat('pe00', $idnext) else if 
            (fn:string-length($idnext) = 3) then 
            concat('pe0', $idnext) else 
            concat('pe', $idnext)

       return

  <html code>
xml xpath xquery exist-db
2个回答
2
投票

我试过这样的:

declare function local:get-id($xml-id as xs:string) as xs:integer {
    xs:integer(replace($xml-id, '[^0-9]+', ''))
};

for $person in (listPerson/person)[position() ne last()]
where local:get-id($person/@xml:id) ne (local:get-id($person/following-sibling::person[1]/@xml:id) - 1)
return local:get-id($person/@xml:id) + 1

并在http://xqueryfiddle.liberty-development.net/nbUY4kh进行样本输入

<listPerson>
    <person xml:id="pe0001">
        <persName>
            <surname>Anderson</surname>
            [...]
       </persName>
    </person>
    <person xml:id="pe0003">
        <persName>
            <surname>Smith</surname>
            [...]
        </persName>
    </person>
    <person xml:id="pe0004">
        <persName>
            <surname>Another</surname>
            [...]
        </persName>
    </person>
    <person xml:id="pe0005">
        <persName>
            <surname>Another</surname>
            [...]
        </persName>
    </person>
    <person xml:id="pe0006">
        <persName>
            <surname>Another</surname>
            [...]
        </persName>
    </person>
    <person xml:id="pe0008">
        <persName>
            <surname>Another</surname>
            [...]
        </persName>
    </person>
    <person xml:id="pe0009">
        <persName>
            <surname>Another</surname>
            [...]
        </persName>
    </person>
    <person xml:id="pe0010">
        <persName>
            <surname>Another</surname>
            [...]
        </persName>
    </person>
    <person xml:id="pe0014">
        <persName>
            <surname>Another</surname>
            [...]
        </persName>
    </person>
</listPerson>

它给

2
7
11

虽然我不确定Exist-Db是否支持,但它也可以通过窗口子句来实现。

至于在没有间隙的情况下返回新的id,我不确定是否有更优雅或更紧凑的解决方案,但我猜一个简单的检查

let $new-ids :=
    for $person in (listPerson/person)[position() ne last()]
    where local:get-id($person/@xml:id) ne (local:get-id($person/following-sibling::person[1]/@xml:id) - 1)
    return local:get-id($person/@xml:id) + 1
return
    if (empty($new-ids))
    then local:get-id(listPerson/person[last()]/@xml:id) + 1
    else $new-ids

实现你的口头描述:http://xqueryfiddle.liberty-development.net/nbUY4kh/2


1
投票

另一种方法:

(for $key in (1 to 9999)!format-number(., '0000')
 where empty($persons[@xml:id=$key])
 return $key)[1]

获取1到9999范围内的第一个数字NNNN,其中$ person中没有元素,xml:id等于peNNNN。

© www.soinside.com 2019 - 2024. All rights reserved.