replacing elements with their children using update
Hello!
So I can use
copy $c := $test
modify (for $each in $c/descendant::html:ins return replace node $each with $each/node())
return $cto unwrap all the html ins elements in some test HTML.
If I try to do this using update (because I am under the perhaps mistaken impression that update can do the same things copy/modify/return can)
$test update {replace node descendant::html:ins with 'CABBAGE'}works fine; the various ins elements are found and replaced. But if I want to replace that particular ins element with its children, there doesn't seem to be a way to specify the children; the context item is the value of $test, not whatever descendant ins element is being replaced.
How could this be written using update? Can this be written using update?
thanks!
Hi Graydon,
You can use FLWOR expressions also with the update keyword:
<xml><a>A</a></xml> update {
for $a in ./a
return replace node $a with $a/node()
}A shorter (possibly cryptic) variant is to use the simple map operator (!):
<xml><a>A</a></xml> update {
a ! (replace node . with node())
}Hope this helps, Christian
Quoted text · Graydon Saunders · 19 lines
Von: Graydon Saunders via BaseX-Talk <basex-talk at mailman.uni-konstanz.de> Gesendet: Samstag, September 27, 2025 7:33:07 AM An: BaseX <basex-talk at mailman.uni-konstanz.de> Betreff: [basex-talk] replacing elements with their children using update Hello! So I can use copy $c := $test modify (for $each in $c/descendant::html:ins return replace node $each with $each/node()) return $c to unwrap all the html ins elements in some test HTML. If I try to do this using update (because I am under the perhaps mistaken impression that update can do the same things copy/modify/return can) $test update {replace node descendant::html:ins with 'CABBAGE'} works fine; the various ins elements are found and replaced. But if I want to replace that particular ins element with its children, there doesn't seem to be a way to specify the children; the context item is the value of $test, not whatever descendant ins element is being replaced. How could this be written using update? Can this be written using update? thanks! -- Graydon Saunders | graydonish at fastmail.com<mailto:graydonish at fastmail.com> Þæs oferéode, ðisses swá mæg. -- Deor ("That passed, so may this.")
Hi Christian -
That helps enormously; thank you!
I got hung up with the FLOWR expression because $test update {for $a in $test/descendant::html:ins return ....} doesn't work and I have yet to establish useful mental patterns for what the "node was not created by transform expression" error message means.
The terse second version is what I was after; thank you!
-- Graydon
Quoted text · Christian Grün · 51 lines
On Sat, Sep 27, 2025, at 02:59, Christian Grün wrote: > Hi Graydon, > > You can use FLWOR expressions also with the update keyword: > > <xml><a>A</a></xml> update { > for $a in ./a > return replace node $a with $a/node() > } > > A shorter (possibly cryptic) variant is to use the simple map operator (!): > > <xml><a>A</a></xml> update { > a ! (replace node . with node()) > } > > Hope this helps, > Christian > > > *Von:* Graydon Saunders via BaseX-Talk <basex-talk at mailman.uni-konstanz.de> > *Gesendet:* Samstag, September 27, 2025 7:33:07 AM > *An:* BaseX <basex-talk at mailman.uni-konstanz.de> > *Betreff:* [basex-talk] replacing elements with their children using update > > Hello! > > So I can use > > copy $c := $test > modify (for $each in $c/descendant::html:ins return replace node $each with $each/node()) > return $c > > to unwrap all the html ins elements in some test HTML. > > If I try to do this using update (because I am under the perhaps mistaken impression that update can do the same things copy/modify/return can) > > $test update {replace node descendant::html:ins with 'CABBAGE'} > > works fine; the various ins elements are found and replaced. But if I want to replace that particular ins element with its children, there doesn't seem to be a way to specify the children; the context item is the value of $test, not whatever descendant ins element is being replaced. > > How could this be written using update? Can this be written using update? > > thanks! > > -- > Graydon Saunders | graydonish at fastmail.com > Þæs oferéode, ðisses swá mæg. > -- Deor ("That passed, so may this.") > >
Written for BaseX 12.0. BaseX may have changed since: please check the current documentation.