[英]xquery “Content for update is empty”
This is the first time I've run into the Xquery (3.1) error Content for update is empty
and a search on Google returns nothing useful. 这是我第一次遇到Xquery(3.1)错误
Content for update is empty
,在Google上进行的搜索没有任何用处。
If I run this simple query to identify nested /tei:p/tei:p
: 如果我运行此简单查询以标识嵌套的
/tei:p/tei:p
:
for $x in $mycollection//tei:p/tei:p
return $x
I get XML fragments like the following: 我得到如下的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>
ie some with text()
and others empty
即一些与
text()
和其他为empty
I am trying to "de-duplicate" the /tei:p/tei:p
, but the following attempts return the same aforementioned error: 我正在尝试“
/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
I don't understand what the error is trying to tell me in order to correct the query. 我不明白该错误试图告诉我什么以更正查询。
Many, many thanks. 非常感谢。
edit: 编辑:
for $x in $mycollection//tei:p[tei:p and count(node()) eq 1]
let $y := $x/tei:p
return update replace $x with $y
I also tried this, replacing parent
with self
axis, which resulted in a very ambiguous error exerr:ERROR node not found
: 我也尝试过使用
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
solution: 解:
for $x in $local:COLLECTIONS//tei:p/tei:p
return if ($x/(text()|*))
then update replace $x with $x/(text()|*)
else update delete $x
The error message indicates that $y
is an empty sequence. 错误消息表明
$y
为空序列。 The XQuery Update documentation describes the replace
statement as follows: XQuery Update文档描述了
replace
语句,如下所示:
update replace expr with exprSingle
Replaces the nodes returned by
expr
with the nodes inexprSingle
.将
expr
返回的节点替换为exprSingle
的节点。expr
must evaluate to a single element, attribute, or text node.expr
必须求值为单个元素,属性或文本节点。 If it is an element,exprSingle
must contain a single element node...如果它是一个元素,则
exprSingle
必须包含一个元素节点。
In certain cases, as shown in your sample data above, $y
would return an empty sequence - which would violate the rule that expr
must evaluate to a single element. 在某些情况下,如上面的示例数据所示,
$y
将返回一个空序列-这将违反expr
必须求值为单个元素的规则。
To work around such cases, you can add a conditional expression, with an else
clause of either an empty sequence ()
or a delete statement: 要解决这种情况,您可以添加条件表达式,并在
else
子句中使用空序列()
或delete语句:
if ($y instance of element()) then
update replace $x with $y
else
update delete $x
If your goal is not simply to workaround the error, but to arrive at a more direct solution for replacing "double-nested" elements such as: 如果您的目标不是简单地解决错误,而是找到更直接的解决方案来替换“双重嵌套”元素,例如:
<p><p>Mixed <hi>content</hi>.</p></p>
.... with: ....具有:
<p>Mixed <hi>content</hi>.</p>
... I'd suggest this query, which takes care not to inadvertently delete nodes that might somehow have slipped in between the two nested <p>
elements: ...我建议使用此查询,该查询应注意不要无意中删除可能以某种方式滑入两个嵌套
<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
Given a $mycollection
such as this: 给定这样的
$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>
The query will transform the collection to be as follows: 查询将转换集合如下:
<text xmlns="http://www.tei-c.org/ns/1.0">
<p>Hello</p>
<p>Hello there</p>
<p>Hello <p>there</p></p>
</text>
This is the expected result for the query, because only the 2nd <p>
element had the nested <p>
that could be cleanly stripped off. 这是查询的预期结果,因为只有第二个
<p>
元素具有可以完全剥离的嵌套<p>
。 Obviously, if you can assume your content meets simpler patterns, you can remove the and count(node()) eq 1
condition. 显然,如果可以假设您的内容符合更简单的模式,则可以删除
and count(node()) eq 1
条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.