簡體   English   中英

xquery“更新內容為空”

[英]xquery “Content for update is empty”

這是我第一次遇到Xquery(3.1)錯誤Content for update is empty ,在Google上進行的搜索沒有任何用處。

如果我運行此簡單查詢以標識嵌套的/tei:p/tei:p

for $x in $mycollection//tei:p/tei:p
return $x

我得到如下的XML片段:

<p xmlns="http://www.tei-c.org/ns/1.0"/>
<p xmlns="http://www.tei-c.org/ns/1.0">Histoires qui sont maintenant du passé (Konjaku monogatari shū). Traduction, introduction et
commentaires de Bernard Frank, Paris, Gallimard/UNESCO, 1987 [1re éd. 1968] (Connaissance de
l'Orient, Série japonaise, 17), p. 323. </p>
<p xmlns="http://www.tei-c.org/ns/1.0">Ed. Chavannes, Cinq cents contes et apologues extraits du Tripitaka chinois, Paris, t. 4,
1934, Notes complémentaires..., p. 147.</p>
<p xmlns="http://www.tei-c.org/ns/1.0"/>
<p xmlns="http://www.tei-c.org/ns/1.0">Ed. Chavannes, Cinq cents contes et apologues extraits du Tripitaka chinois, Paris, t. 4,
1934, Notes complémentaires..., p. 129.</p>

即一些與text()和其他為empty

我正在嘗試“ /tei:p/tei:p重復” /tei:p/tei:p ,但是以下嘗試返回相同的上述錯誤:

for $x in $mycollection//tei:p/tei:p
return update replace $x with $x/(text()|*)


for $x in $mycollection//tei:p/tei:p
let $y := $x/(text()|*)
return update replace $x with $y

我不明白該錯誤試圖告訴我什么以更正查詢。

非常感謝。

編輯:

for $x in $mycollection//tei:p[tei:p and count(node()) eq 1]
let $y := $x/tei:p
return update replace $x with $y

我也嘗試過使用parent軸替換self軸,這導致非常模棱兩可的錯誤exerr:ERROR node not found

for $x in $mycollection//tei:p/tei:p
let $y := $x/self::*
return update replace $x/parent::* with $y

解:

for $x in $local:COLLECTIONS//tei:p/tei:p
return if ($x/(text()|*))
        then update replace $x with $x/(text()|*)
        else update delete $x

錯誤消息表明$y為空序列。 XQuery Update文檔描述了replace語句,如下所示:

 update replace expr with exprSingle 

expr返回的節點替換為exprSingle的節點。 expr必須求值為單個元素,屬性或文本節點。 如果它是一個元素,則exprSingle必須包含一個元素節點。

在某些情況下,如上面的示例數據所示, $y將返回一個空序列-這將違反expr必須求值為單個元素的規則。

要解決這種情況,您可以添加條件表達式,並在else子句中使用空序列()或delete語句:

if ($y instance of element()) then 
    update replace $x with $y
else 
    update delete $x

如果您的目標不是簡單地解決錯誤,而是找到更直接的解決方案來替換“雙重嵌套”元素,例如:

<p><p>Mixed <hi>content</hi>.</p></p>

....具有:

<p>Mixed <hi>content</hi>.</p>

...我建議使用此查詢,該查詢應注意不要無意中刪除可能以某種方式滑入兩個嵌套<p>元素之間的節點:

xquery version "3.1";

declare namespace tei="http://www.tei-c.org/ns/1.0";

for $x in $mycollection//tei:p[tei:p and count(node()) eq 1]
let $y := $x/tei:p
return
    update replace $x with $y

給定這樣的$mycollection

<text xmlns="http://www.tei-c.org/ns/1.0">
    <p>Hello</p>
    <p><p>Hello there</p></p>
    <p>Hello <p>there</p></p>
</text>

查詢將轉換集合如下:

<text xmlns="http://www.tei-c.org/ns/1.0">
    <p>Hello</p>
    <p>Hello there</p>
    <p>Hello <p>there</p></p>
</text>

這是查詢的預期結果,因為只有第二個<p>元素具有可以完全剝離的嵌套<p> 顯然,如果可以假設您的內容符合更簡單的模式,則可以刪除and count(node()) eq 1條件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM