8. insert-after

insert-after(inserted, after?)

Insert inserted, one or more nodes, after node after. Parameter after defaults to the context node.

Examples:

insert-after(copy(.));

insert-after(<meta name="{$metaName}" content="{$metaContent}"/>, $title);

Always pass detached nodes as the first argument of insert or replace

The nodes passed as the first argument of commands insert-after, insert-before, insert-into or replace must have no parent nodes (“detached nodes”).

Example: something like what follows works fine:

delete($caption);
insert-after($caption, $table);

While something like what follows will report an execution error:

insert-after($caption, $table);
delete($caption);

When passing detached nodes is not possible or not desirable, simply use XPath extension function copy() to copy the nodes. Example:

insert-after(copy($caption), $table);
delete($caption);