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.
If I run this simple query to identify nested /tei:p/tei:p:
for $x in $mycollection//tei:p/tei:p
return $x
I get XML fragments like the following:
<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>
i.e. some with text() and others empty
I am trying to "de-duplicate" the /tei:p/tei:p, but the following attempts return the same aforementioned error:
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:
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
$yis an empty sequence. The XQuery Update documentation describes thereplacestatement as follows:In certain cases, as shown in your sample data above,
$ywould return an empty sequence - which would violate the rule thatexprmust evaluate to a single element.To work around such cases, you can add a conditional expression, with an
elseclause of either an empty sequence()or a delete statement: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:
.... with:
... 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:Given a
$mycollectionsuch as this:The query will transform the collection to be as follows:
This is the expected result for the query, because only the 2nd
<p>element had the nested<p>that could be cleanly stripped off. Obviously, if you can assume your content meets simpler patterns, you can remove theand count(node()) eq 1condition.