Hello Christian,


Thank you very much.
This is exactly the requested solution.
A little complicate to understand at present, but it works fine.

In BaseX Documentation 8.5.2,
Chapter "Context Item" shows more information for that.


Manipulation of an XML structure .... with flexible declare function ....

If there are some similar issues or solutions in BaseX community,
this will be very interesting. Any other sources to learn more about?


Many thanks
Michael


Christian Grün hat am 29. Oktober 2017 um 17:10 geschrieben:


Hi Michael,

Please note that an expression will always be evaluated before its
result will be bound to a variable.

You didn’t mention what result you would expect. I guess it should be
as follows:

textBBB

You could pass on a path string to your function and navigate through
the single steps, e.g. with fold-left [1]:

declare function local:node-add($xml, $path, $value) {
copy $copy := $xml
modify (
let $target := fold-left(
tokenize($path, '/'),
$copy,
function($node, $step) {
$node/*[name() = $step]
}
)
return insert node $value into $target
)
return $copy
};

local:node-add(
text,
'xml',
BBB
)

If you need more flexibility, you can pass on a function that takes
care of the navigation inside the copy/modify or update expression:

declare function local:node-add($xml, $target, $value) {
$xml update {
insert node $value into $target(.)
}
};
local:node-add(
document { },
function($n) { $n/A/B },

)

Best,
Christian

[1] http://docs.basex.org/wiki/Higher-Order_Functions#fn:fold-left



On Sun, Oct 29, 2017 at 4:34 PM, wrote:
Hello,

How to add/replace a complete node in a node tree,
with done by a function and by according function parameters.
That means with flexible instructions.

Such as:

declare function local:NodeAdd($XML, $XMLPath, $XMLValue)
{
??
};

let $XML := text
let $XMLAdd := BBB
let $XMLPath := $XML/xml


return
local:NodeAdd($XML, $XMLPath , $XMLAdd)

Problem:
Following examples didn't work, because an internal reference to
copy-XML-structure is needed/forced.

example 1: copy / modify

declare function local:NodeAdd($XML, $XMLPath, $XMLValue)
{

copy $XML2 := $XML
modify (
insert node $XMLValue into $XML2/xml
)
return
$XML2
};

But: Instead of into $XML2/xml,

into $XML/xml is needed, respectively into $XMLPath


Thanks a lot
Michael