I just realized, that there is another, simpler way to simulate a defer XPath expression. So I ended up using:
declare function local:add-node( $input as node()*, $what as node()* ) as node() { copy $c := $input modify insert node $what into $c return $c }; declare function local:add-node( $input as node()*, $what as node()*, $at-location as xs:string ) as node() { copy $c := $input modify let $xpath := xquery:eval($at-location, map{"":$c}) return insert node $what into $c/$xpath return $c };
<html></html> => local:add-node((<head></head>,<body></body>)) => local:add-node(<title>Example Title</title>, "head")